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.jwt.util.DateUtils; 028import com.nimbusds.oauth2.sdk.http.CommonContentTypes; 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@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 IDMust 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 if (iss == null) { 105 throw new IllegalArgumentException("The issuer must not be null"); 106 } 107 this.iss = iss; 108 109 if (aud == null) { 110 throw new IllegalArgumentException("The audience must not be null"); 111 } 112 this.aud = aud; 113 114 if (requestURI == null) { 115 throw new IllegalArgumentException("The request URI must not be null"); 116 } 117 this.requestURI = requestURI; 118 119 if (exp == null) { 120 throw new IllegalArgumentException("The request URI expiration time must not be null"); 121 } 122 this.exp = exp; 123 } 124 125 126 /** 127 * Returns the issuer. 128 * 129 * @return The issuer. 130 */ 131 public Issuer getIssuer() { 132 return iss; 133 } 134 135 136 /** 137 * Returns the audience (the intended client ID). 138 * 139 * @return The audience. 140 */ 141 public Audience getAudience() { 142 return aud; 143 } 144 145 146 /** 147 * Returns the request URI. 148 * 149 * @return The request URI. 150 */ 151 public URI getRequestURI() { 152 return requestURI; 153 } 154 155 156 /** 157 * Returns the expiration time. 158 * 159 * @return The expiration time. 160 */ 161 public Date getExpirationTime() { 162 return exp; 163 } 164 165 166 @Override 167 public boolean indicatesSuccess() { 168 return true; 169 } 170 171 172 /** 173 * Returns a JSON object representation of this request object POST 174 * success response. 175 * 176 * @return The JSON object. 177 */ 178 public JSONObject toJSONObject() { 179 180 JSONObject jsonObject = new JSONObject(); 181 182 jsonObject.put("iss", iss.getValue()); 183 jsonObject.put("aud", aud.getValue()); 184 jsonObject.put("request_uri", requestURI.toString()); 185 jsonObject.put("exp", DateUtils.toSecondsSinceEpoch(exp)); 186 187 return jsonObject; 188 } 189 190 191 @Override 192 public HTTPResponse toHTTPResponse() { 193 194 HTTPResponse httpResponse = new HTTPResponse(HTTPResponse.SC_CREATED); 195 httpResponse.setContentType(CommonContentTypes.APPLICATION_JSON); 196 httpResponse.setContent(toJSONObject().toJSONString()); 197 return httpResponse; 198 } 199 200 201 /** 202 * Parses a request object POST success response from the specified 203 * JSON object. 204 * 205 * @param jsonObject The JSON object to parse. Must not be {@code null}. 206 * 207 * @return The request object POST success response. 208 * 209 * @throws ParseException If the JSON object couldn't be parsed to a 210 * request object POST success response. 211 */ 212 public static RequestObjectPOSTSuccessResponse parse(final JSONObject jsonObject) 213 throws ParseException { 214 215 return new RequestObjectPOSTSuccessResponse( 216 new Issuer(JSONObjectUtils.getString(jsonObject, "iss")), 217 new Audience(JSONObjectUtils.getString(jsonObject, "aud")), 218 JSONObjectUtils.getURI(jsonObject, "request_uri"), 219 DateUtils.fromSecondsSinceEpoch(JSONObjectUtils.getLong(jsonObject, "exp"))); 220 } 221 222 223 /** 224 * Parses a request object POST success response from the specified 225 * HTTP response. 226 * 227 * @param httpResponse The HTTP response. Must not be {@code null}. 228 * 229 * @return The request object POST success response. 230 * 231 * @throws ParseException If the HTTP response couldn't be parsed to a 232 * request object POST success response. 233 */ 234 public static RequestObjectPOSTSuccessResponse parse(final HTTPResponse httpResponse) 235 throws ParseException { 236 237 httpResponse.ensureStatusCode(HTTPResponse.SC_CREATED, HTTPResponse.SC_OK); 238 return parse(httpResponse.getContentAsJSONObject()); 239 } 240}