Search in sources :

Example 1 with CertificateAuthenticationRequest

use of org.platformlayer.auth.CertificateAuthenticationRequest in project platformlayer by platformlayer.

the class LoginService method authenticate.

public AuthenticateResponse authenticate(HttpServletRequest httpRequest, AuthenticateRequest request) {
    AuthenticateResponse response = new AuthenticateResponse();
    String username = null;
    UserEntity user = null;
    if (request.auth.passwordCredentials != null) {
        username = request.auth.passwordCredentials.username;
        String password = request.auth.passwordCredentials.password;
        try {
            user = userAuthenticator.authenticate(username, password);
        } catch (AuthenticatorException e) {
            // An exception indicates something went wrong (i.e. not just
            // bad credentials)
            log.warn("Error while getting user info", e);
            throw new IllegalStateException("Error while getting user info", e);
        }
    } else if (request.auth.certificateCredentials != null) {
        username = request.auth.certificateCredentials.username;
        X509Certificate[] certificateChain = HttpUtils.getCertificateChain(httpRequest);
        if (certificateChain == null) {
            return null;
        }
        byte[] challengeResponse = request.auth.certificateCredentials.challengeResponse;
        CertificateAuthenticationRequest details = new CertificateAuthenticationRequest();
        details.certificateChain = certificateChain;
        details.username = username;
        // details.projectKey = projectKey;
        details.challengeResponse = challengeResponse;
        CertificateAuthenticationResponse result = null;
        try {
            result = userAuthenticator.authenticate(details);
        } catch (AuthenticatorException e) {
            log.warn("Error while authenticating by certificate", e);
            throw new IllegalStateException("Error while authenticating by certificate", e);
        }
        if (result == null) {
            return null;
        }
        if (challengeResponse != null) {
            if (result.user == null) {
                return null;
            }
            user = (UserEntity) result.user;
        } else {
            log.debug("Returning authentication challenge for user: " + username);
            response.challenge = result.challenge;
            return response;
        }
    } else {
        return null;
    }
    if (user == null) {
        log.debug("Authentication request failed.  Username=" + username);
        return null;
    }
    log.debug("Successful authentication for user: " + user.key);
    response.access = tokenHelpers.buildAccess(user);
    return response;
}
Also used : AuthenticateResponse(org.platformlayer.auth.model.AuthenticateResponse) CertificateAuthenticationResponse(org.platformlayer.auth.CertificateAuthenticationResponse) CertificateAuthenticationRequest(org.platformlayer.auth.CertificateAuthenticationRequest) AuthenticatorException(org.platformlayer.auth.AuthenticatorException) UserEntity(org.platformlayer.auth.UserEntity)

Aggregations

AuthenticatorException (org.platformlayer.auth.AuthenticatorException)1 CertificateAuthenticationRequest (org.platformlayer.auth.CertificateAuthenticationRequest)1 CertificateAuthenticationResponse (org.platformlayer.auth.CertificateAuthenticationResponse)1 UserEntity (org.platformlayer.auth.UserEntity)1 AuthenticateResponse (org.platformlayer.auth.model.AuthenticateResponse)1