java.lang.Object
edu.ntnu.idatt2106_2023_06.backend.service.security.JwtService

@Service public class JwtService extends Object
  • Constructor Details

    • JwtService

      public JwtService()
  • Method Details

    • extractUsername

      public String extractUsername(String token)
      Extracts the username from a JWT token.
      Parameters:
      token - The JWT token to extract the username from
      Returns:
      The username associated with the token
    • extractClaim

      public <T> T extractClaim(String token, Function<io.jsonwebtoken.Claims,T> claimsResolver)
      Extracts a claim from a JWT token.
      Type Parameters:
      T - The type of the desired claim
      Parameters:
      token - The JWT token to extract the claim from
      claimsResolver - A function that takes a Claims object and returns a T object representing the desired claim
      Returns:
      The claim represented by a T object
    • isTokenValid

      public boolean isTokenValid(String token, org.springframework.security.core.userdetails.UserDetails userDetails)
      Checks if a JWT token is valid for a given user.
      Parameters:
      token - The JWT token to validate
      userDetails - The UserDetails object representing the user to validate the token for
      Returns:
      true if the token is valid for the user, false otherwise
    • extractExpiration

      public Date extractExpiration(String token)
      Extracts the expiration date from a JWT token.
      Parameters:
      token - The JWT token to extract the expiration date from
      Returns:
      The expiration date associated with the token
    • generateToken

      public String generateToken(org.springframework.security.core.userdetails.UserDetails userDetails)
      Generates a JWT token for a given user.
      Parameters:
      userDetails - The UserDetails object representing the user to generate the token for
      Returns:
      The generated JWT token
    • generateToken

      public String generateToken(Map<String,Object> extraClaims, org.springframework.security.core.userdetails.UserDetails userDetails)
      Generates a JWT token with extra claims for a given user.
      Parameters:
      extraClaims - A Map containing extra claims to include in the token
      userDetails - The UserDetails object representing the user to generate the token for
      Returns:
      The generated JWT token
    • getAuthenticatedUserId

      public Long getAuthenticatedUserId()
      Gets the ID of the authenticated user.
      Returns:
      The ID of the authenticated user. Returns null if the user is not authenticated.
    • isAuthenticated

      public boolean isAuthenticated()
      Checks if the user is authenticated.
      Returns:
      true if the user is authenticated, false otherwise.
    • getAuthenticatedUserEmail

      public String getAuthenticatedUserEmail()
      Gets the email of the authenticated user.
      Returns:
      The email of the authenticated user. Returns null if the user is not authenticated.