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