001/* 002 * oauth2-oidc-sdk 003 * 004 * Copyright 2012-2016, Connect2id Ltd and contributors. 005 * 006 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use 007 * this file except in compliance with the License. You may obtain a copy of the 008 * License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software distributed 013 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 014 * CONDITIONS OF ANY KIND, either express or implied. See the License for the 015 * specific language governing permissions and limitations under the License. 016 */ 017 018package com.nimbusds.oauth2.sdk.client; 019 020 021import java.net.URI; 022import java.util.Date; 023 024import net.minidev.json.JSONObject; 025 026import com.nimbusds.oauth2.sdk.ParseException; 027import com.nimbusds.oauth2.sdk.auth.Secret; 028import com.nimbusds.oauth2.sdk.id.ClientID; 029import com.nimbusds.oauth2.sdk.token.BearerAccessToken; 030import com.nimbusds.oauth2.sdk.util.JSONObjectUtils; 031 032 033/** 034 * Client credentials parser. 035 */ 036public class ClientCredentialsParser { 037 038 039 /** 040 * Parses a client identifier from the specified JSON object. 041 * 042 * @param jsonObject The JSON object. Must not be {@code null}. 043 * 044 * @return The client identifier. 045 * 046 * @throws ParseException If parsing failed. 047 */ 048 public static ClientID parseID(final JSONObject jsonObject) 049 throws ParseException { 050 051 return new ClientID(JSONObjectUtils.getString(jsonObject, "client_id")); 052 } 053 054 055 /** 056 * Parses a client identifier issue date from the specified JSON 057 * object. 058 * 059 * @param jsonObject The JSON object. Must not be {@code null}. 060 * 061 * @return The client identifier issue date, {@code null} if not 062 * specified. 063 * 064 * @throws ParseException If parsing failed. 065 */ 066 public static Date parseIDIssueDate(final JSONObject jsonObject) 067 throws ParseException { 068 069 if (jsonObject.containsKey("client_id_issued_at")) { 070 071 return new Date(JSONObjectUtils.getLong(jsonObject, "client_id_issued_at") * 1000); 072 } else { 073 return null; 074 } 075 } 076 077 078 /** 079 * Parses a client secret from the specified JSON object. 080 * 081 * @param jsonObject The JSON object. Must not be {@code null}. 082 * 083 * @return The client secret, {@code null} if not specified. 084 * 085 * @throws ParseException If parsing failed. 086 */ 087 public static Secret parseSecret(final JSONObject jsonObject) 088 throws ParseException { 089 090 if (jsonObject.containsKey("client_secret")) { 091 092 String value = JSONObjectUtils.getString(jsonObject, "client_secret"); 093 094 Date exp = null; 095 096 if (jsonObject.containsKey("client_secret_expires_at")) { 097 098 final long t = JSONObjectUtils.getLong(jsonObject, "client_secret_expires_at"); 099 100 if (t > 0) { 101 exp = new Date(t * 1000); 102 } 103 } 104 105 return new Secret(value, exp); 106 } else { 107 return null; 108 } 109 } 110 111 112 /** 113 * Parses a client registration URI from the specified JSON object. 114 * 115 * @param jsonObject The JSON object. Must not be {@code null}. 116 * 117 * @return The client registration URI, {@code null} if not specified. 118 * 119 * @throws ParseException If parsing failed. 120 */ 121 public static URI parseRegistrationURI(final JSONObject jsonObject) 122 throws ParseException { 123 124 if (jsonObject.containsKey("registration_client_uri")) { 125 126 return JSONObjectUtils.getURI(jsonObject, "registration_client_uri"); 127 } else { 128 return null; 129 } 130 } 131 132 133 /** 134 * Parses a client registration access token from the specified JSON 135 * object. 136 * 137 * @param jsonObject The JSON object. Must not be {@code null}. 138 * 139 * @return The client registration access token, {@code null} if not 140 * specified. 141 * 142 * @throws ParseException If parsing failed. 143 */ 144 public static BearerAccessToken parseRegistrationAccessToken(final JSONObject jsonObject) 145 throws ParseException { 146 147 if (jsonObject.containsKey("registration_access_token")) { 148 149 return new BearerAccessToken( 150 JSONObjectUtils.getString(jsonObject, "registration_access_token")); 151 } else { 152 return null; 153 } 154 } 155}