Search in sources :

Example 1 with AuthenticationResult

use of com.microsoft.aad.adal4j.AuthenticationResult in project crate by crate.

the class AzureComputeServiceImpl method createConfiguration.

private Configuration createConfiguration() {
    Configuration conf = null;
    try {
        AuthenticationResult authRes = AuthHelper.getAccessTokenFromServicePrincipalCredentials(Azure.ENDPOINT, Azure.AUTH_ENDPOINT, tenantId, appId, appSecret);
        DefaultBuilder registry = DefaultBuilder.create();
        AzureModule.registerServices(registry);
        conf = ManagementConfiguration.configure(null, new Configuration(registry), URI.create(Azure.ENDPOINT), subscriptionId, authRes.getAccessToken());
    } catch (Exception e) {
        logger.error("Could not create configuration for Azure clients", e);
    }
    return conf;
}
Also used : ManagementConfiguration(com.microsoft.windowsazure.management.configuration.ManagementConfiguration) Configuration(com.microsoft.windowsazure.Configuration) DefaultBuilder(com.microsoft.windowsazure.core.DefaultBuilder) ElasticsearchException(org.elasticsearch.ElasticsearchException) IOException(java.io.IOException) AuthenticationResult(com.microsoft.aad.adal4j.AuthenticationResult)

Example 2 with AuthenticationResult

use of com.microsoft.aad.adal4j.AuthenticationResult 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 3 with AuthenticationResult

use of com.microsoft.aad.adal4j.AuthenticationResult 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 4 with AuthenticationResult

use of com.microsoft.aad.adal4j.AuthenticationResult 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

AuthenticationResult (com.microsoft.aad.adal4j.AuthenticationResult)4 AuthenticationContext (com.microsoft.aad.adal4j.AuthenticationContext)3 ExecutorService (java.util.concurrent.ExecutorService)3 ClientCredential (com.microsoft.aad.adal4j.ClientCredential)2 Configuration (com.microsoft.windowsazure.Configuration)1 DefaultBuilder (com.microsoft.windowsazure.core.DefaultBuilder)1 ManagementConfiguration (com.microsoft.windowsazure.management.configuration.ManagementConfiguration)1 IOException (java.io.IOException)1 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 ElasticsearchException (org.elasticsearch.ElasticsearchException)1