Search in sources :

Example 21 with FleetEngineToken

use of com.google.fleetengine.auth.token.FleetEngineToken in project java-fleetengine-auth by googlemaps.

the class LocalSignerTest method sign_returnsCorrectJwtPayload.

@Test
public void sign_returnsCorrectJwtPayload() throws SigningTokenException {
    LocalSigner localSigner = LocalSigner.create(CLIENT_EMAIL, FAKE_PRIVATE_KEY_ID, FAKE_PRIVATE_KEY);
    FleetEngineToken token = FleetEngineToken.builder().setTokenType(FleetEngineTokenType.SERVER).setCreationTimestamp(Date.from(creation.instant())).setExpirationTimestamp(Date.from(expiration.instant())).setAuthorizationClaims(EmptyFleetEngineTokenClaims.INSTANCE).build();
    FleetEngineToken signedToken = localSigner.sign(token);
    DecodedJWT decodedJWT = JWT.decode(signedToken.jwt());
    String payload = new String(Base64.getDecoder().decode(decodedJWT.getPayload()), UTF_8);
    Gson gson = new Gson();
    JwtPayload jwtPayload = gson.fromJson(payload, JwtPayload.class);
    // Payload is signed with the default jwt audience
    assertThat(jwtPayload.audience).isEqualTo(CommonConstants.DEFAULT_JWT_AUDIENCE);
    assertThat(jwtPayload.issuer).isEqualTo(CLIENT_EMAIL);
    assertThat(jwtPayload.subject).isEqualTo(CLIENT_EMAIL);
    assertThat(jwtPayload.issuedAt).isEqualTo(creation.instant().getEpochSecond());
    assertThat(jwtPayload.expiredAt).isEqualTo(expiration.instant().getEpochSecond());
}
Also used : Gson(com.google.gson.Gson) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT) FleetEngineToken(com.google.fleetengine.auth.token.FleetEngineToken) Test(org.junit.Test)

Example 22 with FleetEngineToken

use of com.google.fleetengine.auth.token.FleetEngineToken in project java-fleetengine-auth by googlemaps.

the class FleetEngineTokenFactoryTest method createConsumerToken_returnsConsumerTokenType.

@Test
public void createConsumerToken_returnsConsumerTokenType() {
    FleetEngineTokenFactory factory = new FleetEngineTokenFactory(clock, FleetEngineTokenFactorySettings.builder().build());
    FleetEngineToken signedToken = factory.createConsumerToken(TripClaims.create(TEST_TRIP_ID));
    assertThat(signedToken.tokenType()).isEqualTo(CONSUMER);
}
Also used : FleetEngineToken(com.google.fleetengine.auth.token.FleetEngineToken) Test(org.junit.Test)

Example 23 with FleetEngineToken

use of com.google.fleetengine.auth.token.FleetEngineToken in project java-fleetengine-auth by googlemaps.

the class FleetEngineTokenFactoryTest method createTrustedDriverToken_whenVehicleClaimsAndTaskClaimsAreWild_claimIsWild.

@Test
public void createTrustedDriverToken_whenVehicleClaimsAndTaskClaimsAreWild_claimIsWild() {
    FleetEngineTokenFactorySettings settings = FleetEngineTokenFactorySettings.builder().setAudience(FAKE_AUDIENCE).build();
    FleetEngineTokenFactory factory = new FleetEngineTokenFactory(clock, settings);
    FleetEngineToken token = factory.createTrustedDeliveryDriverToken(DeliveryVehicleClaims.create(), TaskClaims.create());
    assertThat(token.authorizationClaims().isWildcard()).isTrue();
}
Also used : FleetEngineToken(com.google.fleetengine.auth.token.FleetEngineToken) Test(org.junit.Test)

Example 24 with FleetEngineToken

use of com.google.fleetengine.auth.token.FleetEngineToken in project java-fleetengine-auth by googlemaps.

the class DefaultServiceAccountSignerTest method sign_buildsJwtCorrectly.

@Test
public void sign_buildsJwtCorrectly() {
    FleetEngineToken token = FleetEngineToken.builder().setTokenType(FleetEngineTokenType.SERVER).setCreationTimestamp(Date.from(creation.instant())).setExpirationTimestamp(Date.from(expiration.instant())).setAudience(TEST_AUDIENCE).setAuthorizationClaims(EmptyFleetEngineTokenClaims.INSTANCE).build();
    when(serviceAccountCredentials.sign(any(), any())).thenAnswer(invocation -> {
        byte[] presignedHeaderJwt = invocation.getArgument(0, byte[].class);
        byte[] presignedContentJwt = invocation.getArgument(0, byte[].class);
        return Algorithm.none().sign(presignedHeaderJwt, presignedContentJwt);
    });
    when(serviceAccountCredentials.getClientEmail()).thenReturn(TEST_SERVICE_ACCOUNT_EMAIL);
    DefaultServiceAccountSigner signer = new DefaultServiceAccountSigner(serviceAccountCredentials);
    // Sign the token with the "none" algorithm.
    FleetEngineToken signedToken = signer.sign(token);
    // Check that the payload matches what was expected
    DecodedJWT decodedJWT = JWT.decode(signedToken.jwt());
    String payload = new String(Base64.getDecoder().decode(decodedJWT.getPayload()), UTF_8);
    Gson gson = new Gson();
    JwtPayload jwtPayload = gson.fromJson(payload, JwtPayload.class);
    assertThat(jwtPayload.audience).isEqualTo(TEST_AUDIENCE);
    assertThat(jwtPayload.issuer).isEqualTo(TEST_SERVICE_ACCOUNT_EMAIL);
    assertThat(jwtPayload.subject).isEqualTo(TEST_SERVICE_ACCOUNT_EMAIL);
    assertThat(jwtPayload.issuedAt).isEqualTo(creation.instant().getEpochSecond());
    assertThat(jwtPayload.expiredAt).isEqualTo(expiration.instant().getEpochSecond());
}
Also used : Gson(com.google.gson.Gson) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT) FleetEngineToken(com.google.fleetengine.auth.token.FleetEngineToken) Test(org.junit.Test)

Example 25 with FleetEngineToken

use of com.google.fleetengine.auth.token.FleetEngineToken in project java-fleetengine-auth by googlemaps.

the class FleetEngineTokenExpiryValidatorTest method isTokenExpired_whenBeforeExpirationWindow_tokenIsNotExpired.

@Test
public void isTokenExpired_whenBeforeExpirationWindow_tokenIsNotExpired() {
    Duration expirationWindowDuration = Duration.ofDays(1);
    FleetEngineToken expiredToken = defaultToken.toBuilder().setExpirationTimestamp(parseDate("2020-06-15")).build();
    Instant now = parseDateTime("2020-06-13T23:00:00.00Z").toInstant();
    when(fakeNowClock.instant()).thenReturn(now);
    FleetEngineTokenExpiryValidator expiryValidator = new FleetEngineTokenExpiryValidator(fakeNowClock);
    boolean isTokenExpired = expiryValidator.isTokenExpired(expiredToken, expirationWindowDuration);
    assertThat(isTokenExpired).isFalse();
}
Also used : Instant(java.time.Instant) Duration(java.time.Duration) FleetEngineToken(com.google.fleetengine.auth.token.FleetEngineToken) Test(org.junit.Test)

Aggregations

FleetEngineToken (com.google.fleetengine.auth.token.FleetEngineToken)30 Test (org.junit.Test)27 DecodedJWT (com.auth0.jwt.interfaces.DecodedJWT)6 Gson (com.google.gson.Gson)5 Duration (java.time.Duration)3 Instant (java.time.Instant)3 SigningTokenException (com.google.fleetengine.auth.token.factory.signer.SigningTokenException)2 Algorithm (com.auth0.jwt.algorithms.Algorithm)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 FleetEngineTokenType (com.google.fleetengine.auth.token.FleetEngineTokenType)1 ImpersonatedAccountSignerCredentials (com.google.fleetengine.auth.token.factory.signer.ImpersonatedSigner.ImpersonatedAccountSignerCredentials)1 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)1 RSAPublicKey (java.security.interfaces.RSAPublicKey)1