Search in sources :

Example 1 with Auth

use of org.platformlayer.auth.v1.Auth in project platformlayer by platformlayer.

the class PlatformLayerAuthenticationClient method authenticateWithCertificate.

public PlatformlayerAuthenticationToken authenticateWithCertificate(String username, X509Certificate[] certificateChain, PrivateKey privateKey) throws PlatformlayerAuthenticationClientException {
    if (username == null) {
        throw new IllegalArgumentException();
    }
    CertificateCredentials certificateCredentials = new CertificateCredentials();
    certificateCredentials.setUsername(username);
    Auth auth = new Auth();
    auth.setCertificateCredentials(certificateCredentials);
    AuthenticateRequest request = new AuthenticateRequest();
    request.setAuth(auth);
    final KeyManager keyManager = new SimpleClientCertificateKeyManager(privateKey, certificateChain);
    for (int i = 0; i < 2; i++) {
        AuthenticateResponse response;
        try {
            RestfulRequest<AuthenticateResponse> httpRequest = httpClient.buildRequest(HttpMethod.POST, "api/tokens", HttpPayload.asXml(request), AuthenticateResponse.class);
            httpRequest.setKeyManager(keyManager);
            response = httpRequest.execute();
        } catch (RestClientException e) {
            throw new PlatformlayerAuthenticationClientException("Error authenticating", e);
        }
        if (i == 0) {
            if (response == null || response.getChallenge() == null) {
                return null;
            }
            byte[] challenge = response.getChallenge();
            byte[] challengeResponse = decrypt(privateKey, challenge);
            certificateCredentials.setChallengeResponse(challengeResponse);
        } else {
            if (response == null || response.getAccess() == null) {
                return null;
            }
            return new PlatformlayerAuthenticationToken(response.getAccess());
        }
    }
    return null;
}
Also used : SimpleClientCertificateKeyManager(com.fathomdb.crypto.SimpleClientCertificateKeyManager) AuthenticateResponse(org.platformlayer.auth.v1.AuthenticateResponse) PlatformlayerAuthenticationToken(org.platformlayer.auth.PlatformlayerAuthenticationToken) PlatformlayerAuthenticationClientException(org.platformlayer.auth.PlatformlayerAuthenticationClientException) AuthenticateRequest(org.platformlayer.auth.v1.AuthenticateRequest) CertificateCredentials(org.platformlayer.auth.v1.CertificateCredentials) Auth(org.platformlayer.auth.v1.Auth) RestClientException(org.platformlayer.rest.RestClientException) SimpleClientCertificateKeyManager(com.fathomdb.crypto.SimpleClientCertificateKeyManager) KeyManager(javax.net.ssl.KeyManager)

Example 2 with Auth

use of org.platformlayer.auth.v1.Auth in project platformlayer by platformlayer.

the class PlatformLayerAuthAdminClient method signCsr.

// This can actually be moved to the user-auth system
public List<X509Certificate> signCsr(String projectKey, CryptoKey projectSecret, String csr) {
    String url = "pki/csr";
    SignCertificateRequest request = new SignCertificateRequest();
    request.setProject(projectKey);
    request.setCsr(csr);
    request.setProjectSecret(FathomdbCrypto.serialize(projectSecret));
    try {
        SignCertificateResponse response = doSimpleXmlRequest(HttpMethod.POST, url, request, SignCertificateResponse.class);
        List<X509Certificate> certificates = Lists.newArrayList();
        for (String cert : response.getCertificates()) {
            certificates.addAll(CertificateUtils.fromPem(cert));
        }
        return certificates;
    } catch (RestClientException e) {
        throw new IllegalArgumentException("Error while signing certificate", e);
    }
}
Also used : SignCertificateRequest(org.platformlayer.auth.v1.SignCertificateRequest) RestClientException(org.platformlayer.rest.RestClientException) X509Certificate(java.security.cert.X509Certificate) SignCertificateResponse(org.platformlayer.auth.v1.SignCertificateResponse)

Example 3 with Auth

use of org.platformlayer.auth.v1.Auth in project sonarqube by SonarSource.

the class SamlIdentityProvider method init.

@Override
public void init(InitContext context) {
    try {
        Auth auth = newAuth(initSettings(context.getCallbackUrl()), context.getRequest(), context.getResponse());
        auth.login(context.generateCsrfState());
    } catch (IOException | SettingsException e) {
        throw new IllegalStateException("Fail to intialize SAML authentication plugin", e);
    }
}
Also used : Auth(com.onelogin.saml2.Auth) IOException(java.io.IOException) SettingsException(com.onelogin.saml2.exception.SettingsException)

Example 4 with Auth

use of org.platformlayer.auth.v1.Auth in project platformlayer by platformlayer.

the class PlatformLayerAuthenticationClient method authenticate.

public AuthenticateResponse authenticate(PasswordCredentials passwordCredentials) throws PlatformlayerAuthenticationClientException {
    Auth auth = new Auth();
    auth.setPasswordCredentials(passwordCredentials);
    AuthenticateRequest request = new AuthenticateRequest();
    request.setAuth(auth);
    AuthenticateResponse response;
    try {
        response = doSimpleXmlRequest(HttpMethod.POST, "api/tokens", request, AuthenticateResponse.class);
    } catch (RestClientException e) {
        Integer httpResponseCode = e.getHttpResponseCode();
        if (httpResponseCode != null && httpResponseCode == 401) {
            throw new PlatformlayerInvalidCredentialsException("Invalid credentials");
        }
        throw new PlatformlayerAuthenticationClientException("Error authenticating", e);
    }
    return response;
}
Also used : AuthenticateResponse(org.platformlayer.auth.v1.AuthenticateResponse) AuthenticateRequest(org.platformlayer.auth.v1.AuthenticateRequest) PlatformlayerInvalidCredentialsException(org.platformlayer.auth.PlatformlayerInvalidCredentialsException) Auth(org.platformlayer.auth.v1.Auth) RestClientException(org.platformlayer.rest.RestClientException) PlatformlayerAuthenticationClientException(org.platformlayer.auth.PlatformlayerAuthenticationClientException)

Example 5 with Auth

use of org.platformlayer.auth.v1.Auth in project platformlayer by platformlayer.

the class PlatformlayerAuthenticationService method authenticateWithPassword.

@Override
public PlatformlayerAuthenticationToken authenticateWithPassword(String username, String password) throws PlatformlayerAuthenticationClientException {
    PasswordCredentials passwordCredentials = new PasswordCredentials();
    passwordCredentials.setUsername(username);
    passwordCredentials.setPassword(password);
    // TODO: Cache auth tokens??
    AuthenticateResponse response = keystoneUserClient.authenticate(passwordCredentials);
    PlatformlayerAuthenticationToken authToken = new PlatformlayerAuthenticationToken(response.getAccess());
    return authToken;
// // TODO: Cache decoded tokens?
// KeystoneAuthentication auth = (KeystoneAuthentication) keystoneSystemClient.validate(
// authToken.getAuthTokenValue(), project);
// if (auth == null) {
// return null;
// }
// 
// return new KeystoneUser(auth);
}
Also used : AuthenticateResponse(org.platformlayer.auth.v1.AuthenticateResponse) PasswordCredentials(org.platformlayer.auth.v1.PasswordCredentials) PlatformlayerAuthenticationToken(org.platformlayer.auth.PlatformlayerAuthenticationToken)

Aggregations

AuthenticateResponse (org.platformlayer.auth.v1.AuthenticateResponse)3 RestClientException (org.platformlayer.rest.RestClientException)3 Auth (com.onelogin.saml2.Auth)2 PlatformlayerAuthenticationClientException (org.platformlayer.auth.PlatformlayerAuthenticationClientException)2 PlatformlayerAuthenticationToken (org.platformlayer.auth.PlatformlayerAuthenticationToken)2 Auth (org.platformlayer.auth.v1.Auth)2 AuthenticateRequest (org.platformlayer.auth.v1.AuthenticateRequest)2 SimpleClientCertificateKeyManager (com.fathomdb.crypto.SimpleClientCertificateKeyManager)1 SettingsException (com.onelogin.saml2.exception.SettingsException)1 IOException (java.io.IOException)1 X509Certificate (java.security.cert.X509Certificate)1 KeyManager (javax.net.ssl.KeyManager)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 PlatformlayerInvalidCredentialsException (org.platformlayer.auth.PlatformlayerInvalidCredentialsException)1 CertificateCredentials (org.platformlayer.auth.v1.CertificateCredentials)1 PasswordCredentials (org.platformlayer.auth.v1.PasswordCredentials)1 SignCertificateRequest (org.platformlayer.auth.v1.SignCertificateRequest)1 SignCertificateResponse (org.platformlayer.auth.v1.SignCertificateResponse)1 UserIdentity (org.sonar.api.server.authentication.UserIdentity)1