Using Google OAuth in authentik as an Identity Provider

What is authentik?

authentik is an IdP (Identity Provider) and SSO (Single Sign On) platform that is built with security at the forefront of every piece of code, every feature, with an emphasis on flexibility and versatility.

authentik is often used to centralize authentication across all your applications. Using Google as an Identity Provider (IdP), we can leverage authentik to forward authentication to Google so we don't have to register and deregister users manually in authentik.

In this blog post, we will go through how to set up authentik to implement the following flow: flow

We take a look at how to configure Google OAuth in authentik and create the respective Application and Provider to handle authentication to the Kube-API-Server. The Kube-API-Server is just taken as an example here. You can configure OAuth2/OIDC for every service where it is supported.

Prerequisites

Before starting, make sure you have:

  • A running authentik instance (version 2023.x or higher recommended).
  • Administrator access in authentik.
  • A Google account with access to the Google Cloud Console.
  • HTTPS enabled on your authentik installation (Google requires valid HTTPS redirect URLs).

Step 1 - Create a Google OAuth Client

Go to https://console.cloud.google.com/.

Navigate to:

  • APIs & Services -> Credentials
  • Click Create Credentials -> OAuth Client ID

If prompted, configure the OAuth consent screen:

  • Application Type: Web application
  • App name: e.g., authentik

Add your domain under Authorized redirect URIs:

  • https://<authentik-domain>/source/oauth/callback/<slug-from-the-provider>/ (Note the trailing / at the end).

The <slug-from-the-provider> will be created later in Step 2.

Save the configuration and note your:

  • Client ID
  • Client Secret

You’ll need both later in authentik.

Step 2 - Configure Google OAuth in authentik

Open your authentik dashboard.

Go to: Directory -> Federation and Social login

Click on Create.

Select Google OAuth Source.

Fill out the fields:

FieldValue
NameGoogle
Sluggoogle (This is <slug-from-the-provider>)
User matching modeUse the user's email address, but deny enrollment [...]
Group matching modeLink users on unique identifier
Protocol Settings > Consumer key<your Google Client ID>
Protocol Settings > Consumer secret<your Google Client Secret>
Protocol Settings > Scopesopenid email profile
Flow Settings > Authentication flowdefault-source-authentication
Flow Settings > Enrollment flowdefault-source-enrollment

Click on Finish.

Step 3 - Create a Provider

Go to: Applications -> Providers

Click on Create.

Select OAuth2 / OpenID Provider. Click Next.

Fill out the fields:

FieldValue
Provider NameKubeAPI-Server
Slugkubeapi-server
Protocol Settings > Client typeDecide if you want Confidential or Public
Protocol Settings > Redirect URIs/OriginsInsert your redirect URIs
Protocol Settings > Signing KeyChoose your own or authentik Self-signed Certificate
Advanced Flow Settings > Authentication flowdefault-authentication-flow
Advanced Flow Settings > Invalidation flowdefault-provider-invalidation-flow
Subject modeBased on the User's Email
Include claims in id_tokenyes

Click on Finish.

Step 4 - Create an Application

Go to: Applications -> Applications

Click on Create.

Fill out the fields:

FieldValue
NameKubeAPI-Server
Slugkubeapi-server
ProviderSelect the previously created provider

Click on Create.

Step 5 - Test

Now everything should be set up. Depending on the application you want to configure with OAuth you need to initiate the authentication process. In this example, we've used authentik to authenticate users for using the kubectl CLI locally to manage a remote Kubernetes cluster.

So in order to test our implementation, we simply execute:

kubectl get pods -A

The authentik dashboard opens with the Google logo at the bottom, allowing us to authenticate via Google OAuth and access the Kubernetes cluster with kubectl. Login

Troubleshooting

DNS

The DNS name of the authentik URL (<authentik-domain>) must be resolvable not only from the machine executing kubectl. It must be resolvable from the machine running authentik too. This might be the case automatically in your environment, but it is worth checking twice:

dig +short <authentik-domain>
10.0.130.240

If the command above does not show any IP address, the DNS name resolution does not work correctly.

Caching Issues

When testing, you might get errors like "401 Unauthorized" unexpectedly. If this happens, try removing the OIDC login cache:

rm -rf ~/.kube/cache/oidc-login/

Callback URIs

Check the callback URIs twice if authentication does not work. Sometimes, the error message tells you what URI it expects and what it retrieves. A common mistake is the missing trailing /.