Class AuthenticationService

java.lang.Object
com.ntu.fdae.group1.bto.services.user.AuthenticationService
All Implemented Interfaces:
IAuthenticationService

public class AuthenticationService extends Object implements IAuthenticationService
Implementation of the IAuthenticationService interface that provides authentication functionality for the BTO Management System.

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 Details

    • AuthenticationService

      public AuthenticationService(IUserRepository userRepository)
      Constructs a new AuthenticationService with the specified user repository.
      Parameters:
      userRepository - Repository for accessing user data
  • Method Details

    • login

      public User login(String nric, String password) throws AuthenticationException
      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 interface IAuthenticationService
      Parameters:
      nric - The NRIC (National Registration Identity Card) of the user
      password - 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 interface IAuthenticationService
      Parameters:
      user - The user whose password should be changed
      newPassword - 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 interface IAuthenticationService
      Parameters:
      nric - NRIC of the new user (must be unique)
      name - Full name of the user
      age - Age of the user
      maritalStatus - 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.