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