001    package com.nimbusds.openid.connect.sdk;
002    
003    
004    import java.net.URL;
005    
006    import net.minidev.json.JSONObject;
007    
008    import com.nimbusds.oauth2.sdk.ParseException;
009    import com.nimbusds.oauth2.sdk.http.CommonContentTypes;
010    import com.nimbusds.oauth2.sdk.http.HTTPRequest;
011    import com.nimbusds.oauth2.sdk.token.BearerAccessToken;
012    import com.nimbusds.oauth2.sdk.util.StringUtils;
013    import com.nimbusds.openid.connect.sdk.rp.Client;
014    
015    
016    /**
017     * OpenID Connect client add (register) request.
018     *
019     * <p>Example HTTP request:
020     *
021     * <pre>
022     * POST /connect/register HTTP/1.1
023     * Content-Type: application/json
024     * Host: server.example.com
025     * Authorization: Bearer eyJhbGciOiJSUzI1NiJ9.eyJ ...
026     *
027     * {
028     *  "application_type"                : "web",
029     *  "redirect_uris"                   : [ "https://client.example.org/callback", 
030     *                                        "https://client.example.org/callback2" ],
031     *  "client_name"                     : "My Example",
032     *  "client_name#ja-Jpan-JP"          : "クライアント名",
033     *  "logo_url"                        : "https://client.example.org/logo.png",
034     *  "subject_type"                    : "pairwise",
035     *  "sector_identifier_url"           : "https://othercompany.com/file_of_redirect_uris.json",
036     *  "token_endpoint_auth_method"      : "client_secret_basic",
037     *  "x509_url"                        : "https://client.example.org/certs.x509",
038     *  "jwk_url"                         : "https://client.example.org/my_rsa_public_key.jwk",
039     *  "userinfo_encrypted_response_alg" : "RSA1_5",
040     *  "userinfo_encrypted_response_enc" : "A128CBC+HS256"
041     * }
042     * </pre>
043     *
044     * <p>Related specifications:
045     *
046     * <ul>
047     *     <li>OpenID Connect Dynamic Client Registration 1.0, section 3.1.
048     * </ul>
049     *
050     * @author Vladimir Dzhuvinov
051     * @version $version$ (2013-05-10)
052     */
053    public class OIDCClientAddRequest extends OIDCClientRegistrationRequest {
054    
055    
056            /**
057             * The client details.
058             */
059            private final Client client;
060    
061    
062            /**
063             * Creates a new OpenID Connect client add (register) request.
064             *
065             * @param client The client details. Must not be {@code null} and must
066             *               specify one or more redirect URIs.
067             */
068            public OIDCClientAddRequest(final Client client) {
069    
070                    super();
071    
072                    if (client.getRedirectURIs() == null || client.getRedirectURIs().isEmpty())
073                            throw new IllegalArgumentException("The client details must specify one or more redirect URIs");
074    
075                    this.client = client;
076            }
077    
078    
079            /**
080             * Gets the associated client details.
081             *
082             * @return The client details.
083             */
084            public Client getClientDetails() {
085    
086                    return client;
087            }
088    
089    
090            @Override
091            public HTTPRequest toHTTPRequest(final URL url) {
092            
093                    HTTPRequest httpRequest = new HTTPRequest(HTTPRequest.Method.POST, url);
094    
095                    if (getAccessToken() != null)
096                            httpRequest.setAuthorization(getAccessToken().toAuthorizationHeader());
097    
098                    httpRequest.setContentType(CommonContentTypes.APPLICATION_JSON);
099    
100                    httpRequest.setQuery(client.toJSONObject().toString());
101    
102                    return httpRequest;
103            }
104    
105    
106            /**
107             * Parses an OpenID Connect client add (register) request from the
108             * specified HTTP POST request.
109             *
110             * @param httpRequest The HTTP request. Must not be {@code null}.
111             *
112             * @return The client add (register) request.
113             *
114             * @throws ParseException If the HTTP request couldn't be parsed to a 
115             *                        client register request.
116             */
117            public static OIDCClientAddRequest parse(final HTTPRequest httpRequest)
118                    throws ParseException {
119    
120                    httpRequest.ensureMethod(HTTPRequest.Method.POST);
121    
122                    JSONObject jsonObject = httpRequest.getQueryAsJSONObject();
123    
124                    Client client = Client.parse(jsonObject);
125    
126                    if (client.getRedirectURIs() == null ||
127                        client.getRedirectURIs().isEmpty())
128                            throw new ParseException("The client details must specify one or more redirect URIs");
129    
130                    OIDCClientAddRequest req = new OIDCClientAddRequest(client);
131    
132                    String authzHeaderValue = httpRequest.getAuthorization();
133    
134                    if (StringUtils.isDefined(authzHeaderValue))
135                            req.setAccessToken(BearerAccessToken.parse(authzHeaderValue));
136    
137                    return req;
138            }
139    }