Skip to content
Application identity

Workload Identity

Authenticate workloads to Keycard with platform-issued identity tokens, without any long-lived secrets.

Workloads running on a cloud or CI platform can authenticate to Keycard with the identity token their platform already issues them, without any long-lived secrets. The workload presents that token as a client assertion and names the Application credential it authenticates as with client_id.

  1. The platform issues a token. The workload requests a short-lived JWT from its platform, with your Zone issuer URL as the audience. If the platform sets the audience itself, set the credential’s allowed audience to that value.
  2. The workload calls the token endpoint. It sends a standard OAuth request to /oauth/2/token with client_assertion set to the platform token and client_id set to the credential ID.
  3. Keycard validates the token. Keycard looks up the credential by client_id, verifies the token’s signature, issuer, and expiry against the credential’s Provider, then checks the audience, subject, and authentication condition. Any failure returns invalid_client, and the Audit Log records details on the failure.
  4. Keycard issues a token. The workload receives a token for its Application. The client_credentials grant returns a token for the Application itself. The authorization_code grant uses the same client authentication to return tokens on behalf of a User.

The credential’s Client ID is an identifier, not a secret. Store it alongside other configuration as KEYCARD_CLIENT_ID.

The Provider represents the platform that issues the workload’s tokens. It specifies the issuer and where Keycard gets the keys that verify the token’s signature, read from the first of these that’s set:

  1. Stored keys: a key set stored on the Provider, for issuers Keycard can’t reach, such as a private Kubernetes cluster.
  2. Key set URL: a URL Keycard fetches the key set from.
  3. Discovery: the key set URL in the issuer’s <issuer-url>/.well-known/openid-configuration document.

Most platforms publish a discovery document, so their Provider sets only issuer. Create it with the Keycard CLI:

Terminal window
keycard agent api /zones/<zone-id>/providers --org <org-id> -X POST -d '{
"name": "<provider-name>",
"identifier": "<issuer-url>",
"protocols": {
"oauth2": {
"issuer": "<issuer-url>"
}
}
}'

See the API Reference for the jwks and jwks_uri fields.

A token credential belongs to one Application and trusts one Provider. Create it with the Keycard CLI:

Terminal window
keycard agent api /zones/<zone-id>/application-credentials --org <org-id> -X POST -d '{
"type": "token",
"version": 2,
"application_id": "<application-id>",
"provider_id": "<provider-id>",
"subject": "<subject>",
"allowed_audience": "<audience>",
"authentication_condition": "<cel-expression>"
}'
Field Validation
subject The token’s sub must match exactly. A trailing * matches every sub that starts with the text before it, for example repo:<org>/*.
allowed_audience The token’s aud must be exactly this one value. Defaults to the Zone issuer URL.
authentication_condition A CEL expression over the token’s claims. It must evaluate to true. A missing claim or evaluation error denies the request.

Set subject, authentication_condition, or both. Several credentials can trust the same subject, because the workload names its credential with client_id.

authentication_condition is a CEL expression over the token’s verified claims, available as claims. Reference a claim as claims.<claim-key>:

Check Expression
Claim equals a value claims.<claim-key> == "<claim-value>"
All of several claims claims.<claim-key> == "<claim-value>" && claims.<other-claim-key> == "<other-value>"
Claim is one of a set claims.<claim-key> in ["<value-1>", "<value-2>"]
Claim starts with a prefix ending at a segment boundary such as : or / claims.<claim-key>.startsWith("<prefix>/")
Array claim contains a value "<claim-value>" in claims.<claim-key>
Nested claim claims.<parent-key>.<child-key> == "<claim-value>"
Key with -, :, or . claims["<claim-key>"] == "<claim-value>"

Match the claim’s JSON type: quote strings, leave numbers and booleans unquoted. Reading a claim the token doesn’t have denies the request. To make a claim optional, guard it: !has(claims.<claim-key>) || claims.<claim-key> == "<claim-value>". For a key with -, :, or ., guard with !("<claim-key>" in claims) instead, because has() takes only the dot form.

A workload signs in to Keycard with the short-lived token its platform gives it, without any long-lived secrets. Keycard checks that token against the Workload Identity credential, then issues a token.

In code, give the SDK two things:

  • The platform token, or where to read it, such as a file or the cloud’s metadata server.
  • The credential’s Client ID, from KEYCARD_CLIENT_ID.

The SDK sends both with each token request. This works for each way an Application gets a token:

To do this Flow
Get a token for the Application itself Client credentials
Sign a User in through the browser Authorization code
Sign a User in on another device, such as a CLI Device code
Trade a token you hold for a token to a Resource Token exchange

The examples below show each flow. The exact call differs by SDK and version.

Without an SDK

Add these parameters to the token request (RFC 7523):

Parameter Value
client_assertion The platform token
client_assertion_type urn:ietf:params:oauth:client-assertion-type:jwt-bearer
client_id The credential’s Client ID
import asyncio
import os
from keycardai.oauth import AsyncClient, GrantType
from keycardai.oauth.server import FileTokenSource
token_source = FileTokenSource(token_file_path="<token-file-path>")
async def get_access_token() -> str:
async with AsyncClient(os.environ["KEYCARD_ISSUER"]) as client:
response = await client.client_credentials_grant(
client_assertion=await token_source.identity_token(),
client_assertion_type=GrantType.JWT_BEARER_CLIENT_ASSERTION,
client_id=os.environ["KEYCARD_CLIENT_ID"],
resource=os.environ["KEYCARD_RESOURCE_URL"],
)
return response.access_token
access_token = asyncio.run(get_access_token())

Pass a Workload Identity credential to the code exchange. Without any long-lived secrets, it sends the client assertion and credential ID. The code is bound to the client_id in the authorize URL, so use the credential ID there too.

import os
from keycardai.oauth import AsyncClient
from keycardai.oauth.server import FileTokenSource, WorkloadIdentity
credential = WorkloadIdentity(
FileTokenSource(token_file_path="<token-file-path>"),
client_id=os.environ["KEYCARD_CLIENT_ID"],
)
async with AsyncClient(os.environ["KEYCARD_ISSUER"]) as client:
tokens = await client.exchange_authorization_code(
code=code,
code_verifier=code_verifier,
redirect_uri="<redirect-uri>",
credential=credential,
)