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.oauth2.sdk.http.HTTPResponse;
023import com.nimbusds.oauth2.sdk.util.JSONObjectUtils;
024import net.jcip.annotations.Immutable;
025import net.minidev.json.JSONObject;
026
027import java.net.URI;
028import java.util.Objects;
029
030
031/**
032 * Pushed authorisation success response.
033 *
034 * <p>Example HTTP response:
035 *
036 * <pre>
037 * HTTP/1.1 201 Created
038 * Date: Tue, 2 May 2017 15:22:31 GMT
039 * Content-Type: application/json
040 *
041 * {
042 *   "request_uri" : "urn:example:bwc4JK-ESC0w8acc191e-Y1LTC2",
043 *   "expires_in"  : 3600
044 * }
045 * </pre>
046 *
047 * <p>Related specifications:
048 *
049 * <ul>
050 *     <li>OAuth 2.0 Pushed Authorization Requests (RFC 9126)
051 * </ul>
052 */
053@Immutable
054public class PushedAuthorizationSuccessResponse extends PushedAuthorizationResponse {
055        
056        
057        /**
058         * The request URI.
059         */
060        private final URI requestURI;
061        
062        
063        /**
064         * Lifetime, in seconds.
065         */
066        private final long lifetime;
067        
068        
069        /**
070         * Creates a new pushed authorisation success response.
071         *
072         * @param requestURI The request URI. Must not be {@code null}.
073         * @param lifetime   The request lifetime, in seconds. Must be a
074         *                   positive integer.
075         */
076        public PushedAuthorizationSuccessResponse(final URI requestURI, final long lifetime) {
077                this.requestURI = Objects.requireNonNull(requestURI);
078                if (lifetime <= 0) {
079                        throw new IllegalArgumentException("The request lifetime must be a positive integer");
080                }
081                this.lifetime = lifetime;
082        }
083        
084        
085        /**
086         * Returns the request URI.
087         *
088         * @return The request URI.
089         */
090        public URI getRequestURI() {
091                return requestURI;
092        }
093        
094        
095        /**
096         * Returns the request lifetime.
097         *
098         * @return The request lifetime, in seconds.
099         */
100        public long getLifetime() {
101                return lifetime;
102        }
103        
104        
105        @Override
106        public boolean indicatesSuccess() {
107                return true;
108        }
109        
110        
111        /**
112         * Returns a JSON object representation of this pushed authorisation
113         * success response.
114         *
115         * <p>Example JSON object:
116         *
117         * <pre>
118         * {
119         *   "request_uri": "urn:example:bwc4JK-ESC0w8acc191e-Y1LTC2",
120         *   "expires_in": 3600
121         * }
122         * </pre>
123         *
124         * @return The JSON object.
125         */
126        public JSONObject toJSONObject() {
127                
128                JSONObject o = new JSONObject();
129                o.put("request_uri", getRequestURI().toString());
130                o.put("expires_in", getLifetime());
131                return o;
132        }
133        
134        
135        @Override
136        public HTTPResponse toHTTPResponse() {
137                
138                HTTPResponse httpResponse = new HTTPResponse(HTTPResponse.SC_CREATED);
139                httpResponse.setEntityContentType(ContentType.APPLICATION_JSON);
140                httpResponse.setBody(toJSONObject().toString());
141                return httpResponse;
142        }
143        
144        
145        /**
146         * Parses pushed authorisation success response from the specified JSON
147         * object.
148         *
149         * @param jsonObject The JSON object to parse. Must not be
150         *                   {@code null}.
151         *
152         * @return The pushed authorisation success response.
153         *
154         * @throws ParseException If the JSON object couldn't be parsed to a
155         *                        pushed authorisation success response.
156         */
157        public static PushedAuthorizationSuccessResponse parse(final JSONObject jsonObject)
158                throws ParseException {
159                
160                URI requestURI = JSONObjectUtils.getURI(jsonObject, "request_uri");
161                long lifetime = JSONObjectUtils.getLong(jsonObject, "expires_in");
162                return new PushedAuthorizationSuccessResponse(requestURI, lifetime);
163        }
164        
165        
166        /**
167         * Parses a pushed authorisation success response from the specified
168         * HTTP response.
169         *
170         * @param httpResponse The HTTP response. Must not be {@code null}.
171         *
172         * @return The pushed authorisation success response.
173         *
174         * @throws ParseException If the HTTP response couldn't be parsed to a
175         *                        pushed authorisation success response.
176         */
177        public static PushedAuthorizationSuccessResponse parse(final HTTPResponse httpResponse)
178                throws ParseException {
179                
180                httpResponse.ensureStatusCode(HTTPResponse.SC_CREATED, HTTPResponse.SC_OK);
181                JSONObject jsonObject = httpResponse.getBodyAsJSONObject();
182                return parse(jsonObject);
183        }
184}