Package alpine.server.auth
Class JsonWebToken
java.lang.Object
alpine.server.auth.JsonWebToken
Decouples the general usage of JSON Web Tokens with the actual implementation of a JWT library
All JWT usages should only go through this class and hide the actual implementation details
and to avoid improper or insecure use of JWTs.
- Since:
- 1.0.0
- Author:
- Steve Springett
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new JsonWebToken object using the default Alpine-generated secret key.JsonWebToken(SecretKey key) Constructs a new JsonWekToken object using the specified SecretKey which can be retrieved fromKeyManager.getSecretKey()to use the Alpine-generated secret key. -
Method Summary
Modifier and TypeMethodDescriptioncreateToken(Principal principal) Creates a new JWT for the specified principal.createToken(Principal principal, List<Permission> permissions) Creates a new JWT for the specified principal.createToken(Principal principal, List<Permission> permissions, IdentityProvider identityProvider) Creates a new JWT for the specified principal.createToken(Map<String, Object> claims) Creates a new JWT for the specified principal.Returns the expiration of the token.Returns the identity provider of the token.Returns the subject of the token.booleanvalidateToken(String token) Validates a JWT by ensuring the signature matches and validates against the SecretKey and checks the expiration date.
-
Constructor Details
-
JsonWebToken
Constructs a new JsonWekToken object using the specified SecretKey which can be retrieved fromKeyManager.getSecretKey()to use the Alpine-generated secret key. Usage of other SecretKeys is allowed but management of those keys is up to the implementor.- Parameters:
key- the SecretKey to use in generating or validating the token- Since:
- 1.0.0
-
JsonWebToken
public JsonWebToken()Constructs a new JsonWebToken object using the default Alpine-generated secret key.- Since:
- 1.0.0
- See Also:
-
-
Method Details
-
createToken
Creates a new JWT for the specified principal. Token is signed using the SecretKey with an HMAC 256 algorithm.- Parameters:
principal- the Principal to create the token for- Returns:
- a String representation of the generated token
- Since:
- 1.0.0
-
createToken
Creates a new JWT for the specified principal. Token is signed using the SecretKey with an HMAC 256 algorithm.- Parameters:
principal- the Principal to create the token forpermissions- the effective list of permissions for the principal- Returns:
- a String representation of the generated token
- Since:
- 1.1.0
-
createToken
public String createToken(Principal principal, List<Permission> permissions, IdentityProvider identityProvider) Creates a new JWT for the specified principal. Token is signed using the SecretKey with an HMAC 256 algorithm.- Parameters:
principal- the Principal to create the token forpermissions- the effective list of permissions for the principalidentityProvider- the identity provider the principal was authenticated with. If null, it will be derived from principal- Returns:
- a String representation of the generated token
- Since:
- 1.8.0
-
createToken
Creates a new JWT for the specified principal. Token is signed using the SecretKey with an HMAC 256 algorithm.- Parameters:
claims- a Map of all claims- Returns:
- a String representation of the generated token
- Since:
- 1.0.0
-
validateToken
Validates a JWT by ensuring the signature matches and validates against the SecretKey and checks the expiration date.- Parameters:
token- the token to validate- Returns:
- true if validation successful, false if not
- Since:
- 1.0.0
-
getSubject
Returns the subject of the token.- Returns:
- a String
-
getExpiration
Returns the expiration of the token.- Returns:
- a Date
-
getIdentityProvider
Returns the identity provider of the token.- Returns:
- an IdentityProvider
-