Class AuthServiceImpl

java.lang.Object
org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService
com.alpaca.service.impl.AuthServiceImpl
All Implemented Interfaces:
IAuthService, org.springframework.security.core.userdetails.UserDetailsService, org.springframework.security.oauth2.client.userinfo.OAuth2UserService<org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest,org.springframework.security.oauth2.core.user.OAuth2User>

@Service public class AuthServiceImpl extends org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService implements IAuthService
Implementation of IAuthService, handling authentication, user registration, and OAuth2 login flows within a Spring Security context.

Extends DefaultOAuth2UserService to support custom OAuth2 user processing, JWT token generation, and security context management.

See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final JJwtManager
     
    private final PasswordManager
     
    private final IProfileService
     
    private final IRoleService
     
    private final IUserService
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    org.springframework.security.core.Authentication
    authenticate(String username, String password)
    Performs authentication using Spring's authentication token model.
    checkExistingUser(User user, boolean emailVerified)
    Updates an existing user's OAuth2 connection status and email verification flag.
    org.springframework.security.oauth2.core.user.OAuth2User
    loadUser(org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest userRequest)
    Handles OAuth2 login by processing the provider's user data and delegating logic.
    org.springframework.security.core.userdetails.UserDetails
    Loads a user by username (email), required by Spring Security.
    login(String email, String password)
    Authenticates a user using email and password and returns a JWT token wrapped in DTO.
    org.springframework.security.oauth2.core.user.OAuth2User
    processOAuth2User(org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest request, org.springframework.security.oauth2.core.user.OAuth2User user)
    Processes OAuth2 user data: registers or connects existing user based on attributes.
    register(String email, String password)
    Registers a new user, assigns default role, and logs them in returning a JWT token.
    registerOrLoginOAuth2(String email, String firstName, String lastName, String imageURL, boolean emailVerified, Map<String,Object> attributes)
    Registers or logs in a user based on OAuth2 information.
    registerProfile(User user, String firstName, String lastName, String imageURL)
    Creates or updates a user's profile.
    setSecurityContextBefore(org.springframework.security.core.Authentication authentication)
    Sets the Spring Security context with the provided authentication object.
    org.springframework.security.core.userdetails.UserDetails
    validateUserDetails(String rawPassword, org.springframework.security.core.userdetails.UserDetails userDetails)
    Validates raw password against stored user details and enforces account status checks.

    Methods inherited from class org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService

    setAttributesConverter, setRequestEntityConverter, setRestOperations

    Methods inherited from class java.lang.Object

    clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • AuthServiceImpl

      public AuthServiceImpl()
  • Method Details

    • setSecurityContextBefore

      public Object setSecurityContextBefore(org.springframework.security.core.Authentication authentication)
      Sets the Spring Security context with the provided authentication object.
      Parameters:
      authentication - the authentication object; must not be null
      Returns:
      the authenticated user's principal
      Throws:
      UnauthorizedException - if the authentication object is null
    • login

      public AuthResponseDTO login(String email, String password)
      Authenticates a user using email and password and returns a JWT token wrapped in DTO.
      Specified by:
      login in interface IAuthService
      Parameters:
      email - user's email
      password - raw password
      Returns:
      AuthResponseDTO containing the JWT token
      Throws:
      NotFoundException - if the email is not registered
    • register

      public AuthResponseDTO register(String email, String password)
      Registers a new user, assigns default role, and logs them in returning a JWT token.
      Specified by:
      register in interface IAuthService
      Parameters:
      email - new user's email
      password - raw password
      Returns:
      AuthResponseDTO for the newly registered user
      Throws:
      BadRequestException - if the email is already registered
    • loadUserByUsername

      public org.springframework.security.core.userdetails.UserDetails loadUserByUsername(String username)
      Loads a user by username (email), required by Spring Security.
      Specified by:
      loadUserByUsername in interface org.springframework.security.core.userdetails.UserDetailsService
      Parameters:
      username - user's email
      Returns:
      UserDetails for authentication
      Throws:
      org.springframework.security.core.userdetails.UsernameNotFoundException - if user is not found
    • validateUserDetails

      public org.springframework.security.core.userdetails.UserDetails validateUserDetails(String rawPassword, org.springframework.security.core.userdetails.UserDetails userDetails)
      Validates raw password against stored user details and enforces account status checks.
      Parameters:
      rawPassword - the entered password
      userDetails - stored user details
      Returns:
      authenticated UserDetails
      Throws:
      BadRequestException - if validation fails
      UnauthorizedException - if account is disabled or locked
    • authenticate

      public org.springframework.security.core.Authentication authenticate(String username, String password)
      Performs authentication using Spring's authentication token model.
      Parameters:
      username - user's email
      password - raw password
      Returns:
      an Authentication object
    • loadUser

      public org.springframework.security.oauth2.core.user.OAuth2User loadUser(org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest userRequest)
      Handles OAuth2 login by processing the provider's user data and delegating logic.
      Specified by:
      loadUser in interface org.springframework.security.oauth2.client.userinfo.OAuth2UserService<org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest,org.springframework.security.oauth2.core.user.OAuth2User>
      Overrides:
      loadUser in class org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService
      Parameters:
      userRequest - the OAuth2 user request
      Returns:
      an authenticated OAuth2User
    • processOAuth2User

      public org.springframework.security.oauth2.core.user.OAuth2User processOAuth2User(org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest request, org.springframework.security.oauth2.core.user.OAuth2User user)
      Processes OAuth2 user data: registers or connects existing user based on attributes.
      Parameters:
      request - the OAuth2 user request
      user - the authenticated OAuth2 user
      Returns:
      an OAuth2User
      Throws:
      OAuth2AuthenticationProcessingException - if required attributes are missing
    • registerOrLoginOAuth2

      public UserPrincipal registerOrLoginOAuth2(String email, String firstName, String lastName, String imageURL, boolean emailVerified, Map<String,Object> attributes)
      Registers or logs in a user based on OAuth2 information. Sets up profile as needed.
      Parameters:
      email - user's email
      firstName - user's first name
      lastName - user's last name
      imageURL - profile image URL
      emailVerified - email verification status
      attributes - other OAuth2 attributes
      Returns:
      a new or existing UserPrincipal
      Throws:
      BadRequestException - if required fields are missing
    • registerProfile

      public User registerProfile(User user, String firstName, String lastName, String imageURL)
      Creates or updates a user's profile.
      Parameters:
      user - the user entity
      firstName - user’s first name
      lastName - user’s last name
      imageURL - user's avatar URL
      Returns:
      updated User with profile
      Throws:
      BadRequestException - if the user or mandatory fields are invalid
    • checkExistingUser

      public User checkExistingUser(User user, boolean emailVerified)
      Updates an existing user's OAuth2 connection status and email verification flag.
      Parameters:
      user - the existing user
      emailVerified - OAuth2-provided email verification status
      Returns:
      updated User
      Throws:
      UnauthorizedException - if the user account is disabled or locked