Top Inners Fields Constructors Methods
org.keycloak

public Class TokenVerifier<T extends org.keycloak.representations.JsonWebToken>

extends Object
Class Inheritance
Imports
org.keycloak.common.VerificationException, org.keycloak.exceptions.TokenNotActiveException, .TokenSignatureInvalidException, org.keycloak.jose.jws.AlgorithmType, .JWSHeader, .JWSInput, .JWSInputException, org.keycloak.crypto.SignatureVerifierContext, org.keycloak.jose.jws.crypto.HMACProvider, .RSAProvider, org.keycloak.representations.JsonWebToken, org.keycloak.util.TokenUtil, javax.crypto.SecretKey, java.security.PublicKey, java.util.Arrays, .Iterator, .LinkedList, .List, java.util.logging.Level, .Logger

Author
Bill Burke
Version
$Revision: 1 $

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
public static class
public static interface
TokenVerifier.Predicate<
Type of the token handled by this predicate.
T extends JsonWebToken
>

Functional interface of checks that verify some part of a JWT.

public static class
public static class

Field Summary

Modifier and TypeField and Description
private boolean
private final LinkedList<TokenVerifier.Predicate<? super T>>
private boolean
private Class<? extends T>
private List<String>
public static final TokenVerifier.Predicate<JsonWebToken>
IS_ACTIVE

Check for token being neither expired nor used before it gets valid.

private JWSInput
private static final Logger
private PublicKey
private String
private SecretKey
public static final TokenVerifier.Predicate<JsonWebToken>
private T
private String
private SignatureVerifierContext

Constructor Summary

AccessConstructor and Description
protected
TokenVerifier(String tokenString, Class<T> clazz)

protected
TokenVerifier(T token)

Method Summary

Modifier and TypeMethod and Description
public static <T extends JsonWebToken> TokenVerifier.Predicate<T>
alternative(final TokenVerifier.Predicate<? super T>... predicates)

Creates a predicate that will proceed with checks of the given predicates and will pass if and only if at least one of the given predicates passes.

public TokenVerifier<T>

Returns:

This token verifier
audience
(String...
Audiences, which needs to be in the target token. Can be null.
expectedAudiences
)

Add check for verifying that token contains the expectedAudience

public TokenVerifier<T>

Returns:

This token verifier
checkActive
(boolean checkActive)

Deprecated This method is here only for backward compatibility with previous version of TokenVerifier.
public TokenVerifier<T>

Returns:

This token verifier
checkRealmUrl
(boolean checkRealmUrl)

Deprecated This method is here only for backward compatibility with previous version of TokenVerifier.
public TokenVerifier<T>

Returns:

This token verifier
checkTokenType
(boolean checkTokenType)

Deprecated This method is here only for backward compatibility with previous version of TokenVerifier.
public static <
Type of the token
T extends JsonWebToken
>
TokenVerifier<T>
create(String
String representation of JWT
tokenString
,
Class<T>
Class of the token
clazz
)

Creates an instance of TokenVerifier from the given string on a JWT of the given class.

public static <T extends JsonWebToken> TokenVerifier<T>
createWithoutSignature(T token)

Creates an instance of TokenVerifier for the given token.

public JWSHeader
public T
public TokenVerifier<T>

Returns:

This token verifier
issuedFor
(String
issuedFor, which needs to be in the target token. Can't be null
expectedIssuedFor
)

Add check for verifying that token issuedFor (azp claim) is the expected value

public static <T extends JsonWebToken> TokenVerifier.Predicate<T>
optional(final TokenVerifier.Predicate<T> mandatoryPredicate)

Creates an optional predicate from a predicate that will proceed with check but always pass.

public TokenVerifier<T>
public TokenVerifier<T>
publicKey(PublicKey publicKey)

Sets the key for verification of RSA-based signature.

public TokenVerifier<T>

Returns:

This token verifier
realmUrl
(String realmUrl)

Deprecated This method is here only for backward compatibility with previous version of TokenVerifier.
private void
removeCheck(Class<? extends TokenVerifier.Predicate<?>> checkClass)

private void
private <P extends TokenVerifier.Predicate<? super T>> TokenVerifier<T>
replaceCheck(Class<? extends TokenVerifier.Predicate<?>> checkClass, boolean active, P... predicate)

private <P extends TokenVerifier.Predicate<? super T>> TokenVerifier<T>
replaceCheck(TokenVerifier.Predicate<? super T> check, boolean active, P... predicate)

public TokenVerifier<T>
secretKey(SecretKey secretKey)

Sets the key for verification of HMAC-based signature.

public TokenVerifier<T>

Returns:

This token verifier
tokenType
(List<String> tokenTypes)

public TokenVerifier<T>
public TokenVerifier<T>
public void
public final TokenVerifier<T>
withChecks(TokenVerifier.Predicate<? super T>... checks)

Will test the given checks in verify() method in addition to already set checks.

public TokenVerifier<T>

Returns:

This token verifier.
withDefaultChecks
()

Adds default checks to the token verification:

  • Realm URL (JWT issuer field: iss) has to be defined and match realm set via realmUrl(java.lang.String) method
  • Subject (JWT subject field: sub) has to be defined
  • Token type (JWT type field: typ) has to be Bearer. The type can be set via tokenType(java.lang.String) method
  • Token has to be active, ie. both not expired and not used before its validity (JWT issuer fields: exp and nbf)
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

checkRealmUrlback to summary
private boolean checkRealmUrl
checksback to summary
private final LinkedList<TokenVerifier.Predicate<? super T>> checks
checkTokenTypeback to summary
private boolean checkTokenType
clazzback to summary
private Class<? extends T> clazz
expectedTokenTypeback to summary
private List<String> expectedTokenType
IS_ACTIVEback to summary
public static final TokenVerifier.Predicate<JsonWebToken> IS_ACTIVE

Check for token being neither expired nor used before it gets valid.

See Also
JsonWebToken#isActive()
jwsback to summary
private JWSInput jws
LOGback to summary
private static final Logger LOG
publicKeyback to summary
private PublicKey publicKey
realmUrlback to summary
private String realmUrl
secretKeyback to summary
private SecretKey secretKey
SUBJECT_EXISTS_CHECKback to summary
public static final TokenVerifier.Predicate<JsonWebToken> SUBJECT_EXISTS_CHECK
tokenback to summary
private T token
tokenStringback to summary
private String tokenString
verifierback to summary
private SignatureVerifierContext verifier

Constructor Detail

TokenVerifierback to summary
protected TokenVerifier(String tokenString, Class<T> clazz)
TokenVerifierback to summary
protected TokenVerifier(T token)

Method Detail

alternativeback to summary
public static <T extends JsonWebToken> TokenVerifier.Predicate<T> alternative(final TokenVerifier.Predicate<? super T>... predicates)

Creates a predicate that will proceed with checks of the given predicates and will pass if and only if at least one of the given predicates passes.

Annotations
@SafeVarargs
audienceback to summary
public TokenVerifier<T> audience(String... expectedAudiences)

Add check for verifying that token contains the expectedAudience

Parameters
expectedAudiences:String[]

Audiences, which needs to be in the target token. Can be null.

Returns:TokenVerifier<T>

This token verifier

checkActiveback to summary
public TokenVerifier<T> checkActive(boolean checkActive)

Deprecated

This method is here only for backward compatibility with previous version of TokenVerifier.

Returns:TokenVerifier<T>

This token verifier

checkRealmUrlback to summary
public TokenVerifier<T> checkRealmUrl(boolean checkRealmUrl)

Deprecated

This method is here only for backward compatibility with previous version of TokenVerifier.

Returns:TokenVerifier<T>

This token verifier

checkTokenTypeback to summary
public TokenVerifier<T> checkTokenType(boolean checkTokenType)

Deprecated

This method is here only for backward compatibility with previous version of TokenVerifier.

Returns:TokenVerifier<T>

This token verifier

createback to summary
public static <T extends JsonWebToken> TokenVerifier<T> create(String tokenString, Class<T> clazz)

Creates an instance of TokenVerifier from the given string on a JWT of the given class. The token verifier has no checks defined. Note that the checks are only tested when verify() method is invoked.

Parameters
<T>
Type of the token
tokenString:String

String representation of JWT

clazz:Class<T>

Class of the token

createWithoutSignatureback to summary
public static <T extends JsonWebToken> TokenVerifier<T> createWithoutSignature(T token)

Creates an instance of TokenVerifier for the given token. The token verifier has no checks defined. Note that the checks are only tested when verify() method is invoked.

Note

The returned token verifier cannot verify token signature since that is not part of the JsonWebToken object.

getHeaderback to summary
public JWSHeader getHeader() throws VerificationException
getTokenback to summary
public T getToken() throws VerificationException
issuedForback to summary
public TokenVerifier<T> issuedFor(String expectedIssuedFor)

Add check for verifying that token issuedFor (azp claim) is the expected value

Parameters
expectedIssuedFor:String

issuedFor, which needs to be in the target token. Can't be null

Returns:TokenVerifier<T>

This token verifier

optionalback to summary
public static <T extends JsonWebToken> TokenVerifier.Predicate<T> optional(final TokenVerifier.Predicate<T> mandatoryPredicate)

Creates an optional predicate from a predicate that will proceed with check but always pass.

parseback to summary
public TokenVerifier<T> parse() throws VerificationException
publicKeyback to summary
public TokenVerifier<T> publicKey(PublicKey publicKey)

Sets the key for verification of RSA-based signature.

realmUrlback to summary
public TokenVerifier<T> realmUrl(String realmUrl)

Deprecated

This method is here only for backward compatibility with previous version of TokenVerifier.

Returns:TokenVerifier<T>

This token verifier

removeCheckback to summary
private void removeCheck(Class<? extends TokenVerifier.Predicate<?>> checkClass)
removeCheckback to summary
private void removeCheck(TokenVerifier.Predicate<? super T> check)
replaceCheckback to summary
private <P extends TokenVerifier.Predicate<? super T>> TokenVerifier<T> replaceCheck(Class<? extends TokenVerifier.Predicate<?>> checkClass, boolean active, P... predicate)
Annotations
@SuppressWarnings:unchecked
replaceCheckback to summary
private <P extends TokenVerifier.Predicate<? super T>> TokenVerifier<T> replaceCheck(TokenVerifier.Predicate<? super T> check, boolean active, P... predicate)
Annotations
@SuppressWarnings:unchecked
secretKeyback to summary
public TokenVerifier<T> secretKey(SecretKey secretKey)

Sets the key for verification of HMAC-based signature.

tokenTypeback to summary
public TokenVerifier<T> tokenType(List<String> tokenTypes)
Returns:TokenVerifier<T>

This token verifier

verifierContextback to summary
public TokenVerifier<T> verifierContext(SignatureVerifierContext verifier)
verifyback to summary
public TokenVerifier<T> verify() throws VerificationException
verifySignatureback to summary
public void verifySignature() throws VerificationException
withChecksback to summary
public final TokenVerifier<T> withChecks(TokenVerifier.Predicate<? super T>... checks)

Will test the given checks in verify() method in addition to already set checks.

Annotations
@SafeVarargs
withDefaultChecksback to summary
public TokenVerifier<T> withDefaultChecks()

Adds default checks to the token verification:

  • Realm URL (JWT issuer field: iss) has to be defined and match realm set via realmUrl(java.lang.String) method
  • Subject (JWT subject field: sub) has to be defined
  • Token type (JWT type field: typ) has to be Bearer. The type can be set via tokenType(java.lang.String) method
  • Token has to be active, ie. both not expired and not used before its validity (JWT issuer fields: exp and nbf)
Returns:TokenVerifier<T>

This token verifier.

org.keycloak back to summary

public Class TokenVerifier.AudienceCheck

extends Object
implements TokenVerifier.Predicate<JsonWebToken>
Class Inheritance
All Implemented Interfaces
org.keycloak.TokenVerifier.Predicate

Field Summary

Modifier and TypeField and Description
private final String

Constructor Summary

AccessConstructor and Description
public
AudienceCheck(String expectedAudience)

Method Summary

Modifier and TypeMethod and Description
public boolean
test(JsonWebToken
Token, guaranteed to be non-null.
t
)

Implements org.keycloak.TokenVerifier.Predicate.test.

Performs a single check on the given token verifier.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

expectedAudienceback to summary
private final String expectedAudience

Constructor Detail

AudienceCheckback to summary
public AudienceCheck(String expectedAudience)

Method Detail

testback to summary
public boolean test(JsonWebToken t) throws VerificationException

Implements org.keycloak.TokenVerifier.Predicate.test.

Doc from org.keycloak.TokenVerifier.Predicate.test.

Performs a single check on the given token verifier.

Parameters
t:JsonWebToken

Token, guaranteed to be non-null.

Annotations
@Override
org.keycloak back to summary

public Class TokenVerifier.IssuedForCheck

extends Object
implements TokenVerifier.Predicate<JsonWebToken>
Class Inheritance
All Implemented Interfaces
org.keycloak.TokenVerifier.Predicate

Field Summary

Modifier and TypeField and Description
private final String

Constructor Summary

AccessConstructor and Description
public
IssuedForCheck(String expectedIssuedFor)

Method Summary

Modifier and TypeMethod and Description
public boolean
test(JsonWebToken
Token, guaranteed to be non-null.
jsonWebToken
)

Implements org.keycloak.TokenVerifier.Predicate.test.

Performs a single check on the given token verifier.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

expectedIssuedForback to summary
private final String expectedIssuedFor

Constructor Detail

IssuedForCheckback to summary
public IssuedForCheck(String expectedIssuedFor)

Method Detail

testback to summary
public boolean test(JsonWebToken jsonWebToken) throws VerificationException

Implements org.keycloak.TokenVerifier.Predicate.test.

Doc from org.keycloak.TokenVerifier.Predicate.test.

Performs a single check on the given token verifier.

Parameters
jsonWebToken:JsonWebToken

Token, guaranteed to be non-null.

Annotations
@Override
org.keycloak back to summary

public Interface TokenVerifier.Predicate<T extends org.keycloak.representations.JsonWebToken>

Known Direct Implementers
org.keycloak.TokenVerifier.RealmUrlCheck, org.keycloak.TokenVerifier.TokenTypeCheck, org.keycloak.TokenVerifier.AudienceCheck, org.keycloak.TokenVerifier.IssuedForCheck
Type Parameters
<T>
Type of the token handled by this predicate.

Functional interface of checks that verify some part of a JWT.

Method Summary

Modifier and TypeMethod and Description
public boolean
test(T
Token, guaranteed to be non-null.
t
)

Performs a single check on the given token verifier.

Method Detail

testback to summary
public boolean test(T t) throws VerificationException

Performs a single check on the given token verifier.

Parameters
t:T

Token, guaranteed to be non-null.

org.keycloak back to summary

public Class TokenVerifier.RealmUrlCheck

extends Object
implements TokenVerifier.Predicate<JsonWebToken>
Class Inheritance
All Implemented Interfaces
org.keycloak.TokenVerifier.Predicate

Field Summary

Modifier and TypeField and Description
private static final TokenVerifier.RealmUrlCheck
private final String

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public boolean
test(JsonWebToken
Token, guaranteed to be non-null.
t
)

Implements org.keycloak.TokenVerifier.Predicate.test.

Performs a single check on the given token verifier.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

NULL_INSTANCEback to summary
private static final TokenVerifier.RealmUrlCheck NULL_INSTANCE
realmUrlback to summary
private final String realmUrl

Constructor Detail

RealmUrlCheckback to summary
public RealmUrlCheck(String realmUrl)

Method Detail

testback to summary
public boolean test(JsonWebToken t) throws VerificationException

Implements org.keycloak.TokenVerifier.Predicate.test.

Doc from org.keycloak.TokenVerifier.Predicate.test.

Performs a single check on the given token verifier.

Parameters
t:JsonWebToken

Token, guaranteed to be non-null.

Annotations
@Override
org.keycloak back to summary

public Class TokenVerifier.TokenTypeCheck

extends Object
implements TokenVerifier.Predicate<JsonWebToken>
Class Inheritance
All Implemented Interfaces
org.keycloak.TokenVerifier.Predicate

Field Summary

Modifier and TypeField and Description
private static final TokenVerifier.TokenTypeCheck
private final List<String>

Constructor Summary

AccessConstructor and Description
public
TokenTypeCheck(List<String> tokenTypes)

Method Summary

Modifier and TypeMethod and Description
public boolean
test(JsonWebToken
Token, guaranteed to be non-null.
t
)

Implements org.keycloak.TokenVerifier.Predicate.test.

Performs a single check on the given token verifier.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

INSTANCE_DEFAULT_TOKEN_TYPEback to summary
private static final TokenVerifier.TokenTypeCheck INSTANCE_DEFAULT_TOKEN_TYPE
tokenTypesback to summary
private final List<String> tokenTypes

Constructor Detail

TokenTypeCheckback to summary
public TokenTypeCheck(List<String> tokenTypes)

Method Detail

testback to summary
public boolean test(JsonWebToken t) throws VerificationException

Implements org.keycloak.TokenVerifier.Predicate.test.

Doc from org.keycloak.TokenVerifier.Predicate.test.

Performs a single check on the given token verifier.

Parameters
t:JsonWebToken

Token, guaranteed to be non-null.

Annotations
@Override