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;
019
020
021import com.nimbusds.common.contenttype.ContentType;
022import com.nimbusds.jwt.util.DateUtils;
023import com.nimbusds.oauth2.sdk.http.HTTPResponse;
024import com.nimbusds.oauth2.sdk.id.Audience;
025import com.nimbusds.oauth2.sdk.id.Issuer;
026import com.nimbusds.oauth2.sdk.util.JSONObjectUtils;
027import net.jcip.annotations.Immutable;
028import net.minidev.json.JSONObject;
029
030import java.net.URI;
031import java.util.Date;
032import java.util.Objects;
033
034
035/**
036 * Request object POST success response.
037 *
038 * <p>Example request object POST success response:
039 *
040 * <pre>
041 * HTTP/1.1 201 Created
042 * Date: Tue, 2 May 2017 15:22:31 GMT
043 * Content-Type: application/json
044 *
045 * {
046 *   "iss"         : "https://c2id.com",
047 *   "aud"         : "s6bhdrkqt3",
048 *   "request_uri" : "urn:requests:aashoo1Ooj6ahc5C",
049 *   "exp"         : 1493738581
050 * }
051 * </pre>
052 *
053 * <p>Related specifications:
054 *
055 * <ul>
056 *     <li>Financial-grade API - Part 2: Read and Write API Security Profile
057 *     <li>The OAuth 2.0 Authorization Framework: JWT Secured Authorization
058 *         Request (JAR) (RFC 9101)
059 * </ul>
060 */
061@Deprecated
062@Immutable
063public final class RequestObjectPOSTSuccessResponse extends RequestObjectPOSTResponse implements SuccessResponse {
064        
065        
066        /**
067         * The issuer.
068         */
069        private final Issuer iss;
070        
071        
072        /**
073         * The audience (client ID).
074         */
075        private final Audience aud;
076        
077        
078        /**
079         * The request URI.
080         */
081        private final URI requestURI;
082        
083        
084        /**
085         * The request URI expiration time.
086         */
087        private final Date exp;
088        
089        
090        /**
091         * Creates a new request object POST success response.
092         *
093         * @param iss        The issuer. Must not be {@code null}.
094         * @param aud        The audience (the intended client ID). Must not be
095         *                   {@code null}.
096         * @param requestURI The request URI. Must not be {@code null}.
097         * @param exp        The request URI expiration time. Must not be
098         *                   {@code null}.
099         */
100        public RequestObjectPOSTSuccessResponse(final Issuer iss,
101                                                final Audience aud,
102                                                final URI requestURI,
103                                                final Date exp) {
104                this.iss = Objects.requireNonNull(iss);
105                this.aud = Objects.requireNonNull(aud);
106                this.requestURI = Objects.requireNonNull(requestURI);
107                this.exp = Objects.requireNonNull(exp);
108        }
109        
110        
111        /**
112         * Returns the issuer.
113         *
114         * @return The issuer.
115         */
116        public Issuer getIssuer() {
117                return iss;
118        }
119        
120        
121        /**
122         * Returns the audience (the intended client ID).
123         *
124         * @return The audience.
125         */
126        public Audience getAudience() {
127                return aud;
128        }
129        
130        
131        /**
132         * Returns the request URI.
133         *
134         * @return The request URI.
135         */
136        public URI getRequestURI() {
137                return requestURI;
138        }
139        
140        
141        /**
142         * Returns the expiration time.
143         *
144         * @return The expiration time.
145         */
146        public Date getExpirationTime() {
147                return exp;
148        }
149        
150        
151        @Override
152        public boolean indicatesSuccess() {
153                return true;
154        }
155        
156        
157        /**
158         * Returns a JSON object representation of this request object POST
159         * success response.
160         *
161         * @return The JSON object.
162         */
163        public JSONObject toJSONObject() {
164                
165                JSONObject jsonObject = new JSONObject();
166                
167                jsonObject.put("iss", iss.getValue());
168                jsonObject.put("aud", aud.getValue());
169                jsonObject.put("request_uri", requestURI.toString());
170                jsonObject.put("exp", DateUtils.toSecondsSinceEpoch(exp));
171                
172                return jsonObject;
173        }
174        
175        
176        @Override
177        public HTTPResponse toHTTPResponse() {
178                
179                HTTPResponse httpResponse = new HTTPResponse(HTTPResponse.SC_CREATED);
180                httpResponse.setEntityContentType(ContentType.APPLICATION_JSON);
181                httpResponse.setContent(toJSONObject().toJSONString());
182                return httpResponse;
183        }
184        
185        
186        /**
187         * Parses a request object POST success response from the specified
188         * JSON object.
189         *
190         * @param jsonObject The JSON object to parse. Must not be {@code null}.
191         *
192         * @return The request object POST success response.
193         *
194         * @throws ParseException If the JSON object couldn't be parsed to a
195         *                        request object POST success response.
196         */
197        public static RequestObjectPOSTSuccessResponse parse(final JSONObject jsonObject)
198                throws ParseException {
199                
200                return new RequestObjectPOSTSuccessResponse(
201                        new Issuer(JSONObjectUtils.getNonBlankString(jsonObject, "iss")),
202                        new Audience(JSONObjectUtils.getNonBlankString(jsonObject, "aud")),
203                        JSONObjectUtils.getURI(jsonObject, "request_uri"),
204                        DateUtils.fromSecondsSinceEpoch(JSONObjectUtils.getLong(jsonObject, "exp")));
205        }
206        
207        
208        /**
209         * Parses a request object POST success response from the specified
210         * HTTP response.
211         *
212         * @param httpResponse The HTTP response. Must not be {@code null}.
213         *
214         * @return The request object POST success response.
215         *
216         * @throws ParseException If the HTTP response couldn't be parsed to a
217         *                        request object POST success response.
218         */
219        public static RequestObjectPOSTSuccessResponse parse(final HTTPResponse httpResponse)
220                throws ParseException {
221                
222                httpResponse.ensureStatusCode(HTTPResponse.SC_CREATED, HTTPResponse.SC_OK);
223                return parse(httpResponse.getContentAsJSONObject());
224        }
225}