Search in sources :

Example 1 with AuthenticationException

use of com.microsoft.aad.adal4j.AuthenticationException in project autorest-clientruntime-for-java by Azure.

the class DelegatedTokenCredentials method acquireNewAccessToken.

AuthenticationResult acquireNewAccessToken(String resource) throws IOException {
    if (authorizationCode == null) {
        throw new IllegalArgumentException("You must acquire an authorization code by redirecting to the authentication URL");
    }
    String authorityUrl = this.environment().activeDirectoryEndpoint() + this.domain();
    ExecutorService executor = Executors.newSingleThreadExecutor();
    AuthenticationContext context = new AuthenticationContext(authorityUrl, false, executor);
    if (proxy() != null) {
        context.setProxy(proxy());
    }
    try {
        if (applicationCredentials.clientSecret() != null) {
            return context.acquireTokenByAuthorizationCode(authorizationCode, new URI(redirectUrl), new ClientCredential(applicationCredentials.clientId(), applicationCredentials.clientSecret()), resource, null).get();
        } else if (applicationCredentials.clientCertificate() != null && applicationCredentials.clientCertificatePassword() != null) {
            return context.acquireTokenByAuthorizationCode(authorizationCode, new URI(redirectUrl), AsymmetricKeyCredential.create(applicationCredentials.clientId(), new ByteArrayInputStream(applicationCredentials.clientCertificate()), applicationCredentials.clientCertificatePassword()), resource, null).get();
        } else if (applicationCredentials.clientCertificate() != null) {
            return context.acquireTokenByAuthorizationCode(authorizationCode, new URI(redirectUrl), AsymmetricKeyCredential.create(clientId(), ApplicationTokenCredentials.privateKeyFromPem(new String(applicationCredentials.clientCertificate())), ApplicationTokenCredentials.publicKeyFromPem(new String(applicationCredentials.clientCertificate()))), resource, null).get();
        }
        throw new AuthenticationException("Please provide either a non-null secret or a non-null certificate.");
    } catch (Exception e) {
        throw new IOException(e.getMessage(), e);
    } finally {
        executor.shutdown();
    }
}
Also used : ClientCredential(com.microsoft.aad.adal4j.ClientCredential) AuthenticationContext(com.microsoft.aad.adal4j.AuthenticationContext) ByteArrayInputStream(java.io.ByteArrayInputStream) AuthenticationException(com.microsoft.aad.adal4j.AuthenticationException) ExecutorService(java.util.concurrent.ExecutorService) IOException(java.io.IOException) URI(java.net.URI) AuthenticationException(com.microsoft.aad.adal4j.AuthenticationException) IOException(java.io.IOException)

Example 2 with AuthenticationException

use of com.microsoft.aad.adal4j.AuthenticationException in project autorest-clientruntime-for-java by Azure.

the class ApplicationTokenCredentials method acquireAccessToken.

Future<AuthenticationResult> acquireAccessToken(String resource, ExecutorService executor) throws IOException {
    String authorityUrl = this.environment().activeDirectoryEndpoint() + this.domain();
    AuthenticationContext context = new AuthenticationContext(authorityUrl, false, executor);
    if (proxy() != null) {
        context.setProxy(proxy());
    }
    if (sslSocketFactory() != null) {
        context.setSslSocketFactory(sslSocketFactory());
    }
    try {
        if (clientSecret() != null) {
            return context.acquireToken(resource, new ClientCredential(clientId(), clientSecret()), null);
        } else if (clientCertificate() != null && clientCertificatePassword() != null) {
            return context.acquireToken(resource, AsymmetricKeyCredential.create(clientId(), new ByteArrayInputStream(clientCertificate()), clientCertificatePassword()), null);
        } else if (clientCertificate() != null) {
            return context.acquireToken(resource, AsymmetricKeyCredential.create(clientId(), privateKeyFromPem(new String(clientCertificate())), publicKeyFromPem(new String(clientCertificate()))), null);
        }
        throw new AuthenticationException("Please provide either a non-null secret or a non-null certificate.");
    } catch (Exception e) {
        throw new IOException(e.getMessage(), e);
    }
}
Also used : ClientCredential(com.microsoft.aad.adal4j.ClientCredential) AuthenticationContext(com.microsoft.aad.adal4j.AuthenticationContext) ByteArrayInputStream(java.io.ByteArrayInputStream) AuthenticationException(com.microsoft.aad.adal4j.AuthenticationException) IOException(java.io.IOException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) AuthenticationException(com.microsoft.aad.adal4j.AuthenticationException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 3 with AuthenticationException

use of com.microsoft.aad.adal4j.AuthenticationException in project autorest-clientruntime-for-java by Azure.

the class ApplicationTokenCredentials method acquireAccessToken.

private AuthenticationResult acquireAccessToken(String resource) throws IOException {
    String authorityUrl = this.environment().activeDirectoryEndpoint() + this.domain();
    ExecutorService executor = Executors.newSingleThreadExecutor();
    AuthenticationContext context = new AuthenticationContext(authorityUrl, false, executor);
    if (proxy() != null) {
        context.setProxy(proxy());
    }
    if (sslSocketFactory() != null) {
        context.setSslSocketFactory(sslSocketFactory());
    }
    try {
        if (clientSecret != null) {
            return context.acquireToken(resource, new ClientCredential(this.clientId(), clientSecret), null).get();
        } else if (clientCertificate != null && clientCertificatePassword != null) {
            return context.acquireToken(resource, AsymmetricKeyCredential.create(clientId, new ByteArrayInputStream(clientCertificate), clientCertificatePassword), null).get();
        } else if (clientCertificate != null) {
            return context.acquireToken(resource, AsymmetricKeyCredential.create(clientId(), privateKeyFromPem(new String(clientCertificate)), publicKeyFromPem(new String(clientCertificate))), null).get();
        }
        throw new AuthenticationException("Please provide either a non-null secret or a non-null certificate.");
    } catch (Exception e) {
        throw new IOException(e.getMessage(), e);
    } finally {
        executor.shutdown();
    }
}
Also used : ClientCredential(com.microsoft.aad.adal4j.ClientCredential) AuthenticationContext(com.microsoft.aad.adal4j.AuthenticationContext) ByteArrayInputStream(java.io.ByteArrayInputStream) AuthenticationException(com.microsoft.aad.adal4j.AuthenticationException) ExecutorService(java.util.concurrent.ExecutorService) IOException(java.io.IOException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) AuthenticationException(com.microsoft.aad.adal4j.AuthenticationException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 4 with AuthenticationException

use of com.microsoft.aad.adal4j.AuthenticationException in project mssql-jdbc by Microsoft.

the class SQLServerADAL4JUtils method getSqlFedAuthTokenIntegrated.

static SqlFedAuthToken getSqlFedAuthTokenIntegrated(SqlFedAuthInfo fedAuthInfo, String authenticationString) throws SQLServerException {
    ExecutorService executorService = Executors.newFixedThreadPool(1);
    try {
        // principal name does not matter, what matters is the realm name
        // it gets the username in principal_name@realm_name format
        KerberosPrincipal kerberosPrincipal = new KerberosPrincipal("username");
        String username = kerberosPrincipal.getName();
        if (adal4jLogger.isLoggable(Level.FINE)) {
            adal4jLogger.fine(adal4jLogger.toString() + " realm name is:" + kerberosPrincipal.getRealm());
        }
        AuthenticationContext context = new AuthenticationContext(fedAuthInfo.stsurl, false, executorService);
        Future<AuthenticationResult> future = context.acquireToken(fedAuthInfo.spn, ActiveDirectoryAuthentication.JDBC_FEDAUTH_CLIENT_ID, username, null, null);
        AuthenticationResult authenticationResult = future.get();
        SqlFedAuthToken fedAuthToken = new SqlFedAuthToken(authenticationResult.getAccessToken(), authenticationResult.getExpiresOnDate());
        return fedAuthToken;
    } catch (InterruptedException | IOException e) {
        throw new SQLServerException(e.getMessage(), e);
    } catch (ExecutionException e) {
        MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_ADALExecution"));
        Object[] msgArgs = { "", authenticationString };
        if (null == e.getCause() || null == e.getCause().getMessage()) {
            // the case when Future's outcome has no AuthenticationResult but exception
            throw new SQLServerException(form.format(msgArgs), null);
        } else {
            // the cause error message uses \\n\\r which does not give correct format
            // change it to \r\n to provide correct format
            String correctedErrorMessage = e.getCause().getMessage().replaceAll("\\\\r\\\\n", "\r\n");
            AuthenticationException correctedAuthenticationException = new AuthenticationException(correctedErrorMessage);
            // SQLServerException is caused by ExecutionException, which is caused by
            // AuthenticationException
            // to match the exception tree before error message correction
            ExecutionException correctedExecutionException = new ExecutionException(correctedAuthenticationException);
            throw new SQLServerException(form.format(msgArgs), null, 0, correctedExecutionException);
        }
    } finally {
        executorService.shutdown();
    }
}
Also used : KerberosPrincipal(javax.security.auth.kerberos.KerberosPrincipal) AuthenticationContext(com.microsoft.aad.adal4j.AuthenticationContext) MessageFormat(java.text.MessageFormat) AuthenticationException(com.microsoft.aad.adal4j.AuthenticationException) IOException(java.io.IOException) AuthenticationResult(com.microsoft.aad.adal4j.AuthenticationResult) ExecutorService(java.util.concurrent.ExecutorService) ExecutionException(java.util.concurrent.ExecutionException)

Example 5 with AuthenticationException

use of com.microsoft.aad.adal4j.AuthenticationException in project mssql-jdbc by Microsoft.

the class SQLServerADAL4JUtils method getSqlFedAuthToken.

static SqlFedAuthToken getSqlFedAuthToken(SqlFedAuthInfo fedAuthInfo, String user, String password, String authenticationString) throws SQLServerException {
    ExecutorService executorService = Executors.newFixedThreadPool(1);
    try {
        AuthenticationContext context = new AuthenticationContext(fedAuthInfo.stsurl, false, executorService);
        Future<AuthenticationResult> future = context.acquireToken(fedAuthInfo.spn, ActiveDirectoryAuthentication.JDBC_FEDAUTH_CLIENT_ID, user, password, null);
        AuthenticationResult authenticationResult = future.get();
        SqlFedAuthToken fedAuthToken = new SqlFedAuthToken(authenticationResult.getAccessToken(), authenticationResult.getExpiresOnDate());
        return fedAuthToken;
    } catch (MalformedURLException | InterruptedException e) {
        throw new SQLServerException(e.getMessage(), e);
    } catch (ExecutionException e) {
        MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_ADALExecution"));
        Object[] msgArgs = { user, authenticationString };
        // the cause error message uses \\n\\r which does not give correct format
        // change it to \r\n to provide correct format
        String correctedErrorMessage = e.getCause().getMessage().replaceAll("\\\\r\\\\n", "\r\n");
        AuthenticationException correctedAuthenticationException = new AuthenticationException(correctedErrorMessage);
        // SQLServerException is caused by ExecutionException, which is caused by
        // AuthenticationException
        // to match the exception tree before error message correction
        ExecutionException correctedExecutionException = new ExecutionException(correctedAuthenticationException);
        throw new SQLServerException(form.format(msgArgs), null, 0, correctedExecutionException);
    } finally {
        executorService.shutdown();
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) AuthenticationContext(com.microsoft.aad.adal4j.AuthenticationContext) MessageFormat(java.text.MessageFormat) AuthenticationException(com.microsoft.aad.adal4j.AuthenticationException) AuthenticationResult(com.microsoft.aad.adal4j.AuthenticationResult) ExecutorService(java.util.concurrent.ExecutorService) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

AuthenticationContext (com.microsoft.aad.adal4j.AuthenticationContext)5 AuthenticationException (com.microsoft.aad.adal4j.AuthenticationException)5 IOException (java.io.IOException)4 ExecutorService (java.util.concurrent.ExecutorService)4 ClientCredential (com.microsoft.aad.adal4j.ClientCredential)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 AuthenticationResult (com.microsoft.aad.adal4j.AuthenticationResult)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 CertificateException (java.security.cert.CertificateException)2 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)2 MessageFormat (java.text.MessageFormat)2 ExecutionException (java.util.concurrent.ExecutionException)2 MalformedURLException (java.net.MalformedURLException)1 URI (java.net.URI)1 KerberosPrincipal (javax.security.auth.kerberos.KerberosPrincipal)1