Configure Keycloak for built-in SSO
Before P4 Server can use built-in single sign-on (SSO), Keycloak requires a realm and a client that represent P4 Server. You also map Keycloak users to P4 Server usernames and, optionally, connect Keycloak to your organization's identity provider.
Before you begin
Ensure that you have the following:
-
A running Keycloak instance, version 26.2 or later, that P4 Server can reach over HTTPS.
-
If you don't have Keycloak yet, see the Keycloak Server Installation guides.
-
The URL at which users reach the P4 Web Server. You use the URL to build the redirect URI.
Create a realm
A realm is a space in Keycloak that manages a set of users and clients. You can use an existing realm or create a new realm for P4 Server.
-
In the Keycloak administration console, open the realm list and click Create realm.
-
Enter a realm name and click Create.
-
Note the realm URL, which has the form
https://<keycloak-host>/realms/<realm>. For example,https://keycloak.example.com/realms/perforce. This URL becomes the value of theauth.idp.issuer_urlconfigurable.
Create a client for P4 Server
The client is the Keycloak record that represents the P4 Web Server. The client ID and the client secret that you create are the values that you give to P4 Server.
-
In the realm, click Clients, click Create client, and set Client type to OpenID Connect.
-
Enter a client ID, such as
p4server, and click Next. This client ID becomes the value of theauth.idp.client_idconfigurable. -
Turn on Client authentication. This setting makes the client a confidential client.
-
Under Authentication flow, select Standard flow.
-
In the client's Advanced settings, set Proof Key for Code Exchange Code Challenge Method to
S256. -
In the client's Advanced settings, set ID token signature algorithm to
ES256. P4 Server accepts only ID tokens that are signed with the algorithm that theauth.idp.id_token_signing_algconfigurable sets, and the default isES256. To keep the Keycloak default ofRS256instead, setauth.idp.id_token_signing_algtoRS256. -
In Valid redirect URIs, enter
https://<web-server-host>:<port>/auth/verify. Use the URL at which users reach the P4 Web Server. For example,https://p4.example.com:8443/auth/verify. Keycloak compares the redirect URI exactly, so the scheme, host, port, and path must match. -
Save the client.
-
Open the client's Credentials tab and copy the client secret.
Map users to P4 Server usernames
When a user logs in for the first time, P4 Server matches the Keycloak user to a P4 Server user through a claim in the token. By default, the claim is p4_username, which holds the P4 Server username. Alternatively, P4 Server can match the email claim to the email address of the P4 Server user. Keycloak doesn't provide a p4_username claim on its own, so you add it.
-
In the Keycloak administration console, click Realm settings, click the User profile tab, and create an attribute named
p4_username. -
Click Users, open each user, and in the Attributes section, set
p4_usernameto the user's P4 Server username. The value can differ from the Keycloak username. -
Click Client scopes, open a default client scope such as
profile, and add a mapper of type User Attribute. Set User Attribute and Token Claim Name top4_username, turn on Add to ID token and Add to userinfo, and turn off Add to access token.
Add the mapper to a default client scope, not an optional client scope. A mapper on an optional client scope doesn't add the claim to the token. P4 Server uses the claim only to link the users on first login, so the claim isn't required in the access token. A browser login reads the claim from the ID token. A command that runs with an access token reads the claim from the Keycloak userinfo endpoint.
Connect your organization's identity provider
If your users sign in with an identity provider that your organization manages, such as Microsoft Entra ID, Okta, or Ping Identity, connect that provider to Keycloak. Keycloak then passes each login to the upstream identity provider.
-
In the Keycloak administration console, click Identity providers and add the provider as an OpenID Connect or SAML provider. For the steps, see Integrating identity providers in the Keycloak documentation.
-
In the provider's Mappers tab, add a mapper of type Attribute Importer that imports the
p4_usernameclaim from the upstream provider into thep4_usernameuser attribute. Set Sync mode override to Force.
To learn more about Keycloak, see the Keycloak documentation.
Next steps
After you configure Keycloak, configure P4 Server to use it. For more information, see Configure P4 Server for built-in SSO.