Package vaultWeb.services.auth
Class AuthService
java.lang.Object
vaultWeb.services.auth.AuthService
Service class responsible for handling authentication and user session-related operations.
Provides functionality for:
- Authenticating users with username and password.
- Generating JWT tokens for authenticated users.
- Retrieving the currently authenticated user from the security context.
This service integrates with Spring Security's AuthenticationManager for authentication, UserRepository for fetching user entities, and JwtUtil for generating JWT tokens.
Security considerations:
- Passwords are never stored or transmitted in plaintext.
- Authentication uses BCryptPasswordEncoder for secure password hashing.
- JWT tokens are signed and include necessary claims (e.g., username, role) for stateless authentication.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionRetrieves the currently authenticated user from the SecurityContext.Authenticates a user using their username and password and returns a JWT token upon successful authentication.voidLogs out the current session by revoking the active refresh token (identified via its jti) and deleting the refresh token cookie.org.springframework.http.ResponseEntity<?> Refreshes the access token using a valid refresh token and performs refresh token rotation.
-
Constructor Details
-
AuthService
public AuthService()
-
-
Method Details
-
login
Authenticates a user using their username and password and returns a JWT token upon successful authentication.Workflow:
- The AuthenticationManager validates the username and password against the stored hash.
- If authentication succeeds, the Authentication object is stored in the SecurityContext.
- UserDetails are retrieved from the Authentication object, containing basic security info (username, roles).
- The full User entity is then loaded from the database for additional details.
- A JWT token is generated for the user, signed and valid for a specific duration.
Detailed notes on
authenticationManager.authenticate(...):- Spring Security calls the UserDetailsService to fetch user info by username.
- The provided password is compared with the stored hashed password using PasswordEncoder.
- If the password matches, a fully authenticated Authentication object is returned.
- If the password does not match, a BadCredentialsException is thrown.
- Parameters:
username- the username provided by the clientpassword- the plaintext password provided by the client- Returns:
- a signed JWT token representing the authenticated user
- Throws:
UserNotFoundException- if the user does not exist in the database
-
getCurrentUser
Retrieves the currently authenticated user from the SecurityContext.If no user is authenticated, this method returns
null. Otherwise, it fetches the fullUserentity from the database based on the username.- Returns:
- the currently authenticated
User, ornullif no user is authenticated
-
refresh
public org.springframework.http.ResponseEntity<?> refresh(String rawRefreshToken, jakarta.servlet.http.HttpServletResponse response) Refreshes the access token using a valid refresh token and performs refresh token rotation.Workflow:
- Parses and verifies the refresh JWT using the refresh signing key, including signature and expiration validation.
- Extracts the token identifier (
jti) from the refresh token. - Looks up the corresponding refresh token record in the database using the extracted
jti. - Verifies the refresh token by comparing the SHA-256 hash of the provided token with the stored hash.
- If valid, revokes the existing refresh token to prevent reuse (refresh token rotation).
- Issues a new refresh token, stores its hash in the database, and sends it to the client as a secure, HttpOnly cookie.
- Generates and returns a new short-lived access token.
Security considerations:
- Refresh tokens are JWTs signed with a dedicated refresh signing key.
- Only a non-secret identifier (
jti) is used for database lookup; the refresh token itself is never stored in plaintext. - Refresh tokens are stored using a one-way SHA-256 hash.
- Rotation ensures stolen refresh tokens cannot be reused.
- Revoked tokens may be retained temporarily to allow replay-attack detection and auditing.
Error scenarios:
401 Unauthorizedif the refresh token is missing, expired, revoked, invalid, or reused.
- Parameters:
rawRefreshToken- the refresh JWT provided by the client (via HttpOnly cookie)response- HTTP response used to set the rotated refresh token cookie- Returns:
- a response containing a new access token if the refresh succeeds
-
logout
Logs out the current session by revoking the active refresh token (identified via its jti) and deleting the refresh token cookie.This ensures the refresh token cannot be reused even if it was previously leaked or stolen.
-