Search in sources :

Example 1 with ClientCredential

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

use of com.microsoft.aad.adal4j.ClientCredential 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)2 AuthenticationResult (com.microsoft.aad.adal4j.AuthenticationResult)2 ClientCredential (com.microsoft.aad.adal4j.ClientCredential)2 ExecutorService (java.util.concurrent.ExecutorService)2