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