in core/src/main/java/com/microsoft/alm/auth/pat/VstsPatAuthenticator.java [145:203]
private Token getToken(final URI uri, final boolean isCreatingGlobalPat,
final VsoTokenScope tokenScope, final String patDisplayName,
final PromptBehavior promptBehavior, final TokenPair oauth2Token) {
Debug.Assert(uri != null, "uri cannot be null");
Debug.Assert(promptBehavior != null, "promptBehavior cannot be null");
logger.info("Retrieving PersonalAccessToken for uri:{} with name:{}, and with scope:{}, prompt behavior: {}",
uri, patDisplayName, tokenScope, promptBehavior.name());
final String key = getKey(uri);
Debug.Assert(key != null, "Failed to convert uri to key");
final SecretRetriever<Token> secretRetriever = new SecretRetriever<Token>() {
@Override
protected boolean tryGetValidated(final Token token, final AtomicReference<Token> holder) {
Debug.Assert(token != null, "Token is null");
Debug.Assert(holder != null, "Holder is null");
final URI validationEndpoint = URI.create(uri + "/_apis/connectionData");
boolean valid = false;
if (token.Value != null) {
final HttpClient client = Global.getHttpClientFactory().createHttpClient();
token.contributeHeader(client.getHeaders());
try {
client.getGetResponseText(validationEndpoint);
valid = true;
} catch (IOException e) {
logger.debug("Validation failed with IOException.", e);
}
}
logger.debug("Personal Access Token is {}.", valid ? "valid" : "invalid.");
return valid;
}
@Override
protected Token doRetrieve() {
final TokenPair tokenPair = (oauth2Token == null)
? vstsOauthAuthenticator.getOAuth2TokenPair(uri, promptBehavior.AUTO)
: oauth2Token;
if (tokenPair == null) {
// authentication failed, return null
logger.debug("Failed to get an OAuth2 token, cannot generate PersonalAccessToken.");
return null;
}
logger.debug("Got OAuth2 token, retrieving Personal Access Token with it.");
final URI accountSpecificUri = createAccountSpecificUri(uri, tokenPair);
final Token pat = vsoAzureAuthority.generatePersonalAccessToken(accountSpecificUri, tokenPair.AccessToken,
tokenScope, true, isCreatingGlobalPat, patDisplayName);
return pat;
}
};
return secretRetriever.retrieve(key, getStore(), promptBehavior);
}