Class AuthenticationService

java.lang.Object
org.ntnu.idi.idatt2106.sparesti.sparestibackend.service.AuthenticationService

@Service public class AuthenticationService extends Object
Service responsible for registering a new user, logging in an existing user and refreshing a user's access token
Since:
17.4.24
Version:
1.0
Author:
Harry L.X. and Lars M.L.N
  • Constructor Details

    • AuthenticationService

      public AuthenticationService()
  • Method Details

    • register

      Registers a new, valid user. For a user to be valid, they have to have a valid and unique username, a valid and unique email, valid first name and last names, and a strong password.
      Parameters:
      request - Wrapper for user information used for registering
      Returns:
      Jwt tokens for the registered user
      Throws:
      BadInputException - If the user information is invalid (username, first/last names, email) of if password is weak
      UserAlreadyExistsException - If username or email have been taken
      ObjectNotValidException
    • login

      public LoginRegisterResponse login(AuthenticationRequest request) throws ObjectNotValidException, org.springframework.security.authentication.BadCredentialsException
      Log in user with credentials (username and password)
      Parameters:
      request - Wrapper for username and password
      Returns:
      Jwt tokens upon successful login
      Throws:
      ObjectNotValidException - If request object is invalid
      org.springframework.security.authentication.BadCredentialsException - If credentials don't match
    • refreshAccessToken

      public AccessTokenResponse refreshAccessToken(String bearerToken) throws UserNotFoundException
      Refreshes access token given a valid refresh token
      Parameters:
      bearerToken - Stringified HTTP-header (Authorization-header)
      Returns:
      Access token wrapper if the refresh token is valid
      Throws:
      UserNotFoundException - If the tokens subject matches no existing username
    • bioAuthRegistration

      public String bioAuthRegistration(String username) throws com.fasterxml.jackson.core.JsonProcessingException
      Initiates a new biometric authentication registration process for the specified user.
      Parameters:
      username - The username of the user for whom biometric authentication registration is initiated.
      Returns:
      A JSON string representing the registration request for biometric authentication.
      Throws:
      com.fasterxml.jackson.core.JsonProcessingException - If an error occurs during JSON processing.
    • finishBioAuthRegistration

      public void finishBioAuthRegistration(String username, BioAuthRequest credential) throws IOException, com.yubico.webauthn.exception.RegistrationFailedException, com.fasterxml.jackson.core.JsonProcessingException
      Finishes the biometric authentication registration process.
      Parameters:
      username - The username of the user.
      credential - The biometric authentication credential.
      Throws:
      IOException - If an I/O error occurs.
      com.yubico.webauthn.exception.RegistrationFailedException - If the registration fails.
      com.fasterxml.jackson.core.JsonProcessingException - If an error occurs during JSON processing.
    • constructCredRequest

      public String constructCredRequest(String username) throws com.fasterxml.jackson.core.JsonProcessingException
      Constructs a biometric authentication credential request for the specified username.
      Parameters:
      username - The username of the user.
      Returns:
      The JSON representation of the credential request.
      Throws:
      com.fasterxml.jackson.core.JsonProcessingException - If an error occurs during JSON processing.
    • finishBioAuthLogin

      public LoginRegisterResponse finishBioAuthLogin(String username, BioAuthRequest credential) throws IOException, com.yubico.webauthn.exception.AssertionFailedException, org.springframework.security.authentication.BadCredentialsException, AssertionRequestNotFoundException
      Finishes the biometric authentication login process.
      Parameters:
      username - The username of the user.
      credential - The biometric authentication credential.
      Returns:
      The response containing user information and JWT tokens.
      Throws:
      IOException - If an I/O error occurs.
      com.yubico.webauthn.exception.AssertionFailedException - If the assertion fails.
      org.springframework.security.authentication.BadCredentialsException - If the biometric authentication fails.
      AssertionRequestNotFoundException - If the assertion request is not found.