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 java.net.URI;
022import java.util.Date;
023
024import net.jcip.annotations.Immutable;
025import net.minidev.json.JSONObject;
026
027import com.nimbusds.common.contenttype.ContentType;
028import com.nimbusds.jwt.util.DateUtils;
029import com.nimbusds.oauth2.sdk.http.HTTPResponse;
030import com.nimbusds.oauth2.sdk.id.Audience;
031import com.nimbusds.oauth2.sdk.id.Issuer;
032import com.nimbusds.oauth2.sdk.util.JSONObjectUtils;
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 *         section 7.
058 *     <li>The OAuth 2.0 Authorization Framework: JWT Secured Authorization
059 *         Request (JAR) (draft-ietf-oauth-jwsreq-17).
060 * </ul>
061 */
062@Deprecated
063@Immutable
064public final class RequestObjectPOSTSuccessResponse extends RequestObjectPOSTResponse implements SuccessResponse {
065        
066        
067        /**
068         * The issuer.
069         */
070        private final Issuer iss;
071        
072        
073        /**
074         * The audience (client ID).
075         */
076        private final Audience aud;
077        
078        
079        /**
080         * The request URI.
081         */
082        private final URI requestURI;
083        
084        
085        /**
086         * The request URI expiration time.
087         */
088        private final Date exp;
089        
090        
091        /**
092         * Creates a new request object POST success response.
093         *
094         * @param iss        The issuer. Must not be {@code null}.
095         * @param aud        The audience (the intended client IDMust not be
096         *                   {@code null}.).
097         * @param requestURI The request URI. Must not be {@code null}.
098         * @param exp        The request URI expiration time. Must not be
099         *                   {@code null}.
100         */
101        public RequestObjectPOSTSuccessResponse(final Issuer iss,
102                                                final Audience aud,
103                                                final URI requestURI,
104                                                final Date exp) {
105                if (iss == null) {
106                        throw new IllegalArgumentException("The issuer must not be null");
107                }
108                this.iss = iss;
109                
110                if (aud == null) {
111                        throw new IllegalArgumentException("The audience must not be null");
112                }
113                this.aud = aud;
114                
115                if (requestURI == null) {
116                        throw new IllegalArgumentException("The request URI must not be null");
117                }
118                this.requestURI = requestURI;
119                
120                if (exp == null) {
121                        throw new IllegalArgumentException("The request URI expiration time must not be null");
122                }
123                this.exp = exp;
124        }
125        
126        
127        /**
128         * Returns the issuer.
129         *
130         * @return The issuer.
131         */
132        public Issuer getIssuer() {
133                return iss;
134        }
135        
136        
137        /**
138         * Returns the audience (the intended client ID).
139         *
140         * @return The audience.
141         */
142        public Audience getAudience() {
143                return aud;
144        }
145        
146        
147        /**
148         * Returns the request URI.
149         *
150         * @return The request URI.
151         */
152        public URI getRequestURI() {
153                return requestURI;
154        }
155        
156        
157        /**
158         * Returns the expiration time.
159         *
160         * @return The expiration time.
161         */
162        public Date getExpirationTime() {
163                return exp;
164        }
165        
166        
167        @Override
168        public boolean indicatesSuccess() {
169                return true;
170        }
171        
172        
173        /**
174         * Returns a JSON object representation of this request object POST
175         * success response.
176         *
177         * @return The JSON object.
178         */
179        public JSONObject toJSONObject() {
180                
181                JSONObject jsonObject = new JSONObject();
182                
183                jsonObject.put("iss", iss.getValue());
184                jsonObject.put("aud", aud.getValue());
185                jsonObject.put("request_uri", requestURI.toString());
186                jsonObject.put("exp", DateUtils.toSecondsSinceEpoch(exp));
187                
188                return jsonObject;
189        }
190        
191        
192        @Override
193        public HTTPResponse toHTTPResponse() {
194                
195                HTTPResponse httpResponse = new HTTPResponse(HTTPResponse.SC_CREATED);
196                httpResponse.setEntityContentType(ContentType.APPLICATION_JSON);
197                httpResponse.setContent(toJSONObject().toJSONString());
198                return httpResponse;
199        }
200        
201        
202        /**
203         * Parses a request object POST success response from the specified
204         * JSON object.
205         *
206         * @param jsonObject The JSON object to parse. Must not be {@code null}.
207         *
208         * @return The request object POST success response.
209         *
210         * @throws ParseException If the JSON object couldn't be parsed to a
211         *                        request object POST success response.
212         */
213        public static RequestObjectPOSTSuccessResponse parse(final JSONObject jsonObject)
214                throws ParseException {
215                
216                return new RequestObjectPOSTSuccessResponse(
217                        new Issuer(JSONObjectUtils.getString(jsonObject, "iss")),
218                        new Audience(JSONObjectUtils.getString(jsonObject, "aud")),
219                        JSONObjectUtils.getURI(jsonObject, "request_uri"),
220                        DateUtils.fromSecondsSinceEpoch(JSONObjectUtils.getLong(jsonObject, "exp")));
221        }
222        
223        
224        /**
225         * Parses a request object POST success response from the specified
226         * HTTP response.
227         *
228         * @param httpResponse The HTTP response. Must not be {@code null}.
229         *
230         * @return The request object POST success response.
231         *
232         * @throws ParseException If the HTTP response couldn't be parsed to a
233         *                        request object POST success response.
234         */
235        public static RequestObjectPOSTSuccessResponse parse(final HTTPResponse httpResponse)
236                throws ParseException {
237                
238                httpResponse.ensureStatusCode(HTTPResponse.SC_CREATED, HTTPResponse.SC_OK);
239                return parse(httpResponse.getContentAsJSONObject());
240        }
241}