Use access tokens with built-in SSO

Scripts, build systems, and REST clients can authenticate with a Keycloak access token instead of a ticket. An access token is a JSON Web Token (JWT).

Run commands with an access token

To run a command with an access token, use the -T option. For example:

p4 -T <token> info

Alternatively, set the P4ACCESSTOKEN environment variable to the token. To learn about the option and the variable, see Global options and P4ACCESSTOKEN in the P4 CLI Reference.

The -T option takes precedence over P4ACCESSTOKEN. If the server refuses the token, the server checks for a valid ticket or password instead.

The token must meet the following requirements:

  • The issuer configured in auth.idp.issuer_url must have issued the token.

  • The audience of the token must match auth.idp.audience.

P4 Server validates the token itself and does not check with Keycloak. A revoked token continues to work until it expires. Keep the lifetimes of tokens short.

Set up a service account

To let an automated process, such as a build system, run commands without a password or a long-lived ticket, set up a service account. The process gets a token from Keycloak with the client credentials grant.

To set up a service account, for example build-bot, follow these steps:

  1. In Keycloak, create a confidential client, for example build-bot, with Service accounts roles turned on.

  2. Set the p4_username attribute of the service account user of the client, and ensure that a p4_username mapper with Add to userinfo turned on applies to the client. P4 Server reads the claim from the Keycloak userinfo endpoint, not from the access token. To learn how, see Configure Keycloak for built-in SSO.

  3. Create the matching P4 Server user.

  4. In the user form, set AuthMethod: idp. To learn how, see Set up users for built-in SSO.

  5. Store the client secret in an environment variable, for example BUILD_BOT_SECRET.

  6. To get a token with the client credentials grant, run the following command:

    curl -s -d grant_type=client_credentials -d scope=openid -d client_id=build-bot -d client_secret="$BUILD_BOT_SECRET" https://keycloak.example.com/realms/perforce/protocol/openid-connect/token

    The access_token field of the response is the token. Include scope=openid in the request. On the first use of a token, P4 Server reads the claim from the userinfo endpoint, and Keycloak serves userinfo only for a token with the openid scope.

  7. Run the command with the token and the P4 Server username of the service account. P4 Server doesn't take the username from the token, so set P4USER or use the -u option. For example, if the TOKEN environment variable contains the token:

    p4 -u build-bot -T "$TOKEN" sync

The client credentials grant returns an access token without a refresh token. P4 Server doesn't store the token or issue a ticket for the service account. When the token expires, get a new token from Keycloak.

Authenticate REST API requests

REST API clients send the token in the Authorization header:

Authorization: Bearer <JWT>

The REST API accepts a token only if the Keycloak user is already linked to a P4 Server user. The REST API doesn't make the link on first use, and refuses the token with IdP token is invalid. To make the link, log in one time with p4 login, or run a p4 command with the token.

To learn more, see Authentication.