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 accessToken The client registration access token. Must not be
087         *                    {@code null}.
088         * @param metadata    The client metadata. Must not be {@code null} and 
089         *                    must specify one or more redirection URIs.
090         * @param secret      The optional client secret, {@code null} if not
091         *                    specified.
092         */
093        public OIDCClientUpdateRequest(final URI uri,
094                                       final ClientID id,
095                                       final BearerAccessToken accessToken,
096                                       final OIDCClientMetadata metadata,
097                                       final Secret secret) {
098                
099                super(uri, id, accessToken, metadata, secret);
100        }
101        
102        
103        /**
104         * Gets the associated OpenID Connect client metadata.
105         *
106         * @return The OpenID Connect client metadata.
107         */
108        public OIDCClientMetadata getOIDCClientMetadata() {
109                
110                return (OIDCClientMetadata)getClientMetadata();
111        }
112        
113        
114        /**
115         * Parses an OpenID Connect client update request from the specified 
116         * HTTP PUT request.
117         *
118         * @param httpRequest The HTTP request. Must not be {@code null}.
119         *
120         * @return The OpenID Connect client update request.
121         *
122         * @throws ParseException If the HTTP request couldn't be parsed to an
123         *                        OpenID Connect client update request.
124         */
125        public static OIDCClientUpdateRequest parse(final HTTPRequest httpRequest)
126                throws ParseException {
127
128                httpRequest.ensureMethod(HTTPRequest.Method.PUT);
129                
130                BearerAccessToken accessToken = BearerAccessToken.parse(httpRequest.getAuthorization());
131                
132                JSONObject jsonObject = httpRequest.getQueryAsJSONObject();
133                
134                ClientID id = new ClientID(JSONObjectUtils.getString(jsonObject, "client_id"));
135
136                OIDCClientMetadata metadata = OIDCClientMetadata.parse(jsonObject);
137                
138                Secret clientSecret = null;
139                
140                if (jsonObject.get("client_secret") != null)
141                        clientSecret = new Secret(JSONObjectUtils.getString(jsonObject, "client_secret"));
142
143
144                URI endpointURI;
145
146                try {
147                        endpointURI = httpRequest.getURL().toURI();
148
149                } catch (URISyntaxException e) {
150
151                        throw new ParseException(e.getMessage(), e);
152                }
153                
154                return new OIDCClientUpdateRequest(endpointURI, id, accessToken, metadata, clientSecret);
155        }
156}