Search in sources :

Example 1 with FixedClock

use of com.google.api.client.testing.http.FixedClock in project google-auth-library-java by google.

the class ServiceAccountCredentialsTest method refreshAccessToken_tokenExpiry.

@Test
public void refreshAccessToken_tokenExpiry() throws IOException {
    final String tokenString = "1/MkSJoj1xsli0AccessToken_NKPY2";
    MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
    MockTokenServerTransport transport = transportFactory.transport;
    ServiceAccountCredentials credentials = ServiceAccountCredentials.fromPkcs8(SA_CLIENT_ID, SA_CLIENT_EMAIL, SA_PRIVATE_KEY_PKCS8, SA_PRIVATE_KEY_ID, SCOPES, transportFactory, null);
    credentials.clock = new FixedClock(0L);
    transport.addServiceAccount(SA_CLIENT_EMAIL, tokenString);
    AccessToken accessToken = credentials.refreshAccessToken();
    assertEquals(tokenString, accessToken.getTokenValue());
    assertEquals(3600 * 1000L, accessToken.getExpirationTimeMillis().longValue());
    // Test for large expires_in values (should not overflow).
    transport.setExpiresInSeconds(3600 * 1000);
    accessToken = credentials.refreshAccessToken();
    assertEquals(tokenString, accessToken.getTokenValue());
    assertEquals(3600 * 1000 * 1000L, accessToken.getExpirationTimeMillis().longValue());
}
Also used : MockTokenServerTransportFactory(com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory) FixedClock(com.google.api.client.testing.http.FixedClock) Test(org.junit.Test)

Example 2 with FixedClock

use of com.google.api.client.testing.http.FixedClock in project google-api-java-client by google.

the class GoogleIdTokenVerifierTest method testBuilder.

public void testBuilder() throws Exception {
    GoogleIdTokenVerifier.Builder builder = new GoogleIdTokenVerifier.Builder(new GooglePublicKeysManagerTest.PublicCertsMockHttpTransport(), new JacksonFactory()).setIssuer(ISSUER).setAudience(TRUSTED_CLIENT_IDS);
    assertEquals(Clock.SYSTEM, builder.getClock());
    assertEquals(ISSUER, builder.getIssuer());
    assertTrue(TRUSTED_CLIENT_IDS.equals(builder.getAudience()));
    Clock clock = new FixedClock(4);
    builder.setClock(clock);
    assertEquals(clock, builder.getClock());
    IdTokenVerifier verifier = builder.build();
    assertEquals(clock, verifier.getClock());
    assertEquals(ISSUER, verifier.getIssuer());
    assertEquals(TRUSTED_CLIENT_IDS, Lists.newArrayList(verifier.getAudience()));
}
Also used : FixedClock(com.google.api.client.testing.http.FixedClock) Clock(com.google.api.client.util.Clock) FixedClock(com.google.api.client.testing.http.FixedClock) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) IdTokenVerifier(com.google.api.client.auth.openidconnect.IdTokenVerifier)

Example 3 with FixedClock

use of com.google.api.client.testing.http.FixedClock in project google-api-java-client by google.

the class GooglePublicKeysManagerTest method testLoadCerts_cache.

public void testLoadCerts_cache() throws Exception {
    PublicCertsMockHttpTransport transport = new PublicCertsMockHttpTransport();
    transport.useAgeHeader = true;
    GooglePublicKeysManager certs = new GooglePublicKeysManager.Builder(transport, new JacksonFactory()).setClock(new FixedClock(100)).build();
    certs.refresh();
    assertEquals(2, certs.getPublicKeys().size());
    assertEquals(100 + (MAX_AGE - AGE) * 1000, certs.getExpirationTimeMilliseconds());
}
Also used : FixedClock(com.google.api.client.testing.http.FixedClock) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory)

Aggregations

FixedClock (com.google.api.client.testing.http.FixedClock)3 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)2 IdTokenVerifier (com.google.api.client.auth.openidconnect.IdTokenVerifier)1 Clock (com.google.api.client.util.Clock)1 MockTokenServerTransportFactory (com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory)1 Test (org.junit.Test)1