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