Class JwtService
java.lang.Object
edu.ntnu.idatt2106_2023_06.backend.service.security.JwtService
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<T> T
extractClaim
(String token, Function<io.jsonwebtoken.Claims, T> claimsResolver) Extracts a claim from a JWT token.extractExpiration
(String token) Extracts the expiration date from a JWT token.extractUsername
(String token) Extracts the username from a JWT token.generateToken
(Map<String, Object> extraClaims, org.springframework.security.core.userdetails.UserDetails userDetails) Generates a JWT token with extra claims for a given user.generateToken
(org.springframework.security.core.userdetails.UserDetails userDetails) Generates a JWT token for a given user.Gets the email of the authenticated user.Gets the ID of the authenticated user.boolean
Checks if the user is authenticated.boolean
isTokenValid
(String token, org.springframework.security.core.userdetails.UserDetails userDetails) Checks if a JWT token is valid for a given user.
-
Constructor Details
-
JwtService
public JwtService()
-
-
Method Details
-
extractUsername
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
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 fromclaimsResolver
- 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 validateuserDetails
- The UserDetails object representing the user to validate the token for- Returns:
- true if the token is valid for the user, false otherwise
-
extractExpiration
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
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 tokenuserDetails
- The UserDetails object representing the user to generate the token for- Returns:
- The generated JWT token
-
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
Gets the email of the authenticated user.- Returns:
- The email of the authenticated user. Returns null if the user is not authenticated.
-