use of com.google.api.client.auth.oauth2.TokenRequest in project google-api-java-client by google.
the class GoogleCredential method executeRefreshToken.
@Override
@Beta
protected TokenResponse executeRefreshToken() throws IOException {
if (serviceAccountPrivateKey == null) {
return super.executeRefreshToken();
}
// service accounts: no refresh token; instead use private key to request new access token
JsonWebSignature.Header header = new JsonWebSignature.Header();
header.setAlgorithm("RS256");
header.setType("JWT");
header.setKeyId(serviceAccountPrivateKeyId);
JsonWebToken.Payload payload = new JsonWebToken.Payload();
long currentTime = getClock().currentTimeMillis();
payload.setIssuer(serviceAccountId);
payload.setAudience(getTokenServerEncodedUrl());
payload.setIssuedAtTimeSeconds(currentTime / 1000);
payload.setExpirationTimeSeconds(currentTime / 1000 + 3600);
payload.setSubject(serviceAccountUser);
payload.put("scope", Joiner.on(' ').join(serviceAccountScopes));
try {
String assertion = JsonWebSignature.signUsingRsaSha256(serviceAccountPrivateKey, getJsonFactory(), header, payload);
TokenRequest request = new TokenRequest(getTransport(), getJsonFactory(), new GenericUrl(getTokenServerEncodedUrl()), "urn:ietf:params:oauth:grant-type:jwt-bearer");
request.put("assertion", assertion);
return request.execute();
} catch (GeneralSecurityException exception) {
IOException e = new IOException();
e.initCause(exception);
throw e;
}
}
use of com.google.api.client.auth.oauth2.TokenRequest in project hub-alert by blackducksoftware.
the class AzureBoardsProperties method requestTokens.
public Optional<Credential> requestTokens(AuthorizationCodeFlow authorizationCodeFlow, String authorizationCode) throws IOException {
AuthorizationCodeTokenRequest tokenRequest = authorizationCodeFlow.newTokenRequest(authorizationCode);
TokenResponse tokenResponse = tokenRequest.execute();
Credential credential = authorizationCodeFlow.createAndStoreCredential(tokenResponse, oauthUserId);
return Optional.ofNullable(credential);
}
use of com.google.api.client.auth.oauth2.TokenRequest in project hub-alert by blackducksoftware.
the class AzureAuthorizationCodeFlowTest method builderTest.
@Test
public void builderTest() {
AzureAuthorizationCodeFlow.Builder builder = new AzureAuthorizationCodeFlow.Builder(method, httpTransport, jsonFactory, genericUrl, clientAuthentication, clientId, authorizationServerEncodedUrl, null, null);
assertNull(builder.getClientSecret());
assertNull(builder.getRedirectUri());
builder.setClientSecret(clientSecret);
builder.setRedirectUri(redirectUri);
assertEquals(clientSecret, builder.getClientSecret());
assertEquals(redirectUri, builder.getRedirectUri());
AuthorizationCodeFlow azureAuthorizationCodeFlow = builder.build();
AuthorizationCodeTokenRequest tokenRequest = azureAuthorizationCodeFlow.newTokenRequest(authorizationCode);
testAuthorizationCodeTokenRequest(tokenRequest);
}
use of com.google.api.client.auth.oauth2.TokenRequest in project hub-alert by blackducksoftware.
the class AzureAuthorizationCodeFlowTest method newTokenRequestTest.
@Test
public void newTokenRequestTest() {
AzureAuthorizationCodeFlow azureAuthorizationCodeFlow = new AzureAuthorizationCodeFlow(method, httpTransport, jsonFactory, genericUrl, clientAuthentication, clientId, authorizationServerEncodedUrl, clientSecret, redirectUri);
AuthorizationCodeTokenRequest tokenRequest = azureAuthorizationCodeFlow.newTokenRequest(authorizationCode);
assertEquals(authorizationCode, tokenRequest.getCode());
assertEquals(AzureOAuthConstants.DEFAULT_GRANT_TYPE, tokenRequest.getGrantType());
assertEquals(redirectUri, tokenRequest.getRedirectUri());
assertEquals("", tokenRequest.getScopes());
assertEquals(authorizationCode, tokenRequest.get(AzureOAuthConstants.REQUEST_BODY_FIELD_ASSERTION));
assertEquals(AzureOAuthConstants.DEFAULT_CLIENT_ASSERTION_TYPE, tokenRequest.get(AzureOAuthConstants.REQUEST_BODY_FIELD_CLIENT_ASSERTION_TYPE));
assertEquals(clientSecret, tokenRequest.get(AzureOAuthConstants.REQUEST_BODY_FIELD_CLIENT_ASSERTION));
assertEquals(redirectUri, tokenRequest.get(AzureOAuthConstants.REQUEST_BODY_FIELD_REDIRECT_URI));
}
Aggregations