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.jose; 018 019import org.jspecify.annotations.NonNull; 020import javax.annotation.concurrent.Immutable; 021import static java.util.Objects.requireNonNull; 022 023/** 024 * The outcome of JWT validation. Remote key-source failures remain exceptions. 025 * <p> 026 * Applications receive these outcomes from the corresponding operation; they cannot construct them. 027 * Value accessors exist only on the variant that owns a value. Diagnostic text contains no input or result data. 028 * A later release may add variants; consuming switches should include a rejecting default branch. 029 * 030 * @author <a href="https://www.revetkn.com">Mark Allen</a> 031 * @since 1.0.0 032 */ 033@Immutable 034public abstract sealed class JwtValidationResult permits JwtValidationResult.Succeeded, JwtValidationResult.Rejected { 035 JwtValidationResult() { } 036 037 static @NonNull JwtValidationResult fromJwt(@NonNull Jwt jwt) { 038 return new Succeeded(requireNonNull(jwt)); 039 } 040 041 static @NonNull JwtValidationResult fromReason(JoseException.@NonNull Reason reason) { 042 return new Rejected(requireNonNull(reason)); 043 } 044 045 /** 046 * Returns the outcome kind with all values redacted. 047 * @return redacted description 048 * @since 1.0.0 049 */ 050 @Override 051 public final @NonNull String toString() { 052 return "JwtValidationResult{outcome=" + getClass().getSimpleName() + ", data=<redacted>}"; 053 } 054 055 /** 056 * A JWT passed all configured signature, header and claims checks. 057 * @author <a href="https://www.revetkn.com">Mark Allen</a> 058 * @since 1.0.0 059 */ 060 @Immutable 061 public static final class Succeeded extends JwtValidationResult { 062 private final @NonNull Jwt jwt; 063 private Succeeded(@NonNull Jwt jwt) { this.jwt = jwt; } 064 065 /** 066 * Returns the JWT that passed the configured validation policy. 067 * @return jwt 068 * @since 1.0.0 069 */ 070 public @NonNull Jwt getJwt() { return this.jwt; } 071 } 072 073 /** 074 * The input was rejected; this variant contains only a fixed reason. 075 * @author <a href="https://www.revetkn.com">Mark Allen</a> 076 * @since 1.0.0 077 */ 078 @Immutable 079 public static final class Rejected extends JwtValidationResult { 080 private final JoseException.@NonNull Reason reason; 081 private Rejected(JoseException.@NonNull Reason reason) { this.reason = reason; } 082 083 /** 084 * Returns the fixed JOSE rejection reason. 085 * @return reason 086 * @since 1.0.0 087 */ 088 public JoseException.@NonNull Reason getReason() { return this.reason; } 089 } 090}