001package com.nimbusds.openid.connect.sdk.rp;
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.client.ClientInformation;
017import com.nimbusds.oauth2.sdk.client.ClientMetadata;
018import com.nimbusds.oauth2.sdk.id.ClientID;
019import com.nimbusds.oauth2.sdk.token.BearerAccessToken;
020import com.nimbusds.oauth2.sdk.util.JSONObjectUtils;
021
022
023/**
024 * OpenID Connect client information. Encapsulates the registration and 
025 * metadata details of an OpenID Connect client:
026 * 
027 * <ul>
028 *     <li>The client identifier.
029 *     <li>The client registration URI and access token.
030 *     <li>The client OpenID Connect metadata.
031 *     <li>The optional client secret for a confidential client.
032 * </ul>
033 *
034 * <p>Related specifications:
035 *
036 * <ul>
037 *     <li>OpenID Connect Dynamic Client Registration 1.0.
038 *     <li>OAuth 2.0 Dynamic Client Registration Protocol 
039 *         (draft-ietf-oauth-dyn-reg-14), sections 2, 3.2 and 5.1.
040 * </ul>
041 */
042@Immutable
043public final class OIDCClientInformation extends ClientInformation {
044
045
046        /**
047         * The registered parameter names.
048         */
049        private static final Set<String> REGISTERED_PARAMETER_NAMES;
050
051
052        /**
053         * Initialises the registered parameter name set.
054         */
055        static {
056                Set<String> p = new HashSet<String>(ClientInformation.getRegisteredParameterNames());
057
058                p.addAll(OIDCClientMetadata.getRegisteredParameterNames());
059
060                REGISTERED_PARAMETER_NAMES = Collections.unmodifiableSet(p);
061        }
062
063        
064        /**
065         * Creates a new OpenID Connect client information instance.
066         * 
067         * @param id              The client identifier. Must not be 
068         *                        {@code null}.
069         * @param registrationURI The client registration URI. Must not be
070         *                        {@code null}.
071         * @param accessToken     The client registration access token. Must
072         *                        not be {@code null}.
073         * @param metadata        The client metadata. Must not be 
074         *                        {@code null}.
075         * @param secret          The optional client secret, {@code null} if 
076         *                        not specified.
077         * @param issueDate       The issue date of the client identifier,
078         *                        {@code null} if not specified.
079         */
080        public OIDCClientInformation(final ClientID id,
081                                     final URI registrationURI,
082                                     final BearerAccessToken accessToken,
083                                     final ClientMetadata metadata,
084                                     final Secret secret,
085                                     final Date issueDate) {
086                
087                super(id, registrationURI, accessToken, metadata, secret, issueDate);
088        }
089
090
091        /**
092         * Gets the registered client metadata parameter names.
093         *
094         * @return The registered parameter names, as an unmodifiable set.
095         */
096        public static Set<String> getRegisteredParameterNames() {
097
098                return REGISTERED_PARAMETER_NAMES;
099        }
100        
101        
102        /**
103         * Gets the OpenID Connect client metadata.
104         * 
105         * @return The OpenID Connect client metadata.
106         */
107        public OIDCClientMetadata getOIDCClientMetadata() {
108                
109                return (OIDCClientMetadata)getClientMetadata();
110        }
111        
112        
113        /**
114         * Parses an OpenID Connect client information instance from the 
115         * specified JSON object.
116         *
117         * @param jsonObject The JSON object to parse. Must not be 
118         *                   {@code null}.
119         *
120         * @return The client information.
121         *
122         * @throws ParseException If the JSON object couldn't be parsed to an
123         *                        OpenID Connect client information instance.
124         */
125        public static OIDCClientInformation parse(final JSONObject jsonObject)
126                throws ParseException {
127
128                ClientID id = new ClientID(JSONObjectUtils.getString(jsonObject, "client_id"));
129                
130                
131                URI registrationURI = JSONObjectUtils.getURI(jsonObject, "registration_client_uri");
132                
133                
134                BearerAccessToken accessToken = new BearerAccessToken(
135                                JSONObjectUtils.getString(jsonObject, "registration_access_token"));
136
137                
138                OIDCClientMetadata metadata = OIDCClientMetadata.parse(jsonObject);
139                
140                
141                Secret secret = null;
142                
143                if (jsonObject.containsKey("client_secret")) {
144
145                        String value = JSONObjectUtils.getString(jsonObject, "client_secret");
146
147                        Date exp = null;
148
149                        if (jsonObject.containsKey("client_secret_expires_at"))
150                                exp = new Date(JSONObjectUtils.getLong(jsonObject, "client_secret_expires_at") * 1000);
151
152                        secret = new Secret(value, exp);
153                }
154                
155                
156                Date issueDate = null;
157                
158                if (jsonObject.containsKey("client_id_issued_at")) {
159                        
160                        issueDate = new Date(JSONObjectUtils.getLong(jsonObject, "client_id_issued_at") * 1000);
161                }
162
163                
164                return new OIDCClientInformation(id, registrationURI, accessToken, metadata, secret, issueDate);
165        }
166}