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.

You must have administrator access to the Keycloak administration console.

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.

  1. In the Keycloak administration console, open the realm list and click Create realm.

  2. Enter a realm name and click Create.

  3. 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 the auth.idp.issuer_url configurable.

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.

  1. In the realm, click Clients, click Create client, and set Client type to OpenID Connect.

  2. Enter a client ID, such as p4server, and click Next. This client ID becomes the value of the auth.idp.client_id configurable.

  3. Turn on Client authentication. This setting makes the client a confidential client.

  4. Under Authentication flow, select Standard flow.

  5. In the client's Advanced settings, set Proof Key for Code Exchange Code Challenge Method to S256.

  6. 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 the auth.idp.id_token_signing_alg configurable sets, and the default is ES256. To keep the Keycloak default of RS256 instead, set auth.idp.id_token_signing_alg to RS256.

  7. 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.

  8. Save the client.

  9. Open the client's Credentials tab and copy the client secret.

Keep the client secret out of scripts and source control. To learn how to provide the client secret to P4 Server, see Configure P4 Server for built-in SSO.

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.

Keycloak discards user attributes that aren't defined in the realm's user profile, and the request still succeeds. If you skip step 1 in the following procedure, the claim never appears in the tokens and every first login fails.
  1. In the Keycloak administration console, click Realm settings, click the User profile tab, and create an attribute named p4_username.

  2. Click Users, open each user, and in the Attributes section, set p4_username to the user's P4 Server username. The value can differ from the Keycloak username.

  3. 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 to p4_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_username claim from the upstream provider into the p4_username user 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.