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