Search in sources :

Example 11 with FleetEngineToken

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

the class NaiveAuthStateManagerTest method signWildcardToken_whenTokenNotExpired_returnsCachedToken.

@Test
public void signWildcardToken_whenTokenNotExpired_returnsCachedToken() throws SigningTokenException {
    FleetEngineToken signedToken = token.toBuilder().setJwt(TEST_JWT).build();
    when(signer.sign(eq(token))).thenReturn(signedToken);
    when(expiryValidator.isTokenExpired(eq(signedToken), eq(EXPIRATION_WINDOW_DURATION))).thenReturn(false);
    NaiveAuthStateManager manager = new NaiveAuthStateManager(expiryValidator);
    // Sets the new token
    FleetEngineToken returnedToken = manager.signToken(signer, token);
    // Since the token hasn't expired, the first token is returned
    FleetEngineToken returnedToken2 = manager.signToken(signer, token2);
    verify(signer, times(1)).sign(eq(token));
    verify(signer, times(0)).sign(eq(token2));
    assertThat(returnedToken).isEqualTo(signedToken);
    assertThat(returnedToken2).isEqualTo(signedToken);
    verifyNoMoreInteractions(ignoreStubs(expiryValidator));
}
Also used : FleetEngineToken(com.google.fleetengine.auth.token.FleetEngineToken) Test(org.junit.Test)

Example 12 with FleetEngineToken

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

the class NaiveAuthStateManagerTest method signWildcardToken_whenTokenExpired_returnsNewToken.

@Test
public void signWildcardToken_whenTokenExpired_returnsNewToken() throws SigningTokenException {
    FleetEngineToken signedToken = token.toBuilder().setJwt(TEST_JWT).build();
    when(signer.sign(eq(token))).thenReturn(signedToken);
    FleetEngineToken signedToken2 = token2.toBuilder().setJwt(TEST_JWT).build();
    when(signer.sign(eq(token2))).thenReturn(signedToken2);
    when(expiryValidator.isTokenExpired(eq(signedToken), eq(EXPIRATION_WINDOW_DURATION))).thenReturn(true);
    NaiveAuthStateManager manager = new NaiveAuthStateManager(expiryValidator);
    // Sets the new token
    FleetEngineToken returnedToken = manager.signToken(signer, token);
    // Since the token hasn't expired, the first token is returned
    FleetEngineToken returnedToken2 = manager.signToken(signer, token2);
    verify(signer, times(1)).sign(eq(token));
    verify(signer, times(1)).sign(eq(token2));
    assertThat(returnedToken).isEqualTo(signedToken);
    assertThat(returnedToken2).isEqualTo(signedToken2);
    verifyNoMoreInteractions(ignoreStubs(expiryValidator));
}
Also used : FleetEngineToken(com.google.fleetengine.auth.token.FleetEngineToken) Test(org.junit.Test)

Example 13 with FleetEngineToken

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

the class NaiveAuthStateManagerTest method signNonServerWildcardToken_whenTokenNotExpired_returnsCachedToken.

@Test
public void signNonServerWildcardToken_whenTokenNotExpired_returnsCachedToken() throws SigningTokenException {
    token = token.toBuilder().setTokenType(FleetEngineTokenType.CONSUMER).build();
    token2 = token2.toBuilder().setTokenType(FleetEngineTokenType.CONSUMER).build();
    FleetEngineToken signedToken = token.toBuilder().setJwt(TEST_JWT).build();
    when(signer.sign(eq(token))).thenReturn(signedToken);
    when(expiryValidator.isTokenExpired(eq(signedToken), eq(EXPIRATION_WINDOW_DURATION))).thenReturn(false);
    NaiveAuthStateManager manager = new NaiveAuthStateManager(expiryValidator);
    // Sets the new token
    FleetEngineToken returnedToken = manager.signToken(signer, token);
    // Since the token hasn't expired, the first token is returned
    FleetEngineToken returnedToken2 = manager.signToken(signer, token2);
    verify(signer, times(1)).sign(eq(token));
    verify(signer, times(0)).sign(eq(token2));
    assertThat(returnedToken).isEqualTo(signedToken);
    assertThat(returnedToken2).isEqualTo(signedToken);
    verifyNoMoreInteractions(ignoreStubs(expiryValidator));
}
Also used : FleetEngineToken(com.google.fleetengine.auth.token.FleetEngineToken) Test(org.junit.Test)

Example 14 with FleetEngineToken

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

the class NaiveAuthStateManagerTest method findOrCreateDriverToken_returnsNewToken.

@Test
public void findOrCreateDriverToken_returnsNewToken() throws SigningTokenException {
    token = token.toBuilder().setTokenType(FleetEngineTokenType.DRIVER).build();
    FleetEngineToken signedToken = token.toBuilder().setJwt(TEST_JWT).build();
    when(signer.sign(eq(token))).thenReturn(signedToken);
    NaiveAuthStateManager manager = new NaiveAuthStateManager(FleetEngineTokenExpiryValidator.getInstance());
    FleetEngineToken returnedToken = manager.signToken(signer, token);
    assertThat(returnedToken).isEqualTo(signedToken);
    verify(signer, times(1)).sign(eq(token));
    verifyNoMoreInteractions(ignoreStubs(expiryValidator));
}
Also used : FleetEngineToken(com.google.fleetengine.auth.token.FleetEngineToken) Test(org.junit.Test)

Example 15 with FleetEngineToken

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

the class LocalSignerTest method sign_returnsCorrectJwtHeader.

@Test
public void sign_returnsCorrectJwtHeader() 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())).setAudience(TEST_AUDIENCE).setAuthorizationClaims(EmptyFleetEngineTokenClaims.INSTANCE).build();
    FleetEngineToken signedToken = localSigner.sign(token);
    DecodedJWT decodedJWT = JWT.decode(signedToken.jwt());
    String header = new String(Base64.getDecoder().decode(decodedJWT.getHeader()), UTF_8);
    Gson gson = new Gson();
    JwtHeader jwtHeader = gson.fromJson(header, JwtHeader.class);
    assertThat(jwtHeader.keyId).isEqualTo(FAKE_PRIVATE_KEY_ID);
}
Also used : Gson(com.google.gson.Gson) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT) 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