001package com.nimbusds.openid.connect.sdk.rp;
002
003
004import java.net.URI;
005import java.net.URISyntaxException;
006
007import net.jcip.annotations.Immutable;
008
009import net.minidev.json.JSONObject;
010
011import com.nimbusds.oauth2.sdk.ParseException;
012import com.nimbusds.oauth2.sdk.auth.Secret;
013import com.nimbusds.oauth2.sdk.client.ClientUpdateRequest;
014import com.nimbusds.oauth2.sdk.http.HTTPRequest;
015import com.nimbusds.oauth2.sdk.id.ClientID;
016import com.nimbusds.oauth2.sdk.token.BearerAccessToken;
017import com.nimbusds.oauth2.sdk.util.JSONObjectUtils;
018
019
020/**
021 * OpenID Connect client registration request.
022 * 
023 * <p>Note that the update operation is not specified in OpenID Connect Dynamic
024 * Client Registration.
025 * 
026 * <p>Example HTTP request:
027 *
028 * <pre>
029 * PUT /register/s6BhdRkqt3 HTTP/1.1
030 * Accept: application/json
031 * Host: server.example.com
032 * Authorization: Bearer reg-23410913-abewfq.123483
033 *
034 * {
035 *  "client_id"                  :"s6BhdRkqt3",
036 *  "client_secret"              : "cf136dc3c1fc93f31185e5885805d",
037 *  "redirect_uris"              : ["https://client.example.org/callback", "https://client.example.org/alt"],
038 *  "scope"                      : "read write dolphin",
039 *  "grant_types"                : ["authorization_code", "refresh_token"]
040 *  "token_endpoint_auth_method" : "client_secret_basic",
041 *  "jwks_uri"                   : "https://client.example.org/my_public_keys.jwks"
042 *  "client_name"                : "My New Example",
043 *  "client_name#fr"             : "Mon Nouvel Exemple",
044 *  "logo_uri"                   : "https://client.example.org/newlogo.png"
045 *  "logo_uri#fr"                : "https://client.example.org/fr/newlogo.png"
046 * }
047 *
048 * </pre>
049 *
050 * <p>Related specifications:
051 *
052 * <ul>
053 *     <li>OAuth 2.0 Dynamic Client Registration Protocol 
054 *         (draft-ietf-oauth-dyn-reg-14), section 4.3.
055 * </ul>
056 */
057@Immutable
058public class OIDCClientUpdateRequest extends ClientUpdateRequest {
059        
060        
061        /**
062         * Creates a new OpenID Connect client update request.
063         *
064         * @param uri         The URI of the client update endpoint. May be
065         *                    {@code null} if the {@link #toHTTPRequest()}
066         *                    method will not be used.
067         * @param accessToken The client registration access token. Must not be
068         *                    {@code null}.
069         * @param metadata    The client metadata. Must not be {@code null} and 
070         *                    must specify one or more redirection URIs.
071         * @param secret      The optional client secret, {@code null} if not
072         *                    specified.
073         */
074        public OIDCClientUpdateRequest(final URI uri,
075                                       final ClientID id,
076                                       final BearerAccessToken accessToken,
077                                       final OIDCClientMetadata metadata,
078                                       final Secret secret) {
079                
080                super(uri, id, accessToken, metadata, secret);
081        }
082        
083        
084        /**
085         * Gets the associated OpenID Connect client metadata.
086         *
087         * @return The OpenID Connect client metadata.
088         */
089        public OIDCClientMetadata getOIDCClientMetadata() {
090                
091                return (OIDCClientMetadata)getClientMetadata();
092        }
093        
094        
095        /**
096         * Parses an OpenID Connect client update request from the specified 
097         * HTTP PUT request.
098         *
099         * @param httpRequest The HTTP request. Must not be {@code null}.
100         *
101         * @return The OpenID Connect client update request.
102         *
103         * @throws ParseException If the HTTP request couldn't be parsed to an
104         *                        OpenID Connect client update request.
105         */
106        public static OIDCClientUpdateRequest parse(final HTTPRequest httpRequest)
107                throws ParseException {
108
109                httpRequest.ensureMethod(HTTPRequest.Method.PUT);
110                
111                BearerAccessToken accessToken = BearerAccessToken.parse(httpRequest.getAuthorization());
112                
113                JSONObject jsonObject = httpRequest.getQueryAsJSONObject();
114                
115                ClientID id = new ClientID(JSONObjectUtils.getString(jsonObject, "client_id"));
116
117                OIDCClientMetadata metadata = OIDCClientMetadata.parse(jsonObject);
118                
119                Secret clientSecret = null;
120                
121                if (jsonObject.get("client_secret") != null)
122                        clientSecret = new Secret(JSONObjectUtils.getString(jsonObject, "client_secret"));
123
124
125                URI endpointURI;
126
127                try {
128                        endpointURI = httpRequest.getURL().toURI();
129
130                } catch (URISyntaxException e) {
131
132                        throw new ParseException(e.getMessage(), e);
133                }
134                
135                return new OIDCClientUpdateRequest(endpointURI, id, accessToken, metadata, clientSecret);
136        }
137}