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