Java Code Examples for com.microsoft.aad.adal4j.AuthenticationResult#getAccessToken()

The following examples show how to use com.microsoft.aad.adal4j.AuthenticationResult#getAccessToken() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KeyVault.java    From remote-monitoring-services-java with MIT License 6 votes vote down vote up
/**
 * Creates a new KeyVaultCredential based on the access token obtained.
 *
 * @return
 */
private ServiceClientCredentials createCredentials() {
    return new KeyVaultCredentials() {

        //Callback that supplies the token type and access token on request.
        @Override
        public String doAuthenticate(String authorization, String resource, String scope) {

            AuthenticationResult authResult;
            try {
                authResult = getAccessToken(authorization, resource);
                return authResult.getAccessToken();
            } catch (Exception e) {
                // TODO: Add logging
                log.error("Failed to get the saccess token for accessing the keyvault.", e);
                // e.printStackTrace();
            }
            return "";
        }

    };
}
 
Example 2
Source File: KeyVault.java    From remote-monitoring-services-java with MIT License 6 votes vote down vote up
/**
 * Creates a new KeyVaultCredential based on the access token obtained.
 *
 * @return
 */
private ServiceClientCredentials createCredentials() {
    return new KeyVaultCredentials() {

        //Callback that supplies the token type and access token on request.
        @Override
        public String doAuthenticate(String authorization, String resource, String scope) {

            AuthenticationResult authResult;
            try {
                authResult = getAccessToken(authorization, resource);
                return authResult.getAccessToken();
            } catch (Exception e) {
                log.error("Failed to get authentication token for key vault.",e);
                // e.printStackTrace();
            }
            return "";
        }

    };
}
 
Example 3
Source File: KeyVault.java    From remote-monitoring-services-java with MIT License 6 votes vote down vote up
/**
 * Creates a new KeyVaultCredential based on the access token obtained.
 *
 * @return
 */
private ServiceClientCredentials createCredentials() {
    return new KeyVaultCredentials() {

        //Callback that supplies the token type and access token on request.
        @Override
        public String doAuthenticate(String authorization, String resource, String scope) {

            AuthenticationResult authResult;
            try {
                authResult = getAccessToken(authorization, resource);
                return authResult.getAccessToken();
            } catch (Exception e) {
                log.error("Failed to get authentication token for key vault.",e);
                // e.printStackTrace();
            }
            return "";
        }

    };
}
 
Example 4
Source File: KeyVault.java    From remote-monitoring-services-java with MIT License 6 votes vote down vote up
/**
 * Creates a new KeyVaultCredential based on the access token obtained.
 *
 * @return
 */
private ServiceClientCredentials createCredentials() {
    return new KeyVaultCredentials() {

        //Callback that supplies the token type and access token on request.
        @Override
        public String doAuthenticate(String authorization, String resource, String scope) {

            AuthenticationResult authResult;
            try {
                authResult = getAccessToken(authorization, resource);
                return authResult.getAccessToken();
            } catch (Exception e) {
                log.error("Failed to get authentication token for key vault.",e);
                // e.printStackTrace();
            }
            return "";
        }

    };
}
 
Example 5
Source File: EcKeyIntegrationTests.java    From azure-keyvault-java with MIT License 6 votes vote down vote up
private static ServiceClientCredentials createTestCredentials() throws Exception {
    return new KeyVaultCredentials() {

        @Override
        public String doAuthenticate(String authorization, String resource, String scope) {
            try {
                if (isRecordMode()) {
                    AuthenticationResult authResult = getAccessToken(authorization, resource);
                    return authResult.getAccessToken();
                } else {
                    return "";
                }
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }
    };
}
 
Example 6
Source File: KeyVaultClientIntegrationTestBase.java    From azure-keyvault-java with MIT License 6 votes vote down vote up
private static ServiceClientCredentials createTestCredentials() throws Exception {
	return new KeyVaultCredentials() {

		@Override
		public String doAuthenticate(String authorization, String resource, String scope) {
			try {

				if (isRecordMode()) {
					AuthenticationResult authResult = getAccessToken(authorization, resource);
					return authResult.getAccessToken();
				} else {
					return "";
				}

			} catch (Exception ex) {
				throw new RuntimeException(ex);
			}
		}
	};
}
 
Example 7
Source File: ITManagedStorageAccountKey.java    From azure-keyvault-java with MIT License 6 votes vote down vote up
private static ServiceClientCredentials createTestCredentials() throws Exception {
    return new KeyVaultCredentials() {

        @Override
        public String doAuthenticate(String authorization, String resource, String scope) {
            try {

                if (isRecordMode()) {
                    AuthenticationResult authResult = getAccessToken(authorization, resource);
                    return authResult.getAccessToken();
                } else {
                    return "";
                }

            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }
    };
}
 
Example 8
Source File: KeyVaultClientIntegrationTestBase.java    From azure-keyvault-java with MIT License 6 votes vote down vote up
private static ServiceClientCredentials createTestCredentials() throws Exception {
	return new KeyVaultCredentials() {

		@Override
		public String doAuthenticate(String authorization, String resource, String scope) {
			try {

				if (isRecordMode()) {
					AuthenticationResult authResult = getAccessToken(authorization, resource);
					return authResult.getAccessToken();
				} else {
					return "";
				}

			} catch (Exception ex) {
				throw new RuntimeException(ex);
			}
		}
	};
}
 
Example 9
Source File: AzureKms.java    From sfs with Apache License 2.0 6 votes vote down vote up
private CloudCredentials createCredentials(VertxContext<Server> vertxContext) throws Exception {
    return new KeyVaultCredentials() {

        @Override
        public Header doAuthenticate(ServiceRequestContext request, Map<String, String> challenge) {
            try {
                String authorization = challenge.get("authorization");
                String resource = challenge.get("resource");
                AuthenticationResult authResult = getAccessToken(vertxContext, accessKeyId, secretKey, authorization, resource);
                return new BasicHeader("Authorization", authResult.getAccessTokenType() + " " + authResult.getAccessToken());
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }
    };
}
 
Example 10
Source File: UserTokenCredentials.java    From autorest-clientruntime-for-java with MIT License 6 votes vote down vote up
@Override
public synchronized String getToken(String resource) throws IOException {
    // Find exact match for the resource
    AuthenticationResult authenticationResult = tokens.get(resource);
    // Return if found and not expired
    if (authenticationResult != null && authenticationResult.getExpiresOnDate().after(new Date())) {
        return authenticationResult.getAccessToken();
    }
    // If found then refresh
    boolean shouldRefresh = authenticationResult != null;
    // If not found for the resource, but is MRRT then also refresh
    if (authenticationResult == null && !tokens.isEmpty()) {
        authenticationResult = new ArrayList<>(tokens.values()).get(0);
        shouldRefresh = authenticationResult.isMultipleResourceRefreshToken();
    }
    // Refresh
    if (shouldRefresh) {
        authenticationResult = acquireAccessTokenFromRefreshToken(resource, authenticationResult.getRefreshToken());
    }
    // If refresh fails or not refreshable, acquire new token
    if (authenticationResult == null) {
        authenticationResult = acquireNewAccessToken(resource);
    }
    tokens.put(resource, authenticationResult);
    return authenticationResult.getAccessToken();
}
 
Example 11
Source File: DelegatedTokenCredentials.java    From autorest-clientruntime-for-java with MIT License 6 votes vote down vote up
@Override
public synchronized String getToken(String resource) throws IOException {
    // Find exact match for the resource
    AuthenticationResult authenticationResult = tokens.get(resource);
    // Return if found and not expired
    if (authenticationResult != null && authenticationResult.getExpiresOnDate().after(new Date())) {
        return authenticationResult.getAccessToken();
    }
    // If found then refresh
    boolean shouldRefresh = authenticationResult != null;
    // If not found for the resource, but is MRRT then also refresh
    if (authenticationResult == null && !tokens.isEmpty()) {
        authenticationResult = new ArrayList<>(tokens.values()).get(0);
        shouldRefresh = authenticationResult.isMultipleResourceRefreshToken();
    }
    // Refresh
    if (shouldRefresh) {
        authenticationResult = acquireAccessTokenFromRefreshToken(resource, authenticationResult.getRefreshToken());
    }
    // If refresh fails or not refreshable, acquire new token
    if (authenticationResult == null) {
        authenticationResult = acquireNewAccessToken(resource);
    }
    tokens.put(resource, authenticationResult);
    return authenticationResult.getAccessToken();
}
 
Example 12
Source File: ApplicationTokenCredentials.java    From autorest-clientruntime-for-java with MIT License 5 votes vote down vote up
@Override
public synchronized String getToken(String resource) throws IOException {
    AuthenticationResult authenticationResult = tokens.get(resource);
    if (authenticationResult == null || authenticationResult.getExpiresOnDate().before(new Date())) {
        authenticationResult = acquireAccessToken(resource);
    }
    tokens.put(resource, authenticationResult);
    return authenticationResult.getAccessToken();
}
 
Example 13
Source File: AzureCliToken.java    From autorest-clientruntime-for-java with MIT License 5 votes vote down vote up
AzureCliToken withAuthenticationResult(AuthenticationResult result) {
    this.accessToken = result.getAccessToken();
    this.refreshToken = result.getRefreshToken();
    this.expiresIn = result.getExpiresAfter();
    this.expiresOnDate = result.getExpiresOnDate();
    return this;
}
 
Example 14
Source File: CbDelegatedTokenCredentials.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized String getToken(String resource) throws IOException {
    // Find exact match for the resource
    AuthenticationResult authenticationResult = tokens.get(resource);
    // Return if found and not expired
    if (authenticationResult != null && authenticationResult.getExpiresOnDate().after(new Date())) {
        return authenticationResult.getAccessToken();
    }
    // If found then refresh
    boolean shouldRefresh = authenticationResult != null;
    // If not found for the resource, but is MRRT then also refresh
    if (authenticationResult == null && !tokens.isEmpty()) {
        authenticationResult = new ArrayList<>(tokens.values()).get(0);
        shouldRefresh = authenticationResult.isMultipleResourceRefreshToken();
    }
    // Refresh
    if (shouldRefresh) {
        boolean multipleResourceRefreshToken = authenticationResult.isMultipleResourceRefreshToken();
        String refreshToken = authenticationResult.getRefreshToken();
        authenticationResult = acquireAccessTokenFromRefreshToken(resource, refreshToken, multipleResourceRefreshToken);
    }
    // If refresh fails or not refreshable, acquire new token
    if (authenticationResult == null) {
        authenticationResult = acquireNewAccessToken(resource);
    }
    tokens.put(resource, authenticationResult);
    return authenticationResult.getAccessToken();
}
 
Example 15
Source File: AzureKeyVaultClientAuthenticator.java    From ranger with Apache License 2.0 3 votes vote down vote up
/**
 * It does the authentication. This method will be called by the super
 * class.
 * 
 * @param request
 *            The request being sent
 * @param challenge
 *            Information about the challenge from the service.
 */
@Override
public String doAuthenticate(String authorization, String resource, String scope) {
    AuthenticationResult token = getAccessTokenFromClientCredentials(
        authorization, resource, authClientID, authClientSecret);
    return token.getAccessToken();
}