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