Interface TokenService

All Known Implementing Classes:
TokenServiceImpl

public interface TokenService
Interface for handling JWT token generation, parsing, and refreshing operations.

This interface defines the necessary methods for working with JSON Web Tokens (JWT) in an authentication system. Implementations of this interface are responsible for generating access and refresh tokens, parsing the access token from an HTTP request, and refreshing the tokens based on the refresh token.

  • Method Summary

    Modifier and Type
    Method
    Description
    generateToken(org.springframework.security.core.Authentication authentication)
    Generates a new access and refresh token for the given authenticated user.
    id(String token)
    Extracts the user ID from a JWT token.
    org.springframework.security.authentication.UsernamePasswordAuthenticationToken
    Parses the access token from a provided token string and retrieves the associated authentication.
    refreshToken(RefreshTokenDto refreshTokenDto)
    Refreshes the access token using the provided refresh token.
    void
    setCookie(TokensDto tokens, jakarta.servlet.http.HttpServletResponse response)
     
  • Method Details

    • generateToken

      TokensDto generateToken(org.springframework.security.core.Authentication authentication)
      Generates a new access and refresh token for the given authenticated user.

      This method creates an access token and a refresh token for a user based on the provided authentication object. The generated tokens are signed with a secret key and contain expiration times.

      Parameters:
      authentication - the authentication object containing the authenticated user's details
      Returns:
      a TokensDto object containing the generated access and refresh tokens
    • parseAccessToken

      org.springframework.security.authentication.UsernamePasswordAuthenticationToken parseAccessToken(String token)
      Parses the access token from a provided token string and retrieves the associated authentication.

      This method is responsible for extracting the JWT from the authorization header (or other source), verifying its validity, and using it to create a UsernamePasswordAuthenticationToken that represents the authenticated user.

      Parameters:
      token - the JWT token in the authorization header or request
      Returns:
      a UsernamePasswordAuthenticationToken containing the authenticated user's details
      Throws:
      IllegalArgumentException - if the token is invalid or cannot be parsed
    • refreshToken

      TokensDto refreshToken(RefreshTokenDto refreshTokenDto)
      Refreshes the access token using the provided refresh token.

      This method validates the provided refresh token and uses it to generate a new set of tokens, including a fresh access token and a new refresh token. The method ensures that the refresh token is valid and has not expired before issuing the new tokens.

      Parameters:
      refreshTokenDto - a RefreshTokenDto containing the refresh token to be used for refreshing the access token
      Returns:
      a TokensDto containing the newly generated access and refresh tokens
      Throws:
      IllegalArgumentException - if the refresh token is invalid or null
      IllegalStateException - if the refresh token is expired or otherwise cannot be used to generate a new access token
    • id

      Long id(String token)
      Extracts the user ID from a JWT token.

      This method is used to extract the user ID from the provided token's claims.

      Parameters:
      token - the JWT token containing the user ID as its subject
      Returns:
      the user ID extracted from the token
    • setCookie

      void setCookie(TokensDto tokens, jakarta.servlet.http.HttpServletResponse response)