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 051 * (draft-lodderstedt-oauth-par-01) 052 * </ul> 053 */ 054@Immutable 055public class PushedAuthorizationSuccessResponse extends PushedAuthorizationResponse { 056 057 058 /** 059 * The request URI. 060 */ 061 private final URI requestURI; 062 063 064 /** 065 * Lifetime, in seconds. 066 */ 067 private final long lifetime; 068 069 070 /** 071 * Creates a new pushed authorisation success response. 072 * 073 * @param requestURI The request URI. Must not be {@code null}. 074 * @param lifetime The request lifetime, in seconds. Must be a 075 * positive integer. 076 */ 077 public PushedAuthorizationSuccessResponse(final URI requestURI, final long lifetime) { 078 if (requestURI == null) { 079 throw new IllegalArgumentException("The request URI must not be null"); 080 } 081 this.requestURI = requestURI; 082 if (lifetime <= 0) { 083 throw new IllegalArgumentException("The request lifetime must be a positive integer"); 084 } 085 this.lifetime = lifetime; 086 } 087 088 089 /** 090 * Returns the request URI. 091 * 092 * @return The request URI. 093 */ 094 public URI getRequestURI() { 095 return requestURI; 096 } 097 098 099 /** 100 * Returns the request lifetime. 101 * 102 * @return The request lifetime, in seconds. 103 */ 104 public long getLifetime() { 105 return lifetime; 106 } 107 108 109 @Override 110 public boolean indicatesSuccess() { 111 return true; 112 } 113 114 115 /** 116 * Returns a JSON object representation of this pushed authorisation 117 * success response. 118 * 119 * <p>Example JSON object: 120 * 121 * <pre> 122 * { 123 * "request_uri": "urn:example:bwc4JK-ESC0w8acc191e-Y1LTC2", 124 * "expires_in": 3600 125 * } 126 * </pre> 127 * 128 * @return The JSON object. 129 */ 130 public JSONObject toJSONObject() { 131 132 JSONObject o = new JSONObject(); 133 o.put("request_uri", getRequestURI().toString()); 134 o.put("expires_in", getLifetime()); 135 return o; 136 } 137 138 139 @Override 140 public HTTPResponse toHTTPResponse() { 141 142 HTTPResponse httpResponse = new HTTPResponse(HTTPResponse.SC_CREATED); 143 httpResponse.setEntityContentType(ContentType.APPLICATION_JSON); 144 httpResponse.setContent(toJSONObject().toString()); 145 return httpResponse; 146 } 147 148 149 /** 150 * Parses pushed authorisation success response from the specified JSON 151 * object. 152 * 153 * @param jsonObject The JSON object to parse. Must not be 154 * {@code null}. 155 * 156 * @return The pushed authorisation success response. 157 * 158 * @throws ParseException If the JSON object couldn't be parsed to a 159 * pushed authorisation success response. 160 */ 161 public static PushedAuthorizationSuccessResponse parse(final JSONObject jsonObject) 162 throws ParseException { 163 164 URI requestURI = JSONObjectUtils.getURI(jsonObject, "request_uri"); 165 long lifetime = JSONObjectUtils.getLong(jsonObject, "expires_in"); 166 return new PushedAuthorizationSuccessResponse(requestURI, lifetime); 167 } 168 169 170 /** 171 * Parses a pushed authorisation success response from the specified 172 * HTTP response. 173 * 174 * @param httpResponse The HTTP response. Must not be {@code null}. 175 * 176 * @return The pushed authorisation success response. 177 * 178 * @throws ParseException If the HTTP response couldn't be parsed to a 179 * pushed authorisation success response. 180 */ 181 public static PushedAuthorizationSuccessResponse parse(final HTTPResponse httpResponse) 182 throws ParseException { 183 184 httpResponse.ensureStatusCode(HTTPResponse.SC_CREATED, HTTPResponse.SC_OK); 185 JSONObject jsonObject = httpResponse.getContentAsJSONObject(); 186 return parse(jsonObject); 187 } 188}