Class AuthenticationService
- All Implemented Interfaces:
IAuthenticationService
This service is responsible for handling user authentication operations, including: - Verifying user credentials during login - Managing password changes - Securing user authentication data
The service uses the PasswordUtil class for secure password hashing and verification, and interacts with the user repository to access and update user data.
-
Constructor Summary
ConstructorsConstructorDescriptionAuthenticationService
(IUserRepository userRepository) Constructs a new AuthenticationService with the specified user repository. -
Method Summary
Modifier and TypeMethodDescriptionboolean
changePassword
(User user, String newPassword) Changes a user's password after validating its strength.Authenticates a user with the provided credentials.boolean
registerApplicant
(String nric, String name, int age, MaritalStatus maritalStatus) Registers a new applicant user using default password.
-
Constructor Details
-
AuthenticationService
Constructs a new AuthenticationService with the specified user repository.- Parameters:
userRepository
- Repository for accessing user data
-
-
Method Details
-
login
Authenticates a user with the provided credentials.Verifies that the user exists and that the provided password matches the stored password hash.
- Specified by:
login
in interfaceIAuthenticationService
- Parameters:
nric
- The NRIC (National Registration Identity Card) of the userpassword
- The password provided by the user- Returns:
- The authenticated User object if credentials are valid
- Throws:
AuthenticationException
- If the user doesn't exist or the password is incorrect
-
changePassword
public boolean changePassword(User user, String newPassword) throws WeakPasswordException, DataAccessException Changes a user's password after validating its strength.Validates the new password against strength criteria, generates a new password hash, updates the user object, and persists the change.
- Specified by:
changePassword
in interfaceIAuthenticationService
- Parameters:
user
- The user whose password should be changednewPassword
- The new password to set- Returns:
- true if the password was successfully changed, false if any inputs are invalid
- Throws:
WeakPasswordException
- if the new password does not meet strength criteria.DataAccessException
- if there's an error saving the user data.
-
registerApplicant
public boolean registerApplicant(String nric, String name, int age, MaritalStatus maritalStatus) throws AuthenticationException, DataAccessException Registers a new applicant user using default password.Validates the provided information, checks for NRIC uniqueness, hashes default password, creates a new Applicant object, and saves it to the repository.
- Specified by:
registerApplicant
in interfaceIAuthenticationService
- Parameters:
nric
- NRIC of the new user (must be unique)name
- Full name of the userage
- Age of the usermaritalStatus
- Marital status of the user- Returns:
- true if registration was successful, false otherwise
- Throws:
AuthenticationException
- if NRIC already exists or validation fails.DataAccessException
- if saving fails.
-