This article shows how single sign on with OpenID Connect and Keycloak can be set up and how it works.
Register a new client:
Configure
>Clients
>Create
Client ID
: We recommend using the domain nameClient Protocol
:openid-connect
Root URL
: The base URL of your site (WordPress)Configure
>Clients
>{client_id}
>Settings
Access Type
:confidential
Direct Access Grants Enabled
:off
- The defaults are typically fine for the rest.
Configure
>Clients
>{client_id}
>Credentials
Client Authenticator
:Client Id and Secret
- Copy your
Secret
for later
IMPORTANT: Once you've enabled the OpenID Connect Generic plugin, the login via username and password is blocked. So make sure you keep an authenticated admin session open while testing your setup. We therefore recommend testing the SSO authentication in a browser window in private browsing mode.
Configure the OpenID Connect Generic plugin:
- Install and activate the OpenID Connect Generic plugin.
- Navigate to Settings > OpenID Connect Client and configure the plugin as follows:
Login Type
:Auto Login - SSO
Client ID
: The client ID you've configured in KeycloakClient Secret Key
: The secret you've copied from KeycloakOpenID scope
:email profile openid
Login Endpoint URL
:https://{keycloak-domain-name}/auth/realms/{realm}/protocol/openid-connect/auth
( replace{keycloak-domain-name}
and{realm}
)Userinfo Endpoint URL
:https://{keycloak-domain-name}/auth/realms/{realm}/protocol/openid-connect/userinfo
Token Validation Endpoint URL
:https://{keycloak-domain-name}/auth/realms/{realm}/protocol/openid-connect/token
End Session Endpoint URL
:https://{keycloak-domain-name}/auth/realms/{realm}/protocol/openid-connect/logout
Identity Key
:preferred_username
Nickname Key
:preferred_username
Email Formatting
:{email}
Display Name Formatting
:{given_name} {family_name}
Identify with User Name
:false
Link Existing Users
:true
Create user if does not exist
:true
This setup assumes that:
- You manage users and their roles entirely in WordPress.
- Every user of WordPress has a corresponding account in Keycloak.
- Only users that have a WordPress account should be able to log in (despite the
Create user if does not exist
:true
setting.) - The email address of the users in Keycloak and WordPress are (at least initially) identical.
- You won't use the default WordPress login anymore (as long as the OpenID Connect Client Generic plugin is activated).
User mapping:
- On their first sso-login the plugin matches the Keycloak user with the WordPress user by the email address. It then stores the Keycloak's user id in WordPress so the users email address doesn't matter any longer and can now differ in both systems.
- This linking process needs the settings
Link Existing Users
:true
ANDCreate user if does not exist
:true
to work properly. - There is no role mapping. Keycloak serves solely as identity provider. The users role must be managed in WordPress.
Custom modifications:
-
All sso related modifications are located in
{theme_directory}/lib/tweaks/openid-connect-generic.php
-
We do three things:
- Disable automatic account creation for SSO users
(
openid-connect-generic-user-creation-test
filter) - Disable WordPress' standard username and password authentication
(
wp_authenticate_user
filter) - Rename the sso login button (which is only visible after failed tries)
(
openid-connect-generic-login-button-text
filter)
- Disable automatic account creation for SSO users
(