Search in sources :

Example 1 with TokenRequest

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;
    }
}
Also used : JsonWebSignature(com.google.api.client.json.webtoken.JsonWebSignature) GeneralSecurityException(java.security.GeneralSecurityException) TokenRequest(com.google.api.client.auth.oauth2.TokenRequest) GenericUrl(com.google.api.client.http.GenericUrl) IOException(java.io.IOException) JsonWebToken(com.google.api.client.json.webtoken.JsonWebToken) Beta(com.google.api.client.util.Beta)

Example 2 with TokenRequest

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);
}
Also used : Credential(com.google.api.client.auth.oauth2.Credential) StoredCredential(com.google.api.client.auth.oauth2.StoredCredential) TokenResponse(com.google.api.client.auth.oauth2.TokenResponse) AuthorizationCodeTokenRequest(com.google.api.client.auth.oauth2.AuthorizationCodeTokenRequest)

Example 3 with TokenRequest

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);
}
Also used : AuthorizationCodeTokenRequest(com.google.api.client.auth.oauth2.AuthorizationCodeTokenRequest) AuthorizationCodeFlow(com.google.api.client.auth.oauth2.AuthorizationCodeFlow) Test(org.junit.jupiter.api.Test)

Example 4 with 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));
}
Also used : AuthorizationCodeTokenRequest(com.google.api.client.auth.oauth2.AuthorizationCodeTokenRequest) Test(org.junit.jupiter.api.Test)

Aggregations

AuthorizationCodeTokenRequest (com.google.api.client.auth.oauth2.AuthorizationCodeTokenRequest)3 Test (org.junit.jupiter.api.Test)2 AuthorizationCodeFlow (com.google.api.client.auth.oauth2.AuthorizationCodeFlow)1 Credential (com.google.api.client.auth.oauth2.Credential)1 StoredCredential (com.google.api.client.auth.oauth2.StoredCredential)1 TokenRequest (com.google.api.client.auth.oauth2.TokenRequest)1 TokenResponse (com.google.api.client.auth.oauth2.TokenResponse)1 GenericUrl (com.google.api.client.http.GenericUrl)1 JsonWebSignature (com.google.api.client.json.webtoken.JsonWebSignature)1 JsonWebToken (com.google.api.client.json.webtoken.JsonWebToken)1 Beta (com.google.api.client.util.Beta)1 IOException (java.io.IOException)1 GeneralSecurityException (java.security.GeneralSecurityException)1