Search in sources :

Example 26 with FleetEngineToken

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

the class NaiveAuthStateManagerTest method signNonWildcardToken_neverCaches.

/**
 * Thorough check that verifies that only server tokens are cached. No need go through every type
 * of token.
 */
@Test
public void signNonWildcardToken_neverCaches() throws SigningTokenException {
    FleetEngineToken wildcardToken = token.toBuilder().setJwt(TEST_JWT).build();
    FleetEngineToken signedWildcardToken = wildcardToken.toBuilder().setJwt(TEST_JWT).build();
    when(signer.sign(eq(wildcardToken))).thenReturn(signedWildcardToken);
    FleetEngineToken nonWildcardToken = token.toBuilder().setAuthorizationClaims(nonWildcardClaim).build();
    FleetEngineToken signedNonWildcardToken = nonWildcardToken.toBuilder().setJwt(TEST_JWT).build();
    when(signer.sign(eq(nonWildcardToken))).thenReturn(signedNonWildcardToken);
    FleetEngineToken consumerToken2 = token2.toBuilder().setTokenType(FleetEngineTokenType.CONSUMER).build();
    FleetEngineToken signedConsumerToken2 = consumerToken2.toBuilder().setJwt(TEST_JWT).build();
    when(signer.sign(eq(consumerToken2))).thenReturn(signedConsumerToken2);
    when(expiryValidator.isTokenExpired(eq(signedNonWildcardToken), eq(EXPIRATION_WINDOW_DURATION))).thenReturn(false);
    NaiveAuthStateManager manager = new NaiveAuthStateManager(expiryValidator);
    // Sets the server token
    FleetEngineToken returnedServerToken = manager.signToken(signer, wildcardToken);
    // Sets the new consumerToken
    FleetEngineToken returnedConsumerToken = manager.signToken(signer, nonWildcardToken);
    // Since the consumerToken hasn't expired, the first consumerToken is returned
    FleetEngineToken returnedConsumerToken2 = manager.signToken(signer, consumerToken2);
    verify(signer, times(1)).sign(eq(wildcardToken));
    verify(signer, times(1)).sign(eq(nonWildcardToken));
    verify(signer, times(1)).sign(eq(consumerToken2));
    assertThat(returnedServerToken).isEqualTo(signedWildcardToken);
    assertThat(returnedConsumerToken).isEqualTo(signedNonWildcardToken);
    assertThat(returnedConsumerToken2).isEqualTo(signedConsumerToken2);
    verifyNoMoreInteractions(ignoreStubs(expiryValidator));
}
Also used : FleetEngineToken(com.google.fleetengine.auth.token.FleetEngineToken) Test(org.junit.Test)

Example 27 with FleetEngineToken

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

the class NaiveAuthStateManagerTest method signWildcardToken_returnsNewToken.

@Test
public void signWildcardToken_returnsNewToken() throws SigningTokenException {
    FleetEngineToken signedToken = token.toBuilder().setJwt(TEST_JWT).build();
    when(signer.sign(eq(token))).thenReturn(signedToken);
    NaiveAuthStateManager manager = new NaiveAuthStateManager(expiryValidator);
    FleetEngineToken returnedToken = manager.signToken(signer, token);
    assertThat(returnedToken).isEqualTo(signedToken);
    // Throw exception on any interaction that wasn't stubbed
    verifyNoMoreInteractions(ignoreStubs(expiryValidator));
}
Also used : FleetEngineToken(com.google.fleetengine.auth.token.FleetEngineToken) Test(org.junit.Test)

Example 28 with FleetEngineToken

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

the class NaiveAuthStateManagerTest method signConsumerToken_returnsNewToken.

@Test
public void signConsumerToken_returnsNewToken() throws SigningTokenException {
    token = token.toBuilder().setTokenType(FleetEngineTokenType.CONSUMER).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 29 with FleetEngineToken

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

the class NaiveAuthStateManagerTest method signWildcardToken_whenTokensDifferentType_returnsNewToken.

@Test
public void signWildcardToken_whenTokensDifferentType_returnsNewToken() throws SigningTokenException {
    FleetEngineToken signedToken = token.toBuilder().setJwt(TEST_JWT).build();
    when(signer.sign(eq(token))).thenReturn(signedToken);
    token2 = token.toBuilder().setTokenType(FleetEngineTokenType.CONSUMER).build();
    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(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(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 30 with FleetEngineToken

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

the class FleetEngineAuthClientInterceptor method addAuthorizationHeader.

/**
 * Adds the signed base64 encode JWT to the header.
 */
@VisibleForTesting
void addAuthorizationHeader(Metadata headers) {
    try {
        FleetEngineToken token = tokenProvider.getSignedToken();
        headers.put(AUTHORIZATION_HEADER, String.format("Bearer %s", token.jwt()));
    } catch (SigningTokenException e) {
        throw new WritingAuthorizationHeaderException("Exception while getting token.", e);
    }
}
Also used : SigningTokenException(com.google.fleetengine.auth.token.factory.signer.SigningTokenException) FleetEngineToken(com.google.fleetengine.auth.token.FleetEngineToken) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

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