public abstract class KeyVaultCredentials extends Object implements com.microsoft.rest.credentials.ServiceClientCredentials
ServiceClientCredentials that supports automatic bearer token refresh.| Constructor and Description |
|---|
KeyVaultCredentials() |
| Modifier and Type | Method and Description |
|---|---|
void |
applyCredentialsFilter(okhttp3.OkHttpClient.Builder clientBuilder) |
abstract String |
doAuthenticate(String authorization,
String resource,
String scope)
Abstract method to be implemented.
|
public void applyCredentialsFilter(okhttp3.OkHttpClient.Builder clientBuilder)
applyCredentialsFilter in interface com.microsoft.rest.credentials.ServiceClientCredentialspublic abstract String doAuthenticate(String authorization, String resource, String scope)
authorization - Identifier of the authority, a URL.resource - Identifier of the target resource that is the recipient of the
requested token, a URL.scope - The scope of the authentication request.Implementations typically use ADAL to get a token, as performed in the sample below:
@Override
public String doAuthenticate(String authorization, String resource, String scope) {
String clientId = ...; // client GUID as shown in Azure portal.
String clientKey = ...; // client key as provided by Azure portal.
AuthenticationResult token = getAccessTokenFromClientCredentials(authorization, resource, clientId, clientKey);
return token.getAccessToken();;
}
private static AuthenticationResult getAccessTokenFromClientCredentials(String authorization, String resource, String clientId, String clientKey) {
AuthenticationContext context = null;
AuthenticationResult result = null;
ExecutorService service = null;
try {
service = Executors.newFixedThreadPool(1);
context = new AuthenticationContext(authorization, false, service);
ClientCredential credentials = new ClientCredential(clientId, clientKey);
Future<AuthenticationResult> future = context.acquireToken(resource, credentials, null);
result = future.get();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
service.shutdown();
}
if (result == null) {
throw new RuntimeException("authentication result was null");
}
return result;
}
Note: The client key must be securely stored. It's advised to use two client applications - one for development and other for production - managed by separate parties.
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for
* license information.
*/