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; 022 023import net.jcip.annotations.Immutable; 024import net.minidev.json.JSONObject; 025 026import com.nimbusds.common.contenttype.ContentType; 027import com.nimbusds.oauth2.sdk.http.HTTPResponse; 028import com.nimbusds.oauth2.sdk.util.JSONObjectUtils; 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 if (requestURI == null) { 078 throw new IllegalArgumentException("The request URI must not be null"); 079 } 080 this.requestURI = requestURI; 081 if (lifetime <= 0) { 082 throw new IllegalArgumentException("The request lifetime must be a positive integer"); 083 } 084 this.lifetime = lifetime; 085 } 086 087 088 /** 089 * Returns the request URI. 090 * 091 * @return The request URI. 092 */ 093 public URI getRequestURI() { 094 return requestURI; 095 } 096 097 098 /** 099 * Returns the request lifetime. 100 * 101 * @return The request lifetime, in seconds. 102 */ 103 public long getLifetime() { 104 return lifetime; 105 } 106 107 108 @Override 109 public boolean indicatesSuccess() { 110 return true; 111 } 112 113 114 /** 115 * Returns a JSON object representation of this pushed authorisation 116 * success response. 117 * 118 * <p>Example JSON object: 119 * 120 * <pre> 121 * { 122 * "request_uri": "urn:example:bwc4JK-ESC0w8acc191e-Y1LTC2", 123 * "expires_in": 3600 124 * } 125 * </pre> 126 * 127 * @return The JSON object. 128 */ 129 public JSONObject toJSONObject() { 130 131 JSONObject o = new JSONObject(); 132 o.put("request_uri", getRequestURI().toString()); 133 o.put("expires_in", getLifetime()); 134 return o; 135 } 136 137 138 @Override 139 public HTTPResponse toHTTPResponse() { 140 141 HTTPResponse httpResponse = new HTTPResponse(HTTPResponse.SC_CREATED); 142 httpResponse.setEntityContentType(ContentType.APPLICATION_JSON); 143 httpResponse.setContent(toJSONObject().toString()); 144 return httpResponse; 145 } 146 147 148 /** 149 * Parses pushed authorisation success response from the specified JSON 150 * object. 151 * 152 * @param jsonObject The JSON object to parse. Must not be 153 * {@code null}. 154 * 155 * @return The pushed authorisation success response. 156 * 157 * @throws ParseException If the JSON object couldn't be parsed to a 158 * pushed authorisation success response. 159 */ 160 public static PushedAuthorizationSuccessResponse parse(final JSONObject jsonObject) 161 throws ParseException { 162 163 URI requestURI = JSONObjectUtils.getURI(jsonObject, "request_uri"); 164 long lifetime = JSONObjectUtils.getLong(jsonObject, "expires_in"); 165 return new PushedAuthorizationSuccessResponse(requestURI, lifetime); 166 } 167 168 169 /** 170 * Parses a pushed authorisation success response from the specified 171 * HTTP response. 172 * 173 * @param httpResponse The HTTP response. Must not be {@code null}. 174 * 175 * @return The pushed authorisation success response. 176 * 177 * @throws ParseException If the HTTP response couldn't be parsed to a 178 * pushed authorisation success response. 179 */ 180 public static PushedAuthorizationSuccessResponse parse(final HTTPResponse httpResponse) 181 throws ParseException { 182 183 httpResponse.ensureStatusCode(HTTPResponse.SC_CREATED, HTTPResponse.SC_OK); 184 JSONObject jsonObject = httpResponse.getContentAsJSONObject(); 185 return parse(jsonObject); 186 } 187}