Search in sources :

Example 1 with AuthenticationContext

use of com.microsoft.aad.adal4j.AuthenticationContext in project tdi-studio-se by Talend.

the class DynamicsCRMClient method getAccessToken.

protected AuthenticationResult getAccessToken() throws ServiceUnavailableException {
    AuthenticationContext context = null;
    AuthenticationResult result = null;
    ExecutorService service = null;
    try {
        service = Executors.newFixedThreadPool(1);
        context = new AuthenticationContext(clientConfiguration.getAuthoryEndpoint(), false, service);
        Proxy proxy = getProxy();
        if (proxy != null) {
            context.setProxy(proxy);
        }
        Future<AuthenticationResult> future = context.acquireToken(clientConfiguration.getResource(), clientConfiguration.getClientId(), clientConfiguration.getUserName(), clientConfiguration.getPassword(), null);
        result = future.get();
    } catch (Exception e) {
        throw new ServiceUnavailableException(e.getMessage());
    } finally {
        service.shutdown();
    }
    if (result == null) {
        throw new ServiceUnavailableException("Authenticated failed! Please check your configuration!");
    }
    return result;
}
Also used : Proxy(java.net.Proxy) AuthenticationContext(com.microsoft.aad.adal4j.AuthenticationContext) ExecutorService(java.util.concurrent.ExecutorService) ServiceUnavailableException(javax.naming.ServiceUnavailableException) URISyntaxException(java.net.URISyntaxException) ServiceUnavailableException(javax.naming.ServiceUnavailableException) ODataClientErrorException(org.apache.olingo.client.api.communication.ODataClientErrorException) HttpClientException(org.apache.olingo.client.api.http.HttpClientException) AuthenticationResult(com.microsoft.aad.adal4j.AuthenticationResult)

Example 2 with AuthenticationContext

use of com.microsoft.aad.adal4j.AuthenticationContext in project azure-sdk-for-java by Azure.

the class KeyVaultClientIntegrationTestBase method getAccessToken.

private static AuthenticationResult getAccessToken(String authorization, String resource) throws Exception {
    String clientId = System.getenv("arm.clientid");
    if (clientId == null) {
        throw new Exception("Please inform arm.clientid in the environment settings.");
    }
    String clientKey = System.getenv("arm.clientkey");
    String username = System.getenv("arm.username");
    String password = System.getenv("arm.password");
    AuthenticationResult result = null;
    ExecutorService service = null;
    try {
        service = Executors.newFixedThreadPool(1);
        AuthenticationContext context = new AuthenticationContext(authorization, false, service);
        Future<AuthenticationResult> future = null;
        if (clientKey != null && password == null) {
            ClientCredential credentials = new ClientCredential(clientId, clientKey);
            future = context.acquireToken(resource, credentials, null);
        }
        if (password != null && clientKey == null) {
            future = context.acquireToken(resource, clientId, username, password, null);
        }
        if (future == null) {
            throw new Exception("Missing or ambiguous credentials - please inform exactly one of arm.clientkey or arm.password in the environment settings.");
        }
        result = future.get();
    } finally {
        service.shutdown();
    }
    if (result == null) {
        throw new RuntimeException("authentication result was null");
    }
    return result;
}
Also used : ClientCredential(com.microsoft.aad.adal4j.ClientCredential) AuthenticationContext(com.microsoft.aad.adal4j.AuthenticationContext) ExecutorService(java.util.concurrent.ExecutorService) AuthenticationResult(com.microsoft.aad.adal4j.AuthenticationResult)

Example 3 with AuthenticationContext

use of com.microsoft.aad.adal4j.AuthenticationContext in project azure-sdk-for-java by Azure.

the class KeyVaultClientIntegrationTestBase method getAccessToken.

private static AuthenticationResult getAccessToken(String authorization, String resource) throws Exception {
    String clientId = System.getenv("arm.clientid");
    if (clientId == null) {
        throw new Exception("Please inform arm.clientid in the environment settings.");
    }
    String clientKey = System.getenv("arm.clientkey");
    String username = System.getenv("arm.username");
    String password = System.getenv("arm.password");
    AuthenticationResult result = null;
    ExecutorService service = null;
    try {
        service = Executors.newFixedThreadPool(1);
        AuthenticationContext context = new AuthenticationContext(authorization, false, service);
        Future<AuthenticationResult> future = null;
        if (clientKey != null && password == null) {
            ClientCredential credentials = new ClientCredential(clientId, clientKey);
            future = context.acquireToken(resource, credentials, null);
        }
        if (password != null && clientKey == null) {
            future = context.acquireToken(resource, clientId, username, password, null);
        }
        if (future == null) {
            throw new Exception("Missing or ambiguous credentials - please inform exactly one of arm.clientkey or arm.password in the environment settings.");
        }
        result = future.get();
    } finally {
        service.shutdown();
    }
    if (result == null) {
        throw new RuntimeException("authentication result was null");
    }
    return result;
}
Also used : ClientCredential(com.microsoft.aad.adal4j.ClientCredential) AuthenticationContext(com.microsoft.aad.adal4j.AuthenticationContext) ExecutorService(java.util.concurrent.ExecutorService) AuthenticationResult(com.microsoft.aad.adal4j.AuthenticationResult)

Aggregations

AuthenticationContext (com.microsoft.aad.adal4j.AuthenticationContext)3 AuthenticationResult (com.microsoft.aad.adal4j.AuthenticationResult)3 ExecutorService (java.util.concurrent.ExecutorService)3 ClientCredential (com.microsoft.aad.adal4j.ClientCredential)2 Proxy (java.net.Proxy)1 URISyntaxException (java.net.URISyntaxException)1 ServiceUnavailableException (javax.naming.ServiceUnavailableException)1 ODataClientErrorException (org.apache.olingo.client.api.communication.ODataClientErrorException)1 HttpClientException (org.apache.olingo.client.api.http.HttpClientException)1