Class JwtService

java.lang.Object
it.univr.passportease.service.jwt.JwtService

@Service public class JwtService extends Object
This class is used to generate and validate JWT tokens. It manages both access and refresh tokens.
  • Field Details

    • ACCESS_TOKEN_KEY

      @NonNull private static final @NonNull String ACCESS_TOKEN_KEY
      The access token key used to sign the access token.
    • REFRESH_TOKEN_KEY

      @NonNull private static final @NonNull String REFRESH_TOKEN_KEY
      The refresh token key used to sign the refresh token.
    • workerRepository

      private final WorkerRepository workerRepository
      The repository for the Worker entity.
    • userRepository

      private final UserRepository userRepository
      The repository for the User entity.
    • redisTemplate

      private org.springframework.data.redis.core.RedisTemplate<String,String> redisTemplate
      The redis template used to store the access tokens.
  • Constructor Details

    • JwtService

      public JwtService()
  • Method Details

    • extractId

      public UUID extractId(JWT token)
      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

      public Date extractExpiration(JWT token)
      Extracts the expiration date of the token.
      Parameters:
      token - JWT token
      Returns:
      the expiration date of the token
    • extractClaim

      public <T> T extractClaim(JWT token, Function<io.jsonwebtoken.Claims,T> claimsResolver)
      Extracts the claims of the token.
      Type Parameters:
      T - type of the object to return
      Parameters:
      token - JWT token
      claimsResolver - function that takes a Claims object and returns a T object
      Returns:
      the object of type T extracted from the token
    • extractAllClaims

      private io.jsonwebtoken.Claims extractAllClaims(JWT token)
      Extracts all the claims of the token.
      Parameters:
      token - JWT token
      Returns:
      all the claims of the token
    • isTokenExpired

      public Boolean 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 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 token
      userDetails - user details of the user that owns the token
      Returns:
      true if the token is valid, false otherwise
    • generateAccessToken

      public JWT generateAccessToken(UUID id) throws UserOrWorkerIDNotFoundException
      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 UserOrWorkerIDNotFoundException
      Generates a new access token.
      Parameters:
      claims - claims to add to the token
      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
    • saveTokenInRedis

      private void saveTokenInRedis(UUID id, JWT token)
      Saves the token in redis with the key being the id of the user or worker.
      Parameters:
      id - id of the user or worker
      token - JWT token
    • generateRefreshToken

      public JWT generateRefreshToken(UUID id)
      Generates a new refresh token.
      Parameters:
      id - id of the user or worker
      Returns:
      a new refresh token, valid for 30 days
    • createRefreshToken

      private JWT createRefreshToken(UUID id)
      Generates a new refresh token.
      Parameters:
      id - id of the user or worker
      Returns:
      a new refresh token, valid for 30 days
    • saveRefreshTokenInDB

      private void saveRefreshTokenInDB(UUID id, JWT refreshtoken)
      Saves the refresh token in the database.
      Parameters:
      id - id of the user or worker
      refreshtoken - JWT refresh token
    • getRoleById

      private Roles getRoleById(UUID id) throws UserOrWorkerIDNotFoundException
      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

      private SecretKey getAccessSignKey()
      Gets the SecretKey used to sign the access token.
      Returns:
      the SecretKey used to sign the access token
    • getRefreshSignKey

      private SecretKey getRefreshSignKey()
      Gets the SecretKey used to sign the refresh token.
      Returns:
      the SecretKey used to sign the refresh token
    • invalidateAccessToken

      public Boolean invalidateAccessToken(JWT token)
      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

      public void invalidateRefreshToken(JWT token) throws UserNotFoundException
      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

      public UserType getUserOrWorkerFromToken(JWT token) throws UserNotFoundException
      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