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 com.revetsec.ErrorCategory; 020import org.jspecify.annotations.NonNull; 021 022import javax.annotation.concurrent.NotThreadSafe; 023 024import static java.util.Objects.requireNonNull; 025 026/** 027 * Thrown when a well-formed token fails a check: its algorithm, key, signature, type, issuer, audience, lifetime, 028 * required claims or confirmation claim. 029 * <p> 030 * Its category is {@link ErrorCategory#VALIDATION_FAILURE}, and its {@link JoseException.Reason} says which check 031 * failed. It has no cause and is never transient. 032 * 033 * @author <a href="https://www.revetkn.com">Mark Allen</a> 034 * @since 1.0.0 035 */ 036@NotThreadSafe 037public final class JwtValidationException extends JoseException { 038 /** 039 * The serialized form's version. 040 */ 041 private static final long serialVersionUID = 1L; 042 043 private JwtValidationException(@NonNull Reason reason) { 044 super(reason); 045 } 046 047 /** 048 * Returns a new instance for {@code reason}. Only Revetsec calls this. 049 * 050 * @param reason a reason in category {@link ErrorCategory#VALIDATION_FAILURE} 051 * @return a new instance 052 * @throws IllegalArgumentException if {@code reason} belongs to another category 053 */ 054 @NonNull 055 static JwtValidationException fromReason(@NonNull Reason reason) { 056 if (requireNonNull(reason).category() != ErrorCategory.VALIDATION_FAILURE) 057 throw new IllegalArgumentException("A validation reason must be in category VALIDATION_FAILURE."); 058 059 return new JwtValidationException(reason); 060 } 061}