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;
}
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();
}
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();
}
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);
}
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();
}
Aggregations