Search in sources :

Example 41 with AccessToken

use of org.glassfish.jersey.client.oauth1.AccessToken in project terra-cli by DataBiosphere.

the class GoogleOauth method getExistingUserCredential.

/**
 * Get the existing credential for the given user.
 *
 * @param scopes list of scopes requested of the user
 * @param clientSecretFile stream to the client secret file
 * @param dataStoreDir directory where the local credential store is persisted
 * @return credentials object for the user
 */
public static UserCredentials getExistingUserCredential(List<String> scopes, InputStream clientSecretFile, File dataStoreDir) throws IOException, GeneralSecurityException {
    // load client_secret.json file
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(clientSecretFile, StandardCharsets.UTF_8));
    // get a pointer to the credential datastore
    GoogleAuthorizationCodeFlow flow = getOAuth2Flow(scopes, clientSecrets, dataStoreDir);
    DataStore<StoredCredential> dataStore = flow.getCredentialDataStore();
    // fetch the stored credential for the specified userId
    StoredCredential storedCredential = dataStore.get(CREDENTIAL_STORE_KEY);
    if (storedCredential == null) {
        // there is no credential, return here
        return null;
    }
    // now turn the stored credential into a regular OAuth2 Credentials representing a user's
    // identity and consent
    UserCredentials credentials = UserCredentials.newBuilder().setClientId(clientSecrets.getDetails().getClientId()).setClientSecret(clientSecrets.getDetails().getClientSecret()).setRefreshToken(storedCredential.getRefreshToken()).setAccessToken(new AccessToken(storedCredential.getAccessToken(), new Date(storedCredential.getExpirationTimeMilliseconds()))).build();
    return credentials;
}
Also used : InputStreamReader(java.io.InputStreamReader) StoredCredential(com.google.api.client.auth.oauth2.StoredCredential) AccessToken(com.google.auth.oauth2.AccessToken) GoogleAuthorizationCodeFlow(com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow) GoogleClientSecrets(com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets) UserCredentials(com.google.auth.oauth2.UserCredentials) Date(java.util.Date)

Example 42 with AccessToken

use of org.glassfish.jersey.client.oauth1.AccessToken in project cdap by cdapio.

the class GCPRemoteAuthenticatorTest method testRemoteAuthenticatorRefreshesExpiredAccessToken.

@Test
public void testRemoteAuthenticatorRefreshesExpiredAccessToken() throws Exception {
    String expiredAccessTokenValue = "expired-access-token";
    String accessTokenValue = "access-token";
    // This is just an arbitrary fixed point in time.
    Instant fixedInstant = Instant.ofEpochSecond(1646358109);
    Clock fixedClock = Clock.fixed(fixedInstant, ZoneId.systemDefault());
    GoogleCredentials mockGoogleCredentials = mock(GoogleCredentials.class);
    AccessToken expiredAccessToken = new AccessToken(expiredAccessTokenValue, Date.from(fixedInstant.minus(Duration.ofHours(1))));
    AccessToken accessToken = new AccessToken(accessTokenValue, Date.from(fixedInstant.plus(Duration.ofHours(1))));
    when(mockGoogleCredentials.refreshAccessToken()).thenReturn(accessToken);
    GCPRemoteAuthenticator gcpRemoteAuthenticator = new GCPRemoteAuthenticator(mockGoogleCredentials, fixedClock, expiredAccessToken);
    // Verify expected credential value and that refresh was called exactly once.
    Credential credential = gcpRemoteAuthenticator.getCredentials();
    Assert.assertEquals(accessTokenValue, credential.getValue());
    verify(mockGoogleCredentials, times(1)).refreshAccessToken();
}
Also used : Credential(io.cdap.cdap.proto.security.Credential) AccessToken(com.google.auth.oauth2.AccessToken) Instant(java.time.Instant) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) Clock(java.time.Clock) Test(org.junit.Test)

Example 43 with AccessToken

use of org.glassfish.jersey.client.oauth1.AccessToken in project cdap by cdapio.

the class GCPRemoteAuthenticatorTest method testRemoteAuthenticatorRefreshesNullAccessToken.

@Test
public void testRemoteAuthenticatorRefreshesNullAccessToken() throws Exception {
    String accessTokenValue = "access-token";
    // This is just an arbitrary fixed point in time.
    Instant fixedInstant = Instant.ofEpochSecond(1646358109);
    Clock fixedClock = Clock.fixed(fixedInstant, ZoneId.systemDefault());
    GoogleCredentials mockGoogleCredentials = mock(GoogleCredentials.class);
    AccessToken accessToken = new AccessToken(accessTokenValue, Date.from(fixedInstant.plus(Duration.ofHours(1))));
    when(mockGoogleCredentials.refreshAccessToken()).thenReturn(accessToken);
    GCPRemoteAuthenticator gcpRemoteAuthenticator = new GCPRemoteAuthenticator(mockGoogleCredentials, fixedClock, null);
    // Verify expected credential value and that refresh was called exactly once.
    Credential credential = gcpRemoteAuthenticator.getCredentials();
    Assert.assertEquals(accessTokenValue, credential.getValue());
    verify(mockGoogleCredentials, times(1)).refreshAccessToken();
}
Also used : Credential(io.cdap.cdap.proto.security.Credential) AccessToken(com.google.auth.oauth2.AccessToken) Instant(java.time.Instant) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) Clock(java.time.Clock) Test(org.junit.Test)

Example 44 with AccessToken

use of org.glassfish.jersey.client.oauth1.AccessToken in project hadoop-connectors by GoogleCloudDataproc.

the class HadoopCredentialsConfigurationTest method userCredentials_credentialFactory_noNewRefreshToken.

@Test
public void userCredentials_credentialFactory_noNewRefreshToken() throws IOException {
    // GIVEN
    String initialRefreshToken = "FAKE_REFRESH_TOKEN";
    String tokenServerUrl = "http://localhost/token";
    configuration.set(getConfigKey(TOKEN_SERVER_URL_SUFFIX), tokenServerUrl);
    configuration.setEnum(getConfigKey(AUTHENTICATION_TYPE_SUFFIX), AuthenticationType.USER_CREDENTIALS);
    configuration.set(getConfigKey(AUTH_REFRESH_TOKEN_SUFFIX), initialRefreshToken);
    configuration.set(getConfigKey(AUTH_CLIENT_ID_SUFFIX), "FAKE_CLIENT_ID");
    configuration.set(getConfigKey(AUTH_CLIENT_SECRET_SUFFIX), "FAKE_CLIENT_SECRET");
    long expireInSec = 300L;
    String accessTokenAsString = "SlAV32hkKG";
    TokenResponse tokenResponse = new TokenResponse().setAccessToken(accessTokenAsString).setExpiresInSeconds(expireInSec);
    MockHttpTransport transport = mockTransport(jsonDataResponse(tokenResponse));
    // WHEN
    GoogleCredentials credentials = getCredentials(transport);
    credentials.refresh();
    // THEN
    assertThat(credentials).isInstanceOf(UserCredentials.class);
    UserCredentials userCredentials = (UserCredentials) credentials;
    assertThat(userCredentials.getClientId()).isEqualTo("FAKE_CLIENT_ID");
    assertThat(userCredentials.getClientSecret()).isEqualTo("FAKE_CLIENT_SECRET");
    AccessToken accessToken = userCredentials.getAccessToken();
    assertThat(accessToken).isNotNull();
    // To avoid any timebase issue, we test a time range instead
    assertThat(accessToken.getExpirationTime()).isGreaterThan(Date.from(Instant.now().plusSeconds(expireInSec - 10)));
    assertThat(accessToken.getExpirationTime()).isLessThan(Date.from(Instant.now().plusSeconds(expireInSec + 10)));
    String refreshToken = userCredentials.getRefreshToken();
    assertThat(refreshToken).isEqualTo(initialRefreshToken);
}
Also used : MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) TokenResponse(com.google.api.client.auth.oauth2.TokenResponse) AccessToken(com.google.auth.oauth2.AccessToken) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) UserCredentials(com.google.auth.oauth2.UserCredentials) Test(org.junit.Test)

Example 45 with AccessToken

use of org.glassfish.jersey.client.oauth1.AccessToken in project nomulus by google.

the class DatastoreAdminTest method beforeEach.

@BeforeEach
void beforeEach() {
    Date oneHourLater = new Date(System.currentTimeMillis() + 3_600_000);
    GoogleCredentials googleCredentials = GoogleCredentials.create(new AccessToken(ACCESS_TOKEN, oneHourLater));
    GoogleCredentialsBundle credentialsBundle = GoogleCredentialsBundle.create(googleCredentials);
    datastoreAdmin = new DatastoreAdmin.Builder(credentialsBundle.getHttpTransport(), credentialsBundle.getJsonFactory(), credentialsBundle.getHttpRequestInitializer()).setApplicationName("MyApplication").setProjectId("MyCloudProject").build();
}
Also used : AccessToken(com.google.auth.oauth2.AccessToken) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) GoogleCredentialsBundle(google.registry.util.GoogleCredentialsBundle) Date(java.util.Date) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

AccessToken (com.google.auth.oauth2.AccessToken)71 Test (org.junit.Test)41 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)29 Date (java.util.Date)22 IOException (java.io.IOException)19 AccessToken (io.vertx.ext.auth.oauth2.AccessToken)16 Client (javax.ws.rs.client.Client)10 AccessToken (org.glassfish.jersey.client.oauth1.AccessToken)10 ConsumerCredentials (org.glassfish.jersey.client.oauth1.ConsumerCredentials)10 JsonObject (io.vertx.core.json.JsonObject)9 URI (java.net.URI)9 Feature (javax.ws.rs.core.Feature)8 JerseyTest (org.glassfish.jersey.test.JerseyTest)8 MockHttpTransport (com.google.api.client.testing.http.MockHttpTransport)6 InputStreamReader (java.io.InputStreamReader)6 Instant (java.time.Instant)6 WebTarget (javax.ws.rs.client.WebTarget)6 LoggingFeature (org.glassfish.jersey.logging.LoggingFeature)6 OAuth2Credentials (com.google.auth.oauth2.OAuth2Credentials)5 OAuth2TokenImpl (io.vertx.ext.auth.oauth2.impl.OAuth2TokenImpl)5