001/* 002 * Copyright 2026 Revetware LLC. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.revetsec.oauth; 018 019import org.jspecify.annotations.NonNull; 020import org.jspecify.annotations.Nullable; 021 022import javax.annotation.concurrent.NotThreadSafe; 023import java.time.Duration; 024import java.util.Optional; 025 026/** 027 * A token, revocation or UserInfo endpoint error. The response body and its prose are never retained. 028 * 029 * @author <a href="https://www.revetkn.com">Mark Allen</a> 030 * @since 1.0.0 031 */ 032@NotThreadSafe 033public final class OAuthErrorResponseException extends OAuthException { 034 private static final long serialVersionUID = 1L; 035 /** Endpoint HTTP status retained without the response body. */ 036 private final int status; 037 /** Distinguishes a grant/resource endpoint response from discovery infrastructure. */ 038 private final boolean operationEndpoint; 039 /** Safe bounded OAuth error code; null when absent or unsafe. */ 040 private final @Nullable String errorCode; 041 /** Parsed retry delay; null when the header is absent or invalid. */ 042 private final @Nullable Duration retryAfter; 043 044 private OAuthErrorResponseException(int status, @Nullable String errorCode, 045 @Nullable Duration retryAfter, boolean operationEndpoint) { 046 super(Reason.ENDPOINT_ERROR, status == 429 || status >= 500 047 || "temporarily_unavailable".equals(errorCode), null); 048 this.status = status; 049 this.operationEndpoint = operationEndpoint; 050 this.errorCode = errorCode; 051 this.retryAfter = retryAfter; 052 } 053 054 @NonNull 055 static OAuthErrorResponseException fromResponse(int status, @NonNull String code, 056 @NonNull Optional<@NonNull Duration> retryAfter) { 057 return new OAuthErrorResponseException(status, OAuthSafeErrorCode.fromValue(code), retryAfter.orElse(null), true); 058 } 059 060 @NonNull 061 static OAuthErrorResponseException fromMetadataResponse(int status, 062 @NonNull Optional<@NonNull Duration> retryAfter) { 063 return new OAuthErrorResponseException(status, null, retryAfter.orElse(null), false); 064 } 065 066 boolean isEndpointRejection() { 067 return this.operationEndpoint && this.status >= 400 && this.status < 500 && !isTemporaryCondition(); 068 } 069 070 @NonNull OAuthErrorResponseException copyForCaller() { 071 return new OAuthErrorResponseException(this.status, this.errorCode, this.retryAfter, this.operationEndpoint); 072 } 073 074 /** 075 * Returns the endpoint's HTTP status, including 200 when the body reported an OAuth error. 076 * 077 * @return the HTTP status 078 * @since 1.0.0 079 */ 080 public @NonNull Integer getStatus() { 081 return this.status; 082 } 083 084 /** 085 * Returns the bounded, safe OAuth error code, if present. 086 * 087 * @return the safe code 088 * @since 1.0.0 089 */ 090 public @NonNull Optional<@NonNull String> getErrorCode() { 091 return Optional.ofNullable(this.errorCode); 092 } 093 094 /** 095 * Returns a parsed Retry-After delay, if present and valid. 096 * 097 * @return the delay 098 * @since 1.0.0 099 */ 100 public @NonNull Optional<@NonNull Duration> getRetryAfter() { 101 return Optional.ofNullable(this.retryAfter); 102 } 103}