This Keycloak authentication plugin is a middleware for Go applications, which manages user authentication using Keycloak, an open-source identity and access management solution.
The plugin uses several data structures:
The plugin has several main functions:
When a request is received, the middleware first checks for an "Authorization" cookie. If it exists and the token inside is valid, the request is allowed to proceed. If the token is invalid or doesn't exist, the middleware checks for an authorization code in the request URL. If the authorization code exists, the middleware exchanges it for a token, sets this token as a cookie, and redirects the user to their original location. If the authorization code doesn't exist, the middleware redirects the user to Keycloak for authentication. The user is then prompted to enter their credentials on the Keycloak login page. After successful authentication, Keycloak redirects the user back to the application with an authorization code. The middleware then exchanges this code for a token and the process starts over. By using this middleware, applications can easily integrate with Keycloak for user authentication without having to implement the logic themselves.
First, enable the plugins support in your Traefik configuration file (traefik.yml or traefik.toml):
experimental:plugins:keycloakopenid:moduleName: "github.com/Gwojda/keycloakopenid"version: "v0.1.34"
Usage Add the plugin's specific configuration to your Traefik routers:
http:middlewares:my-keycloakopenid:plugin:keycloakopenid:KeycloakURL: "my-keycloak-url.com" # <- Also supports complete URL, e.g. https://my-keycloak-url.com/authClientID: "<CLIENT_ID"ClientSecret: "<CLIENT_SECRET"KeycloakRealm: "<REALM"Scope: "<Scope [space deliminated] (default: 'openid', example: 'openid profile email')"TokenCookieName: "<TOKEN_COOKIE_NAME (default: 'AUTH_TOKEN')"UseAuthHeader: "<true|false (default: false)"IgnorePathPrefixes: "/api,/favicon.ico [comma deliminated] (optional)"
Alternatively, ClientID and ClientSecret can be read from a file to support Docker Secrets and Kubernetes Secrets:
http:middlewares:my-keycloakopenid:plugin:keycloakopenid:KeycloakURL: "https://my-keycloak-url.com/auth"ClientIDFile: "/run/secrets/clientId.txt"ClientSecretFile: "/run/secrets/clientSecret.txt"KeycloakRealm: "<REALM"Scope: "<SCOPE [space deliminated] (default: 'openid', example: 'openid profile email')"TokenCookieName: "<TOKEN_COOKIE_NAME (default: 'AUTH_TOKEN')"UseAuthHeader: "<true|false (default: false)"
Last but not least, each configuration can be read from environment file to support some Kubernetes configurations:
http:middlewares:my-keycloakopenid:plugin:keycloakopenid:KeycloakURLEnv: "MY_KEYCLOAK_URL"ClientIDEnv: "MY_KEYCLOAK_CLIENT_ID"ClientSecretEnv: "MY_KEYCLOAK_CLIENT_SECRET"KeycloakRealmEnv: "MY_KEYCLOAK_REALM"ScopeEnv: "SCOPE [space deliminated] (default: 'openid', example: 'openid profile email')"TokenCookieNameEnv: "TOKEN_COOKIE_NAME (default: 'AUTH_TOKEN')"UseAuthHeaderEnv: "USE_AUTH_HEADER (default: false)"
This plugin also sets a header with a claim from Keycloak, as it has become reasonably common. Claim name and header name can be modified.
The default claim is preferred_username
, the default header name is X-Forwarded-User
:
http:middlewares:my-keycloakopenid:plugin:keycloakopenid:KeycloakURL: "my-keycloak-url.com" # <- Also supports complete URL, e.g. https://my-keycloak-url.com/authClientID: "<CLIENT_ID"ClientSecret: "<CLIENT_SECRET"KeycloakRealm: "<REALM"Scope: "<SCOPE [space deliminated] (default: 'openid', example: 'openid profile email')"TokenCookieName: "TOKEN_COOKIE_NAME (default: "AUTH_TOKEN)"UseAuthHeader: "true|false (default: false)"UserClaimName: "my-uncommon-claim"UserHeaderName: "X-Custom-Header"