Class JwtService
java.lang.Object
it.univr.passportease.service.jwt.JwtService
This class is used to generate and validate JWT tokens. It manages both access and refresh tokens.
-
Field Summary
Modifier and TypeFieldDescriptionprivate static final @NonNull String
The access token key used to sign the access token.The redis template used to store the access tokens.private static final @NonNull String
The refresh token key used to sign the refresh token.private final UserRepository
The repository for theUser
entity.private final WorkerRepository
The repository for theWorker
entity. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprivate JWT
createAccessToken
(Map<String, Object> claims, UUID id) Generates a new access token.private JWT
Generates a new refresh token.private io.jsonwebtoken.Claims
extractAllClaims
(JWT token) Extracts all the claims of the token.<T> T
extractClaim
(JWT token, Function<io.jsonwebtoken.Claims, T> claimsResolver) Extracts the claims of the token.extractExpiration
(JWT token) Extracts the expiration date of the token.Extracts the id of the user or worker from the token.Generates a new access token.Generates a new refresh token.private SecretKey
Gets theSecretKey
used to sign the access token.private SecretKey
Gets theSecretKey
used to sign the refresh token.private Roles
getRoleById
(UUID id) Gets the role of the user or worker.getUserOrWorkerFromToken
(JWT token) Wrapper function to return User or Worker depending on the token.invalidateAccessToken
(JWT token) Invalidates the access token by deleting it from redis.void
invalidateRefreshToken
(JWT token) Invalidates the refresh token by setting it to an empty string.isTokenExpired
(JWT token) A token is expired if: it is expired it is not in redis the token in redis is not the same as the token in the request the token has nbf (not before) field and if it is after the current time the token has iat (issued at) field and if it is after the current timeprivate void
saveRefreshTokenInDB
(UUID id, JWT refreshtoken) Saves the refresh token in the database.private void
saveTokenInRedis
(UUID id, JWT token) Saves the token in redis with the key being the id of the user or worker.validTokenFromUserDetails
(JWT token, org.springframework.security.core.userdetails.UserDetails userDetails) Checks if the token is valid.
-
Field Details
-
ACCESS_TOKEN_KEY
The access token key used to sign the access token. -
REFRESH_TOKEN_KEY
The refresh token key used to sign the refresh token. -
workerRepository
The repository for theWorker
entity. -
userRepository
The repository for theUser
entity. -
redisTemplate
The redis template used to store the access tokens.
-
-
Constructor Details
-
JwtService
public JwtService()
-
-
Method Details
-
extractId
Extracts the id of the user or worker from the token.- Parameters:
token
- JWT token- Returns:
- the id of the user or worker that owns the token
-
extractExpiration
Extracts the expiration date of the token.- Parameters:
token
- JWT token- Returns:
- the expiration date of the token
-
extractClaim
Extracts the claims of the token.- Type Parameters:
T
- type of the object to return- Parameters:
token
- JWT tokenclaimsResolver
- function that takes a Claims object and returns a T object- Returns:
- the object of type T extracted from the token
-
extractAllClaims
Extracts all the claims of the token.- Parameters:
token
- JWT token- Returns:
- all the claims of the token
-
isTokenExpired
A token is expired if:- it is expired
- it is not in redis
- the token in redis is not the same as the token in the request
- the token has nbf (not before) field and if it is after the current time
- the token has iat (issued at) field and if it is after the current time
- Parameters:
token
- JWT token- Returns:
- true if the token is expired, false otherwise.
-
validTokenFromUserDetails
public Boolean validTokenFromUserDetails(JWT token, org.springframework.security.core.userdetails.UserDetails userDetails) Checks if the token is valid.- Parameters:
token
- JWT tokenuserDetails
- user details of the user that owns the token- Returns:
- true if the token is valid, false otherwise
-
generateAccessToken
Generates a new access token.- Parameters:
id
- id of the user or worker- Returns:
- a new access token, valid for 15 minutes
- Throws:
UserOrWorkerIDNotFoundException
- if the id does not belong to either a user or a worker
-
createAccessToken
private JWT createAccessToken(Map<String, Object> claims, UUID id) throws UserOrWorkerIDNotFoundExceptionGenerates a new access token.- Parameters:
claims
- claims to add to the tokenid
- id of the user or worker- Returns:
- a new access token, valid for 15 minutes
- Throws:
UserOrWorkerIDNotFoundException
- if the id does not belong to either a user or a worker
-
saveTokenInRedis
Saves the token in redis with the key being the id of the user or worker.- Parameters:
id
- id of the user or workertoken
- JWT token
-
generateRefreshToken
Generates a new refresh token.- Parameters:
id
- id of the user or worker- Returns:
- a new refresh token, valid for 30 days
-
createRefreshToken
Generates a new refresh token.- Parameters:
id
- id of the user or worker- Returns:
- a new refresh token, valid for 30 days
-
saveRefreshTokenInDB
Saves the refresh token in the database.- Parameters:
id
- id of the user or workerrefreshtoken
- JWT refresh token
-
getRoleById
Gets the role of the user or worker.- Parameters:
id
- id of the user or worker- Returns:
- an
Roles
object representing the role of the user or worker - Throws:
UserOrWorkerIDNotFoundException
- if the id does not belong to either a user or a worker
-
getAccessSignKey
Gets theSecretKey
used to sign the access token.- Returns:
- the
SecretKey
used to sign the access token
-
getRefreshSignKey
Gets theSecretKey
used to sign the refresh token.- Returns:
- the
SecretKey
used to sign the refresh token
-
invalidateAccessToken
Invalidates the access token by deleting it from redis.- Parameters:
token
- JWT Access token- Returns:
- true if the access token is deleted from redis, false otherwise
-
invalidateRefreshToken
Invalidates the refresh token by setting it to an empty string.- Parameters:
token
- JWT Refresh token- Throws:
UserNotFoundException
- if the user or worker is not found
-
getUserOrWorkerFromToken
Wrapper function to return User or Worker depending on the token.- Parameters:
token
- JWT token- Returns:
- the user or worker that owns the token, as a
UserType
object - Throws:
UserNotFoundException
- if the user or worker is not found
-