← Back to case studies

Designing a secure PAT architecture for Product

Case study02

CLI clients, automation, and external integrations cannot use a browser session, but still need durable API credentials that are revocable, scoped, auditable, and isolated to an organization.

Added Personal Access Tokens as a separate authentication path. The BFF generates and hashes each token, validates it at the external boundary, and passes trusted identity context to internal APIs.

Machine clients can authenticate without browser sessions or Auth0 credentials, while token lifecycle controls and backend authorization keep access bounded and traceable.

01020903080410070506
01CLI / Automation02Personal Access Token03BFF Auth04Hash + validate token05PostgreSQL / CNPG06Product APIs07Organization + scopes08RBAC / Casbin09Authorized operation10Audit eventA PAT is a Product credential, not an Auth0 token. The BFF hashes it and checks the stored record, then passes trusted identity context, never the raw token, to APIs that still enforce scopes and RBAC.
  1. Bearer credential
  2. PAT presented to BFF Auth
  3. Hash the token
  4. Look up the hash
  5. Known, active, unexpired
  6. Trusted identity context
  7. Organization + scopes
  8. Casbin RBAC
  9. Authorized operation
  10. Audit event recorded

A credential for non-browser clients

Browser sessions use Auth0 through the BFF, but a CLI, deployment pipeline, or external integration has no browser session to present. A separate Product-issued PAT gives these clients a credential designed for API requests without turning browser sessions into automation secrets. The PAT is validated by BFF Auth, not as an Auth0 access token.

Clients send the PAT as a Bearer credential. The BFF remains the external authentication boundary, and successful authentication converges on the same backend authorization model used by browser requests.

Generate once, store a hash

When a user creates a token, the BFF generates a cryptographically random secret with a recognizable prefix such as mt_live_. It stores a SHA-256 hash and token metadata in PostgreSQL, then returns the raw PAT once to the user.

The raw value is not recoverable from the database. If it is lost, the user creates a replacement rather than retrieving the original credential.

Keep the token lifecycle manageable

Each record is associated with a user and organization, and can include scopes, a display prefix, creation and last-used timestamps, an expiration time, and a revocation timestamp. PostgreSQL is the durable source of truth for these records; Redis remains suited to short-lived state such as sessions and rate limits.

A token is rejected if it is unknown, expired, or revoked. Expiration limits its useful lifetime, and explicit revocation disables it immediately without affecting other credentials owned by the user.

Validate at the authentication boundary

For each request, the BFF extracts the Bearer PAT, hashes it, looks up the matching record, and checks expiration and revocation before accepting it. Invalid credentials stop at the boundary with 401 Unauthorized.

After validation, the BFF forwards trusted identity context rather than the raw token. Internal APIs and agent services therefore do not need to process or propagate the external secret.

0102030604080507
01Incoming API request02Extract bearer PAT03Hash PAT04Lookup token hash05Known, active, unexpired?06401 Unauthorized07Trusted identity context08Product APIThe BFF extracts the bearer PAT, hashes it and looks up the record. Unknown, expired or revoked tokens stop with 401; accepted ones continue as identity context, so internal services never see the raw secret.
  1. Bearer PAT extracted
  2. Hash the PAT
  3. Look up the token hash
  4. Known, active, unexpired?
  5. Yes: build identity context
  6. API never sees the raw token
  7. No: 401 Unauthorized

Authentication is not authorization

A valid PAT proves that the request has a recognized Product credential; it does not grant every permission available to its owner. The BFF carries user, organization, token, and scope context to backend APIs, where scope checks and Casbin RBAC still decide whether an operation is allowed.

Organization context prevents a token from crossing tenant boundaries. Audit events can include the user, organization, token ID, operation, timestamp, and result, making activity traceable to the specific credential used.

PATToken securityRBACPostgreSQLAudit