All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.smartcosmos.extension.stormpath.service.AuthenticateUserServiceStormpath Maven / Gradle / Ivy

The newest version!
package net.smartcosmos.extension.stormpath.service;

import com.stormpath.sdk.error.Error;
import com.stormpath.sdk.resource.ResourceException;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Service;

import net.smartcosmos.userdetails.domain.UserDetails;
import net.smartcosmos.userdetails.domain.rest.AuthenticateRequest;
import net.smartcosmos.userdetails.service.AuthenticateUserService;
import net.smartcosmos.userdetails.service.UserDetailsService;

import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;

import static net.smartcosmos.domain.rest.ErrorResponse.CODE_ERROR;
import static net.smartcosmos.userdetails.util.ResponseEntityFactory.errorResponse;
import static net.smartcosmos.userdetails.util.ResponseEntityFactory.invalidDataReturned;
import static net.smartcosmos.userdetails.util.ResponseEntityFactory.invalidUsernameOrPassword;
import static net.smartcosmos.userdetails.util.ResponseEntityFactory.success;

/**
 * Stormpath-specific implementation of the {@link AuthenticateUserService} interface.
 */
@Slf4j
@Service
public class AuthenticateUserServiceStormpath implements AuthenticateUserService {

    private final UserDetailsService stormpathService;

    @Autowired
    public AuthenticateUserServiceStormpath(UserDetailsService stormpathService) {

        this.stormpathService = stormpathService;
    }

    @Override
    public ResponseEntity authenticateUser(AuthenticateRequest request) {

        log.debug("Requested information on username {} with {}", request.getName(), request);

        try {
            UserDetails entity = stormpathService.getUserDetails(request.getName(), request.getCredentials());

            if (stormpathService.isValid(entity)) {
                log.info("Validation of authentication response for user {} : valid", request.getName());
                return success(entity);
            }
            log.info("Validation of authentication response for user {} : invalid", request.getName());
            return invalidDataReturned();
        } catch (AuthenticationServiceException e) {
            log.info("Authenticating user {} failed. Request was {}. Exception: {}", request.getName(), request, e.toString());
            return errorResponse(INTERNAL_SERVER_ERROR, CODE_ERROR, e.getMessage());
        } catch (AuthenticationException e) {
            log.info("Authenticating user {} failed. Request was {}. Exception: {}", request.getName(), request, e.toString());
            if (e.getCause() instanceof ResourceException) {
                Error stormpathError = ((ResourceException) e.getCause()).getStormpathError();
                log.debug("Stormpath returned error: {}", stormpathError);
                return invalidUsernameOrPassword();
            }

            return errorResponse(BAD_REQUEST, CODE_ERROR, e.getMessage());
        }
    }

    @Override
    public ResponseEntity isUserActive(String username) {

        log.debug("Requested information on username {} ", username);

        try {
            UserDetails userDetails = stormpathService.getUserDetails(username);

            if (stormpathService.isValid(userDetails)) {
                log.info("Validation of authentication response for user {} : valid", username);
                return success(userDetails);
            }

            log.info("Validation of authentication response for user {} : invalid", username);
            return invalidDataReturned();
        } catch (AuthenticationServiceException e) {
            log.info("Authenticating user {} failed. Exception: {}", username, e.toString());
            return errorResponse(INTERNAL_SERVER_ERROR, CODE_ERROR, e.getMessage());
        } catch (AuthenticationException e) {
            log.info("Authenticating user {} failed (no longer active?). Exception: {}", username, e.toString());
            return invalidUsernameOrPassword();
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy