Authentication
The P4 REST API supports Basic authentication and, when Identity Provider (IdP) authentication is configured, Bearer authentication using JSON Web Tokens (JWTs).
When using Basic authentication, provide a P4 Server username and a valid P4 ticket in the HTTP Authorization header.
When IdP authentication is configured, obtain a JWT through the IdP authentication flow and provide the JWT in the HTTP Authorization header using the Bearer authentication scheme.
Basic authentication continues to be supported and is not being deprecated. Customers who have not configured IdP authentication can continue to authenticate using P4 tickets.
Authenticate using a P4 ticket
To obtain a P4 ticket that can be used for authenticated REST API requests, generate a ticket that is valid for all hosts:
p4 login -a -p username
Basic authentication header format
For Basic authentication, combine the P4 Server username and P4 ticket using a colon (:) separator:
web.user:6BDB56A0949EC1F9FC561C4A3E633FAC
Base64-encode the resulting string:
d2ViLnVzZXI6NkJEQjU2QTA5NDlFQzFGOUZDNTYxQzRBM0U2MzNGQUM=
Send the encoded value in the HTTP Authorization header:
Authorization: Basic d2ViLnVzZXI6NkJEQjU2QTA5NDlFQzFGOUZDNTYxQzRBM0U2MzNGQUM=
You can then include this header in authenticated requests to the P4 REST API.
Authenticate using an Identity Provider (IdP)
Before you begin:
-
IdP authentication is configured on the server, including the issuer URL and
auth.idp.audience. -
Users access the web server over HTTPS, either directly or through a reverse proxy that terminates TLS. The
auth.idp.web_urisetting should be configured with this HTTPS address. -
The user has signed in at least once with p4 login through the IdP. This links the IdP account to the P4 user. Until this occurs, Bearer tokens for that user are rejected.
Use a reverse proxy that terminates TLS
The web server accepts sign-in requests (/auth/login), token refresh requests (/auth/refresh), and Bearer-authenticated API requests (/api/v0) on its HTTP listener. As a result, you can run the web server behind a reverse proxy that terminates TLS.
Set auth.idp.web_uri to the HTTPS address that users reach through the proxy, including any path prefix.
If auth.idp.web_uri includes a path, configure the proxy to forward <path>/liveness as well as <path>/auth/. Before p4 login opens the browser, it checks <auth.idp.web_uri>/liveness.
The web server serves HTTP only when you specify a tcp prefix on the -p option (tcp:, tcp4:, tcp6:, tcp46:, or tcp64:).
For example:
p4 webserver start -p tcp:127.0.0.1:3000
-
On a server that uses SSL,
-pwithout a prefix starts the web server with HTTPS. -
On a server that does not use SSL, the server refuses
-pwithout atcpprefix, because the server has no certificate for HTTPS.
The same rule applies to a web server that a startup.N configurable starts. On a server that does not use SSL, add the tcp prefix to each existing startup.N entry that starts the web server. Otherwise, the web server does not start.
To learn more, see p4 webserver in the P4 CLI Reference.
Obtain and use an access token
To obtain an access token:
-
In a browser, open:
<auth.idp.web_uri>/auth/login -
Sign in through the configured IdP.
-
After successful authentication, the web server returns a JSON response containing authentication tokens.
-
Copy the
access_tokenvalue. The response also contains an id_token, which is not used for REST API requests.
Include the access token in subsequent requests using the Authorization header:
Authorization: Bearer <access_token>
When the access token expires, send a POST request to /auth/refresh with:
-
The expired access token in the Authorization header.
-
The corresponding
refresh_tokenin the request body.
The response returns a new set of tokens, including a new refresh token for future use.
"Your sign-in could not be verified because the browser did not return the sign-in cookies. Browsers only return them over HTTPS: reach this server over HTTPS, or through a reverse proxy that terminates TLS. The cookies also expire if the sign-in takes longer than five minutes."