Using Firebase to authenticate users

This page describes how to support user authentication in API Gateway.

To authenticate a user, a client application must send a JSON Web Token (JWT) in the authorization header of the HTTP request to your backend API. API Gateway validates the token on behalf of your API, so you don't have to add any code in your API to process the authentication. However, you do need to configure the API config for your gateway to support your chosen authentication methods.

API Gateway validates a JWT in a performant way by using the JWT issuer's JSON Web Key Set (JWKS). The location of the JWKS is specified in the x-google-jwks_uri field of the gateway's API config. API Gateway caches the JWKS for five minutes and refreshes it every five minutes.

Before you begin

  • Add authentication code to your client application, following the Firebase authentication documentation. Firebase supports authentication by using passwords, phone numbers, and popular federated identity providers like Google, Facebook and Twitter.
    • When your client application sends an HTTP request, the authorization header in the request must contain the following JWT claims:
      • iss (issuer)
      • sub (subject)
      • aud (audience)
      • iat (issued at)
      • exp (expiration time)

Configuring API Gateway to support client authentication

You must have a security requirement object and a security definitions object in your API config for API Gateway to validate the claims in the signed JWT.

To support Firebase authentication:

  1. Add the following to the security definition in your API config, which follows the OpenAPI 2.0 security scheme:

     securityDefinitions:
        firebase:
          authorizationUrl: ""
          flow: "implicit"
          type: "oauth2"
          # Replace YOUR-PROJECT-ID with your project ID
          x-google-issuer: "https://securetoken.google.com/YOUR-PROJECT-ID"
          x-google-jwks_uri: "https://www.googleapis.com/service_accounts/v1/metadata/x509/securetoken@system.gserviceaccount.com"
          x-google-audiences: "YOUR-PROJECT-ID"
    
  2. Add a security section at either the API level to apply to the entire API, or at the method level to apply to a specific method.

     security:
        - firebase: []
    

You can define multiple security definitions in the API config, but each definition must have a different issuer. If you use security sections at both the API level and at the method level, the method-level settings override the API-level settings.

Making an authenticated call to an API Gateway API

When you send a request using an authentication token, we recommend that you put the token in the Authorization:Bearer header. For example:

curl --request POST \
  --header "Authorization: Bearer ${TOKEN}" \
  "${GATEWAY_URL}/echo"

Here, GATEWAY_URL and TOKEN are environment variables containing your deployed gateway URL and authentication token, respectively. See Making an authenticated request to an API Gateway API for sample code that sends a request using the Authorization:Bearer header.

If you cannot use the header when sending the request, you can put the authentication token in a query parameter called access_token. For example:

curl "${GATEWAY_URL}/echo?access_token=${TOKEN}"

Receiving authenticated results in your API

API Gateway usually forwards all headers it receives. However, it overrides the original Authorization header when the backend address is specified by x-google-backend in the API config.

API Gateway will send the authentication result in the X-Apigateway-Api-Userinfo to the backend API. It is recommended to use this header instead of the original Authorization header. This header is base64url encoded and contains the JWT payload.

What's next