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.oauth2.sdk.client;
019
020
021import com.nimbusds.common.contenttype.ContentType;
022import com.nimbusds.oauth2.sdk.ParseException;
023import com.nimbusds.oauth2.sdk.SuccessResponse;
024import com.nimbusds.oauth2.sdk.http.HTTPResponse;
025import net.jcip.annotations.Immutable;
026
027import java.util.Objects;
028
029
030/**
031 * Client information response.
032 *
033 * <p>Example HTTP response:
034 *
035 * <pre>
036 * HTTP/1.1 200 OK
037 * Content-Type: application/json
038 * Cache-Control: no-store
039 * Pragma: no-cache
040 *
041 * {
042 *  "registration_access_token"  : "reg-23410913-abewfq.123483",
043 *  "registration_client_uri"    : "https://server.example.com/register/s6BhdRkqt3",
044 *  "client_id"                  : "s6BhdRkqt3",
045 *  "client_secret"              : "cf136dc3c1fc93f31185e5885805d",
046 *  "client_id_issued_at"        : 2893256800
047 *  "client_secret_expires_at"   : 2893276800
048 *  "client_name"                : "My Example Client",
049 *  "client_name#ja-Jpan-JP"     : "\u30AF\u30E9\u30A4\u30A2\u30F3\u30C8\u540D",
050 *  "redirect_uris"              : [ "https://client.example.org/callback",
051 *                                   "https://client.example.org/callback2" ]
052 *  "scope"                      : "read write dolphin",
053 *  "grant_types"                : [ "authorization_code", "refresh_token" ]
054 *  "token_endpoint_auth_method" : "client_secret_basic",
055 *  "logo_uri"                   : "https://client.example.org/logo.png",
056 *  "jwks_uri"                   : "https://client.example.org/my_public_keys.jwks"
057 * }
058 * </pre>
059 *
060 * <p>Related specifications:
061 *
062 * <ul>
063 *     <li>OAuth 2.0 Dynamic Client Registration Management Protocol (RFC 7592)
064 *     <li>OAuth 2.0 Dynamic Client Registration Protocol (RFC 7591)
065 * </ul>
066 */
067@Immutable
068public class ClientInformationResponse 
069        extends ClientRegistrationResponse
070        implements SuccessResponse {
071
072
073        /**
074         * The client information.
075         */
076        private final ClientInformation clientInfo;
077        
078        
079        /**
080         * {@code true} for a newly registered client.
081         */
082        private final boolean forNewClient;
083
084
085        /**
086         * Creates a new client information response.
087         *
088         * @param clientInfo   The client information. Must not be
089         *                     {@code null}.
090         * @param forNewClient {@code true} for a newly registered client,
091         *                     {@code false} for a retrieved or updated client.
092         */
093        public ClientInformationResponse(final ClientInformation clientInfo,
094                                         final boolean forNewClient) {
095
096                this.clientInfo = Objects.requireNonNull(clientInfo);
097                this.forNewClient = forNewClient;
098        }
099
100
101        @Override
102        public boolean indicatesSuccess() {
103
104                return true;
105        }
106
107
108        /**
109         * Gets the client information.
110         *
111         * @return The client information.
112         */
113        public ClientInformation getClientInformation() {
114
115                return clientInfo;
116        }
117        
118        
119        /**
120         * Checks if the client information response is for a new client.
121         *
122         * @return {@code true} for a newly registered client, {@code false}
123         *         for a retrieved or updated client.
124         */
125        public boolean isForNewClient() {
126                
127                return forNewClient;
128        }
129        
130        
131        @Override
132        public HTTPResponse toHTTPResponse() {
133        
134                // 201 for POST, 200 for GET or PUT
135                HTTPResponse httpResponse = new HTTPResponse(forNewClient ? HTTPResponse.SC_CREATED : HTTPResponse.SC_OK);
136                httpResponse.setEntityContentType(ContentType.APPLICATION_JSON);
137                httpResponse.setCacheControl("no-store");
138                httpResponse.setPragma("no-cache");
139                httpResponse.setBody(clientInfo.toJSONObject().toString());
140                return httpResponse;
141        }
142
143
144        /**
145         * Parses a client information response from the specified 
146         * HTTP response.
147         *
148         * @param httpResponse The HTTP response. Must not be {@code null}.
149         *
150         * @return The client information response.
151         *
152         * @throws ParseException If the HTTP response couldn't be parsed to a
153         *                        client information response.
154         */
155        public static ClientInformationResponse parse(final HTTPResponse httpResponse)
156                throws ParseException {
157
158                httpResponse.ensureStatusCode(HTTPResponse.SC_OK, HTTPResponse.SC_CREATED);
159                ClientInformation clientInfo = ClientInformation.parse(httpResponse.getBodyAsJSONObject());
160                boolean forNewClient = HTTPResponse.SC_CREATED == httpResponse.getStatusCode();
161                return new ClientInformationResponse(clientInfo, forNewClient);
162        }
163}