Search in sources :

Example 1 with FleetEngineTokenType

use of com.google.fleetengine.auth.token.FleetEngineTokenType 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)

Aggregations

FleetEngineToken (com.google.fleetengine.auth.token.FleetEngineToken)1 FleetEngineTokenType (com.google.fleetengine.auth.token.FleetEngineTokenType)1 SigningTokenException (com.google.fleetengine.auth.token.factory.signer.SigningTokenException)1