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