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 net.jcip.annotations.Immutable; 024 025import java.util.Objects; 026 027 028/** 029 * Pushed authorisation error response. 030 * 031 * <p>Example HTTP response: 032 * 033 * <pre> 034 * HTTP/1.1 400 Bad Request 035 * Content-Type: application/json 036 * Cache-Control: no-cache, no-store 037 * 038 * { 039 * "error ": "invalid_request", 040 * "error_description" : "The redirect_uri is not valid for the given client" 041 * } 042 * </pre> 043 * 044 * <p>Related specifications: 045 * 046 * <ul> 047 * <li>OAuth 2.0 Pushed Authorization Requests (RFC 9126) 048 * </ul> 049 */ 050@Immutable 051public class PushedAuthorizationErrorResponse extends PushedAuthorizationResponse implements ErrorResponse { 052 053 054 /** 055 * The error. 056 */ 057 private final ErrorObject error; 058 059 060 /** 061 * Creates a new pushed authorisation error response. 062 * 063 * @param error The error. Must not be {@code null}. 064 */ 065 public PushedAuthorizationErrorResponse(final ErrorObject error) { 066 this.error = Objects.requireNonNull(error); 067 } 068 069 070 @Override 071 public boolean indicatesSuccess() { 072 return false; 073 } 074 075 076 @Override 077 public ErrorObject getErrorObject() { 078 return error; 079 } 080 081 082 @Override 083 public HTTPResponse toHTTPResponse() { 084 return getErrorObject().toHTTPResponse(); 085 } 086 087 088 /** 089 * Parses a pushed authorisation error response from the specified HTTP 090 * response. 091 * 092 * @param httpResponse The HTTP response. Must not be {@code null}. 093 * 094 * @return The pushed authorisation error response. 095 * 096 * @throws ParseException If the HTTP response couldn't be parsed to a 097 * pushed authorisation error response. 098 */ 099 public static PushedAuthorizationErrorResponse parse(final HTTPResponse httpResponse) 100 throws ParseException { 101 102 int statusCode = httpResponse.getStatusCode(); 103 104 if (statusCode == HTTPResponse.SC_CREATED || statusCode == HTTPResponse.SC_OK) { 105 throw new ParseException("The HTTP status code must be other than 201 and 200"); 106 } 107 108 ErrorObject errorObject; 109 if (httpResponse.getEntityContentType() != null && ContentType.APPLICATION_JSON.matches(httpResponse.getEntityContentType())) { 110 errorObject = ErrorObject.parse(httpResponse.getContentAsJSONObject()); 111 } else { 112 errorObject = new ErrorObject(null); 113 } 114 115 return new PushedAuthorizationErrorResponse(errorObject.setHTTPStatusCode(statusCode)); 116 } 117}