This is a Traefik middleware plugin that authenticates users with Google OpenID Connect, and then checks that their email address or Google Workspace domain is authorized.
oauth2.googleapis.com and
www.googleapis.com. The middleware exchanges the login code at the first
host. It downloads Google's public signing keys from the second host to verify
each ID token, and caches the keys for the lifetime that Google states. Logins
fail if either host is not reachable.| Option | Default | Required | Description |
|---|---|---|---|
| oidc.clientID | X | OAuth client ID | |
| oidc.clientSecret | ** | OAuth client secret. Not recommended. Traefik shows this value in clear text in its dashboard, API, and debug log. Use oidc.clientSecretFile. | |
| oidc.clientSecretFile | ** | Recommended. Path to a file that contains the OAuth client secret. See Reading secrets from files. | |
| oidc.callbackPath | /oidc/callback | The path where the OIDC provider will redirect the user after authenticating. | |
| oidc.redirectHost | Optional host override for the OIDC redirect URI. Use this to configure a single, central redirect URI for multiple subdomains (e.g., auth.example.com). Requires cookie.domain to be set for cookie sharing. | ||
| oidc.returnURLOrigins | Optional list of sites that users can be sent back to after login. It applies only when oidc.redirectHost is set. If the list is empty, any site at cookie.domain or one of its subdomains is allowed. If the list is set, only the listed sites are allowed. Write each entry as scheme and host, plus the port if it is not the default, e.g., ["https://app1.example.com", "https://app2.example.com:8443"]. Do not add a path, wildcard, or trailing slash. http:// entries require cookie.insecure to be true. See Post-login return URLs. | ||
| oidc.prompt | A space-delimited, case-sensitive list of prompts to present the user. Possible values are: none, consent, select_account. See Google's docs for more info. | ||
| cookie.name | oidc_auth | Name of the cookie. It can be customized to avoid collisions when running multiple instances of the middleware. | |
| cookie.path | / | You can use this to limit the scope of the cookie to a specific path. Defaults to '/'. | |
| cookie.secret | ** | The HMAC key for cookie signing, which gives integrity protection to cookies. Not recommended. Traefik shows this value in clear text in its dashboard, API, and debug log. Use cookie.secretFile. | |
| cookie.secretFile | ** | Recommended. Path to a file that contains the HMAC key for cookie signing, which gives integrity protection to cookies. See Reading secrets from files. | |
| cookie.duration | 24h | Validity period for new cookies. Users are granted access for this length of time regardless of changes to user's account in the OIDC provider. Removing a user in Google does not invalidate an existing cookie, so keep this short relative to how quickly you need access revoked. Uses the Go time.Duration format. | |
| cookie.insecure | false | Set to true to omit the Secure attribute from cookies. | |
| cookie.sameSite | Lax | SameSite attribute for cookies. Options: Strict, Lax, None. Lax provides CSRF protection while allowing cookies on top-level navigation. | |
| cookie.domain | Domain attribute for cookies. Use this to share cookies across subdomains (e.g., .example.com). Must start with a dot. Required when using oidc.redirectHost. | ||
| authorized.emails | * | List of allowed email addresses. | |
| authorized.domains | * | List of allowed domains. This is matched against the hd claim of the user's Google Workspace account. | |
| authorized.allowAllAuthenticatedUsers | false | * | Grant access to any user who can authenticate with the OAuth client, without checking authorized.emails or authorized.domains. Cannot be combined with those options. See Allowing all authenticated users. |
| debug | false | Enable debug logging to stdout. |
* One of authorized.emails, authorized.domains, or
authorized.allowAllAuthenticatedUsers must be set. emails and domains can
be used together. allowAllAuthenticatedUsers cannot be used with either list.
The middleware will not start if none of them is set.
** Each secret is required. Set the file option (recommended) or the inline option, not both. See Reading secrets from files.
After a user signs in with Google, the middleware sends them back to the page they first asked for. The middleware limits where that return trip can go, so that a login cannot be used to send a user to a site you do not control.
oidc.redirectHostThere is nothing to configure. The user always returns to the same site where
the login started. The middleware remembers only the path and query of the
original page (for example, /reports?year=2026) and never a host name, so
the X-Forwarded-Host header cannot change the destination.
oidc.redirectHost (central callback)The login finishes on one host, such as auth.example.com, and the user must
then be sent to a different host, such as app1.example.com. The middleware
decides which hosts are acceptable in one of two ways:
| Mode | How to select it | Where users can be sent after login |
|---|---|---|
| Domain-based (default) | Leave oidc.returnURLOrigins unset | cookie.domain and any of its subdomains. With .example.com, that is example.com, app1.example.com, a.b.example.com, and so on. |
| Exact list | Set oidc.returnURLOrigins | Only the sites in the list. |
Domain-based is the convenient choice. A new application on the domain
works without a change to the middleware configuration. Use it only if you
trust every host under cookie.domain. A host does not have to be
protected by this middleware to receive a redirect, so an abandoned or
third-party-hosted subdomain is also a valid destination.
Exact list is the strict choice. The list replaces the domain-based rule, so you must add every site you protect, and update the list each time you add a site. Include the callback host itself if it also serves protected pages.
oidc.returnURLOriginsEach entry is an origin: the scheme, the host, and the port if it is not the default one. The middleware compares entries to the browser's address as exact text. A browser sends the host in lowercase and leaves out the default port, so an entry must do the same.
| Entry | Accepted | Reason |
|---|---|---|
https://app1.example.com | Yes | |
https://app1.example.com:8443 | Yes | A non-default port must be included. |
https://app1.example.com/ | No | No trailing slash. |
https://app1.example.com/admin | No | No paths. The whole site is allowed or it is not. |
https://*.example.com | No | No wildcards. Use the domain-based mode instead. |
https://App1.Example.com | No | The host must be lowercase. |
https://app1.example.com:443 | No | Leave out the default port (443 for https, 80 for http). |
http://app1.example.com | No | Accepted only when cookie.insecure is true. |
In both modes, http destinations are allowed only when cookie.insecure is
true. The middleware will not start if an entry is not valid.
400 Invalid return URL and does not start the login. It does the same for
a request path that a browser could read as another host. Such a path
starts with // or /\. With debug enabled, the log shows the rejected
host and the reason.401 Not authorized.X-Forwarded-Proto and X-Forwarded-Host
headers that Traefik sets. If Traefik is configured to trust these headers
from another proxy, that proxy must set them correctly.X-Forwarded-User
When the middleware proxies a request it adds an X-Forwarded-User header
containing the user's email address. This can be used by the downstream service
to identify the authenticated user.
If you want your JSON access logs to include the user's email address then
configure the access log to retain the X-Forwarded-User header. Here is a
CLI example:
# Adding X-Forwarded-User to JSON access logs.
--accesslog
--accesslog.format=json
--accesslog.fields.headers.names.X-Forwarded-User=keep
The resulting access log will contain a request_X-Forwarded-User field.
See Limiting the Fields/Including Headers for more details.
Static config
# traefik.ymlexperimental:plugins:google-oidc-auth-middleware:moduleName: "github.com/andrewkroh/google-oidc-auth-middleware"# Populate this with the latest release tag.version: vX.Y.Z
Dynamic config
# dynamic.ymlhttp:middlewares:oidc-auth:plugin:google-oidc-auth-middleware:oidc:clientID: example.apps.googleusercontent.comclientSecretFile: /run/secrets/oidc_client_secretcookie:secretFile: /run/secrets/oidc_cookie_secretauthorized:emails:domains:- example.comrouters:my-router:rule: host(`localhost`)service: service-fooentryPoints:- webmiddlewares:- oidc-auth
We recommend that you always use oidc.clientSecretFile and
cookie.secretFile. The inline oidc.clientSecret and cookie.secret options
continue to work for existing configurations, but they are not safe. Traefik
does not treat plugin configuration as secret, and it shows the inline values
in clear text in:
/api/http/middlewares and
/api/rawdata),DEBUG level, which prints the dynamic configuration,docker inspect shows.A person who has the cookie secret can make a valid cookie for any email address. With the file options, Traefik shows only the paths.
The files must be readable by the Traefik process. With Kubernetes, mount them from a Secret volume. With Docker Compose, use secrets:
# docker-compose.ymlsecrets:oidc_client_secret:file: ./secrets/oidc_client_secretoidc_cookie_secret:file: ./secrets/oidc_cookie_secretservices:traefik:image: traefik:v3secrets:- oidc_client_secret- oidc_cookie_secret
Compose mounts each secret at /run/secrets/<name>, which are the paths in the
example config.
Set authorized.allowAllAuthenticatedUsers: true to grant access to any user
who can complete the OAuth flow with your OAuth client. The middleware does
not check an allowlist in this mode.
Use this option only when the OAuth client itself restricts who can authenticate. For example, a Google OAuth client whose user type is set to Internal lets only members of your Google Workspace organization sign in. If the client is External, this option gives access to every Google account on the internet.
Authentication is not changed by this option. Users still need a valid, signed,
unexpired cookie, Google must report the email address as verified, and the
X-Forwarded-User header is still set.
When the option is enabled, the middleware always writes a warning to the
Traefik log at startup, even when debug is false.
We still recommend that you set authorized.domains instead of this option
when you can, as defense in depth. You can change the client's audience from
Internal to External with one setting in the Google Cloud console, and that
change leaves no trace in your Traefik configuration. A domain allowlist
continues to protect you if that happens.
If your Workspace organization has secondary domains, note that Google sets a
different hd claim for each domain. You must list each domain in
authorized.domains.
# dynamic.ymlhttp:middlewares:oidc-auth:plugin:google-oidc-auth-middleware:oidc:# This OAuth client must be restricted to an internal audience.clientID: example.apps.googleusercontent.comclientSecretFile: /run/secrets/oidc_client_secretcookie:secretFile: /run/secrets/oidc_cookie_secretauthorized:allowAllAuthenticatedUsers: true
When protecting multiple subdomains (e.g., app1.example.com, app2.example.com, app3.example.com) under the same parent domain, you can configure a single central redirect URI instead of registering each subdomain individually with your OAuth provider.
This feature requires two settings:
oidc.redirectHost: Set this to a central host that will handle all OIDC callbacks (e.g., auth.example.com)cookie.domain: Set this to share cookies across all subdomains (e.g., .example.com)Optionally set oidc.returnURLOrigins to limit users to an exact list of
return origins rather than any host within cookie.domain.
# dynamic.ymlhttp:middlewares:oidc-auth:plugin:google-oidc-auth-middleware:oidc:clientID: example.apps.googleusercontent.comclientSecretFile: /run/secrets/oidc_client_secretredirectHost: auth.example.com # Central callback host# Optional: restrict post-login redirects to these exact origins.# Without this list, any host within .example.com is allowed.# returnURLOrigins:# - https://app1.example.com# - https://app2.example.com# - https://app3.example.comcallbackPath: /oidc/callbackcookie:secretFile: /run/secrets/oidc_cookie_secretdomain: .example.com # Share cookies across *.example.comauthorized:emails:domains:- example.comrouters:# Router for the central callback hostauth-callback:rule: Host(`auth.example.com`) && Path(`/oidc/callback`)service: noop@internalentryPoints:- webmiddlewares:- oidc-auth#tls: ...# Routers for protected subdomainsapp1:rule: Host(`app1.example.com`)service: service-app1entryPoints:- webmiddlewares:- oidc-authapp2:rule: Host(`app2.example.com`)service: service-app2entryPoints:- webmiddlewares:- oidc-authapp3:rule: Host(`app3.example.com`)service: service-app3entryPoints:- webmiddlewares:- oidc-auth
In your Google OAuth console, you only need to register one authorized redirect URI:
https://auth.example.com/oidc/callback
Instead of having to register:
https://app1.example.com/oidc/callbackhttps://app2.example.com/oidc/callbackhttps://app3.example.com/oidc/callbackhttps://app1.example.comredirect_uri=https://auth.example.com/oidc/callbackhttps://auth.example.com/oidc/callbackDomain=.example.com (shared across all subdomains)https://app1.example.com*.example.com)example.com vs example.org) is not supported