How to implement OpenID Connect authentication with 3rd party IDPs in a microservices architecture

The following answer does only apply for a OpenID Connect authentication flow with a 3rd party IDP (like Google). It does not apply for an architecture where you host your own IDP.

(There are some API gateways (e.g Tyk or Kong) which support OpenID Connect out of the box.)

You can use JWTs (ID token) to secure your APIs. However, this has one disadvantage. JWTs cannot be revoked easily.

I would not recommend this. Instead you should implement an OAuth2 authorization server which issues access tokens for your API. (In this case, you have two OAuth2 flows. One for authentication and one for authorization. The ID and access token from the IDP are used only for authentication.)

The following picture shows a setup where the API gateway and authentication/authorization server are two separate services. (As mentioned above, the authentication/authorization can also be done by the API gateway.)

The authentication flow (Authorization Code Grant) calls are marked blue. The authorization flow (Implicit Grant) calls are marked green.

OpenID Connect Authentication Flow

1: Your web app is loaded from the app server.

2a: The user clicks on your login button, your web app builds the authorization URL and opens it. (See: Authorization Request)

2b: Because the user hasn't authenticated and has no valid session with your authorization server, the URL he wanted to access is stored and your authorization server responds with a redirect to its login page.

3: The login page is loaded from your authorization server.

4a: The user clicks on "Login with ...".

4b: Your authorization server builds the IDP authorization URL and responds with a redirect to it. (See: Authentication Request)

5a: The IDP authorization URL is opend.

5b: Because the user hasn't authenticated and has no valid session with the IDP, the URL he wanted to access is stored and the IDP responds with a redirect to its login page.

6: The login page is loaded from the IDP.

7a: The user fills in his credentials and clicks on the login button.

7b: The IDP checks the credentials, creates a new session and responds with a redirect to the stored URL.

8a: The IDP authorization URL is opend again.

(The approval steps are ignored here for simplicity.)

8b: The IDP creates an authorization and responds with a redirect to the callback URL of your authorization server. (See: Authentication Response)

9a: The callback URL is opened.

9b: Your authorization server extracts the authorization code from the callback URL.

10a: Your authorization server calls the IDP's token endpoint, gets an ID and access token and validates the data in the ID token. (See: Token Request)

(10b: Your authorization server calls the IDP's user info endpoint if some needed claims aren't available in the ID token.)

11a/b: Your authorization server queries/creates the user in your service/DB, creates a new session and responds with a redirect to the stored URL.

12a: The authorization URL is opend again.

(The approval steps are ignored here for simplicity.)

12b/+13a/b: Your authorization server creates/gets the authorization (creates access token) and responds with a redirect to the callback URL of your web app. (See: Access Token Response)

14a: The callback URL is opened.

14b: Your web app extracts the access token from the callback URL.

15: Your web app makes an API call.

16/17/18: The API gateway checks the access token, exchanges the access token with an JWT (which contains user infos, ...) and forwards the call.

A setup where the authorization server calls the API gateway is also possible. In this case, after the authorization is done, the authorization server passes the access token and JWT to the API gateway. Here, however, everytime the user infos change the authorization server has to "inform" the API gateway.