Search in sources :

Example 1 with FleetEngineToken

use of com.google.fleetengine.auth.token.FleetEngineToken in project java-on-demand-rides-deliveries-stub-provider by googlemaps.

the class TokenServlet method doGet.

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    ServletUtils.setStandardResponseHeaders(response);
    String tokenType = request.getPathInfo().substring(1);
    FleetEngineTokenType tokenTypeEnum;
    try {
        tokenTypeEnum = FleetEngineTokenType.valueOf(Ascii.toUpperCase(tokenType));
    } catch (IllegalArgumentException e) {
        logger.warning(String.format("Requested token for tokenType [%s], but did not find token.", tokenType));
        response.sendError(HttpServletResponse.SC_BAD_REQUEST, String.format("Could not find token for the given type: %s", tokenType));
        return;
    }
    FleetEngineToken authToken;
    try {
        switch(tokenTypeEnum) {
            case DRIVER:
                authToken = this.minter.getDriverToken(VehicleClaims.create());
                break;
            case CONSUMER:
                authToken = this.minter.getConsumerToken(TripClaims.create());
                break;
            default:
                logger.severe("Requested token for tokenType [%s], but it should not get here.");
                response.sendError(HttpServletResponse.SC_BAD_REQUEST, String.format("Could not find token for the given type: %s", tokenType));
                return;
        }
    } catch (SigningTokenException exception) {
        throw new SampleProviderException("Error signing token", exception);
    }
    logger.info(String.format("Found token for type %s: %s", tokenType, authToken.jwt()));
    response.getWriter().print(GsonProvider.get().toJson(authToken));
    response.getWriter().flush();
}
Also used : FleetEngineTokenType(com.google.fleetengine.auth.token.FleetEngineTokenType) SigningTokenException(com.google.fleetengine.auth.token.factory.signer.SigningTokenException) FleetEngineToken(com.google.fleetengine.auth.token.FleetEngineToken)

Example 2 with FleetEngineToken

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

the class FleetEngineTokenFactoryTest method createCustomToken_returnsCustomTokenType.

@Test
public void createCustomToken_returnsCustomTokenType() {
    FleetEngineTokenFactory factory = new FleetEngineTokenFactory(clock, FleetEngineTokenFactorySettings.builder().build());
    FleetEngineToken signedToken = factory.createCustomToken(TripClaims.create(TEST_TRIP_ID));
    assertThat(signedToken.tokenType()).isEqualTo(CUSTOM);
}
Also used : FleetEngineToken(com.google.fleetengine.auth.token.FleetEngineToken) Test(org.junit.Test)

Example 3 with FleetEngineToken

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

the class FleetEngineTokenFactoryTest method createTrustedDriverToken_whenVehicleClaimsWildAndTaskClaimsNOTWild_claimIsNOTWild.

@Test
public void createTrustedDriverToken_whenVehicleClaimsWildAndTaskClaimsNOTWild_claimIsNOTWild() {
    FleetEngineTokenFactorySettings settings = FleetEngineTokenFactorySettings.builder().setAudience(FAKE_AUDIENCE).build();
    FleetEngineTokenFactory factory = new FleetEngineTokenFactory(clock, settings);
    FleetEngineToken token = factory.createTrustedDeliveryDriverToken(DeliveryVehicleClaims.create(), TaskClaims.create(TEST_TASK_ID));
    assertThat(token.authorizationClaims().isWildcard()).isFalse();
}
Also used : FleetEngineToken(com.google.fleetengine.auth.token.FleetEngineToken) Test(org.junit.Test)

Example 4 with FleetEngineToken

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

the class FleetEngineTokenFactoryTest method createTrustedDriverToken_whenVehicleClaimsNOTWildAndTaskClaimsWild_claimIsNOTWild.

@Test
public void createTrustedDriverToken_whenVehicleClaimsNOTWildAndTaskClaimsWild_claimIsNOTWild() {
    FleetEngineTokenFactorySettings settings = FleetEngineTokenFactorySettings.builder().setAudience(FAKE_AUDIENCE).build();
    FleetEngineTokenFactory factory = new FleetEngineTokenFactory(clock, settings);
    FleetEngineToken token = factory.createTrustedDeliveryDriverToken(DeliveryVehicleClaims.create(TEST_VEHICLE_ID), TaskClaims.create());
    assertThat(token.authorizationClaims().isWildcard()).isFalse();
}
Also used : FleetEngineToken(com.google.fleetengine.auth.token.FleetEngineToken) Test(org.junit.Test)

Example 5 with FleetEngineToken

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

the class FleetEngineTokenFactoryTest method createVehicleToken_returnsVehicleTokenType.

@Test
public void createVehicleToken_returnsVehicleTokenType() {
    FleetEngineTokenFactory factory = new FleetEngineTokenFactory(clock, FleetEngineTokenFactorySettings.builder().build());
    FleetEngineToken token = factory.createDriverToken(VehicleClaims.create(TEST_VEHICLE_ID));
    assertThat(token.tokenType()).isEqualTo(DRIVER);
}
Also used : 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