001package com.nimbusds.oauth2.sdk.client; 002 003 004import java.net.URI; 005import java.util.Collections; 006import java.util.Date; 007import java.util.HashSet; 008import java.util.Set; 009 010import net.jcip.annotations.Immutable; 011 012import net.minidev.json.JSONObject; 013 014import com.nimbusds.oauth2.sdk.ParseException; 015import com.nimbusds.oauth2.sdk.auth.Secret; 016import com.nimbusds.oauth2.sdk.id.ClientID; 017import com.nimbusds.oauth2.sdk.token.BearerAccessToken; 018 019 020/** 021 * Client information. Encapsulates the registration and metadata details of 022 * an OAuth 2.0 client: 023 * 024 * <ul> 025 * <li>The client identifier. 026 * <li>The client metadata. 027 * <li>The optional client secret for a confidential client. 028 * <li>The optional registration URI and access token if dynamic client 029 * registration is permitted. 030 * </ul> 031 * 032 * <p>Related specifications: 033 * 034 * <ul> 035 * <li>OAuth 2.0 Dynamic Client Registration Protocol (RFC 7591), section 036 * 3.2.1. 037 * <li>OAuth 2.0 Dynamic Client Registration Management Protocol (RFC 038 * 7592), section 3. 039 * </ul> 040 */ 041@Immutable 042public class ClientInformation { 043 044 045 /** 046 * The registered parameter names. 047 */ 048 private static final Set<String> REGISTERED_PARAMETER_NAMES; 049 050 051 /** 052 * Initialises the registered parameter name set. 053 */ 054 static { 055 Set<String> p = new HashSet<>(ClientMetadata.getRegisteredParameterNames()); 056 057 p.add("client_id"); 058 p.add("client_id_issued_at"); 059 p.add("client_secret"); 060 p.add("client_secret_expires_at"); 061 p.add("registration_access_token"); 062 p.add("registration_client_uri"); 063 064 REGISTERED_PARAMETER_NAMES = Collections.unmodifiableSet(p); 065 } 066 067 068 /** 069 * The registered client ID. 070 */ 071 private final ClientID id; 072 073 074 /** 075 * The date the client ID was issued at. 076 */ 077 private final Date issueDate; 078 079 080 /** 081 * The client metadata. 082 */ 083 private final ClientMetadata metadata; 084 085 086 /** 087 * The optional client secret. 088 */ 089 private final Secret secret; 090 091 092 /** 093 * The client registration URI. 094 */ 095 private final URI registrationURI; 096 097 098 /** 099 * The client registration access token. 100 */ 101 private final BearerAccessToken accessToken; 102 103 104 /** 105 * Creates a new client information instance. 106 * 107 * @param id The client identifier. Must not be 108 * {@code null}. 109 * @param issueDate The issue date of the client identifier, 110 * {@code null} if not specified. 111 * @param metadata The client metadata. Must not be 112 * {@code null}. 113 * @param secret The optional client secret, {@code null} if 114 * not specified. 115 */ 116 public ClientInformation(final ClientID id, 117 final Date issueDate, 118 final ClientMetadata metadata, 119 final Secret secret) { 120 121 this(id, issueDate, metadata, secret, null, null); 122 } 123 124 125 /** 126 * Creates a new client information instance permitting dynamic client 127 * registration management. 128 * 129 * @param id The client identifier. Must not be 130 * {@code null}. 131 * @param issueDate The issue date of the client identifier, 132 * {@code null} if not specified. 133 * @param metadata The client metadata. Must not be 134 * {@code null}. 135 * @param secret The optional client secret, {@code null} if 136 * not specified. 137 * @param registrationURI The client registration URI, {@code null} if 138 * not specified. 139 * @param accessToken The client registration access token, 140 * {@code null} if not specified. 141 */ 142 public ClientInformation(final ClientID id, 143 final Date issueDate, 144 final ClientMetadata metadata, 145 final Secret secret, 146 final URI registrationURI, 147 final BearerAccessToken accessToken) { 148 149 if (id == null) 150 throw new IllegalArgumentException("The client identifier must not be null"); 151 152 this.id = id; 153 154 this.issueDate = issueDate; 155 156 if (metadata == null) 157 throw new IllegalArgumentException("The client metadata must not be null"); 158 159 this.metadata = metadata; 160 161 this.secret = secret; 162 163 this.registrationURI = registrationURI; 164 165 this.accessToken = accessToken; 166 } 167 168 169 /** 170 * Gets the registered client metadata parameter names. 171 * 172 * @return The registered parameter names, as an unmodifiable set. 173 */ 174 public static Set<String> getRegisteredParameterNames() { 175 176 return REGISTERED_PARAMETER_NAMES; 177 } 178 179 180 /** 181 * Gets the client identifier. Corresponds to the {@code client_id} 182 * client registration parameter. 183 * 184 * @return The client ID. 185 */ 186 public ClientID getID() { 187 188 return id; 189 } 190 191 192 /** 193 * Gets the issue date of the client identifier. Corresponds to the 194 * {@code client_id_issued_at} client registration parameter. 195 * 196 * @return The issue date, {@code null} if not specified. 197 */ 198 public Date getIDIssueDate() { 199 200 return issueDate; 201 } 202 203 204 /** 205 * Gets the client metadata. 206 * 207 * @return The client metadata. 208 */ 209 public ClientMetadata getMetadata() { 210 211 return metadata; 212 } 213 214 215 /** 216 * Gets the client secret. Corresponds to the {@code client_secret} and 217 * {@code client_secret_expires_at} client registration parameters. 218 * 219 * @return The client secret, {@code null} if not specified. 220 */ 221 public Secret getSecret() { 222 223 return secret; 224 } 225 226 227 /** 228 * Gets the URI of the client registration. Corresponds to the 229 * {@code registration_client_uri} client registration parameter. 230 * 231 * @return The registration URI, {@code null} if not specified. 232 */ 233 public URI getRegistrationURI() { 234 235 return registrationURI; 236 } 237 238 239 /** 240 * Gets the registration access token. Corresponds to the 241 * {@code registration_access_token} client registration parameter. 242 * 243 * @return The registration access token, {@code null} if not 244 * specified. 245 */ 246 public BearerAccessToken getRegistrationAccessToken() { 247 248 return accessToken; 249 } 250 251 252 /** 253 * Returns the JSON object representation of this client information 254 * instance. 255 * 256 * @return The JSON object. 257 */ 258 public JSONObject toJSONObject() { 259 260 JSONObject o = metadata.toJSONObject(); 261 262 o.put("client_id", id.getValue()); 263 264 if (issueDate != null) { 265 266 o.put("client_id_issued_at", issueDate.getTime() / 1000); 267 } 268 269 if (secret != null) { 270 o.put("client_secret", secret.getValue()); 271 272 if (secret.getExpirationDate() != null) { 273 o.put("client_secret_expires_at", secret.getExpirationDate().getTime() / 1000); 274 } else { 275 o.put("client_secret_expires_at", 0L); 276 } 277 } 278 279 if (registrationURI != null) { 280 281 o.put("registration_client_uri", registrationURI.toString()); 282 } 283 284 if (accessToken != null) { 285 286 o.put("registration_access_token", accessToken.getValue()); 287 } 288 289 return o; 290 } 291 292 293 /** 294 * Parses a client information instance from the specified JSON object. 295 * 296 * @param jsonObject The JSON object to parse. Must not be 297 * {@code null}. 298 * 299 * @return The client information. 300 * 301 * @throws ParseException If the JSON object couldn't be parsed to a 302 * client information instance. 303 */ 304 public static ClientInformation parse(final JSONObject jsonObject) 305 throws ParseException { 306 307 return new ClientInformation( 308 ClientCredentialsParser.parseID(jsonObject), 309 ClientCredentialsParser.parseIDIssueDate(jsonObject), 310 ClientMetadata.parse(jsonObject), 311 ClientCredentialsParser.parseSecret(jsonObject), 312 ClientCredentialsParser.parseRegistrationURI(jsonObject), 313 ClientCredentialsParser.parseRegistrationAccessToken(jsonObject)); 314 } 315}