With Cloudflare Access, how does a nightly reporting job with no browser authenticate to one HR endpoint using a service token?
answer
- Client ID plus Client Secret
- two request headers
- Service Auth, not Allow
- strict mode: no redirect, no cookie
- rotate with a grace period
basics
~20 sCreate a service token, attach a Service Auth policy with the Service Token selector to an Access application for that endpoint, and have the job send CF-Access-Client-Id and CF-Access-Client-Secret headers; Access then forwards a JWT whose common_name is the Client ID.
solid answer
~50 sA service token is a Client ID and Client Secret that Access generates; the secret is shown once. The job sends them as `CF-Access-Client-Id` and `CF-Access-Client-Secret` request headers. The policy that accepts them must use the Service Auth action with the Service Token selector set to this token; under an Allow policy Access would try to send the job to an identity provider login. Scope it narrowly, with an Access application for the HR export path alone, so the token opens nothing else. The origin still verifies `Cf-Access-Jwt-Assertion`: for a service token the JWT has an empty `sub` and the Client ID in `common_name`. With strict service token authentication, on by default for organisations created from 5 October 2026, a failure returns 401 or 403 instead of a 302 to the login page, and Access issues no `CF_Authorization` cookie.
code
bash · 4 linescurl --fail \
-H "CF-Access-Client-Id: ${HR_REPORT_CLIENT_ID}" \
-H "CF-Access-Client-Secret: ${HR_REPORT_CLIENT_SECRET}" \
https://hr.example.com/api/reports/exportgo deeper
Recall that a service token is a Client ID and Client Secret sent as CF-Access-Client-Id and CF-Access-Client-Secret headers, and that the secret is shown only once.
Explain why the policy must use Service Auth, how to scope the token to one path with its own application, and what the forwarded JWT contains for a service token.
Diagnose a script stuck on a 302, explain what strict service token authentication changes, and rotate a secret with a grace period without breaking the job.
Set the rules: one token per job, Service Token rather than Any Access Service Token, strict mode on, expiry alerts and inactivity cleanup, and deletion as the revocation path.
## What a service token is A **service token** is the credential Access gives to automated systems that cannot complete a browser login. Access generates two values: - a **Client ID**, which ends in `.access` and identifies the token in policies, logs and the forwarded JWT; - a **Client Secret**, displayed only once at creation. Lose it and you must generate a new token. Since 26 August 2026 new secrets use the format `cfast_` followed by 40 alphanumeric characters and an 8-character checksum, so credential scanners can recognise a leaked one; older 64-character hexadecimal secrets keep working. Each token has a **Service Token Duration** chosen at creation. A notification called **Expiring Access Service Token** can warn an administrator a week before expiry, and **Refresh** extends the lifetime by one year. ## Wiring the nightly job to one endpoint 1. Create a service token named for the job, such as `hr-nightly-report`, and store the secret in the job's secret store. 2. Create an Access application for the single path the job needs, for example `hr.example.com/api/reports/export`. A more specific path takes precedence over the HR portal's own application and inherits none of its policies, so this application decides alone. 3. Give that application a policy with the **Service Auth** action, an Include rule on the **Service Token** selector and the value `hr-nightly-report`. **Any Access Service Token** would accept every service token in the account, which is far wider than one job needs. 4. Have the job send both headers on every request. 5. At the origin, verify `Cf-Access-Jwt-Assertion` as for any user, then check that `common_name` equals the job's Client ID before serving the export. Without the Service Auth action, Access treats the request like a user's and starts an identity provider login, which a script cannot finish. ## Without and with strict service token authentication **Strict service token authentication** is an organisation-wide setting under Access settings, **Manage service tokens**. | Behaviour | Strict off | Strict on | |---|---|---| | Authentication or authorisation fails | May redirect with 302 to the login page | Always 401 or 403 | | What can authorise the request | Service Auth policies, but also Allow policies and a `CF_Authorization` cookie the client sends | Only Service Auth policies | | Cookie after success | Access returns a `CF_Authorization` cookie the client may reuse | No cookie; every request sends the headers | With strict mode on, failed requests for a recognised token also appear in Access authentication logs: expired or disabled tokens, wrong Client Secrets and tokens not authorised for the application. Malformed headers and unknown Client IDs are not logged. Organisations created on or after 5 October 2026 have it on and cannot turn it off; older organisations can turn it on, which Cloudflare recommends. For an older organisation that keeps it off, the per-application **401 Response for Service Auth policies** setting at least makes a request without the right token fail with 401 instead of a redirect. ## Rotation, expiry and revocation - **Rotate secret** issues a new Client Secret and keeps the Client ID. A grace period of one hour to 30 days keeps both secrets valid while the job's configuration is updated; **Revoke immediately** is the choice after a leak. - **Disable token** stops a token temporarily and also ends any rotation grace period. - **Delete** is the real revocation. **Revoke existing tokens** on an application only ends current sessions: a valid Client ID and Client Secret simply get a new session on the next request. - **Automatically clean up inactive service tokens** disables or deletes tokens unused for an inactivity period of 30 to 365 days, unless a policy rule references the token directly. ## Mistakes worth catching in review - An Allow policy holding the Service Token selector, so the job is redirected to a login page. - A Bypass policy on the export path "because the job has no browser", which removes authentication and logging. - Any Access Service Token on the HR portal, which lets every other team's token in. - The secret committed to the job's repository instead of a secret store. - An origin that, seeing an empty `sub`, skips its checks instead of matching `common_name`.
- The job receives a 302 to a login page instead of the export; what do you check first?Whether the policy on that path uses the Service Auth action and names this token, and whether both headers arrive with the right values. Without strict service token authentication Access answers a failed or unmatched service-token request with a login redirect; turning strict mode on, or the application's 401 Response for Service Auth policies setting, makes the failure explicit.
- How do you rotate the job's secret without a failed night?Choose Rotate secret with a grace period, say one day. The Client ID stays the same, so only the secret changes in the job's secret store. Both secrets work until the grace period ends, then Access revokes the old one. Disabling the token during that window would stop both.
- What if a caller can send only one custom header?A self-hosted Access application can be set, through the API's `read_service_tokens_from_header` field, to read the service token from one named header, such as `Authorization`, carrying a JSON object with both the client ID and the client secret.
- An Allow policy with the service token is enough; Service Auth is only for client certificates.
- Revoke existing tokens on the application permanently locks the service token out.
- Rotating the secret also issues a new Client ID, so every caller changes both values.
- The Access JWT for a service token carries the job's identity in sub.
- Any Access Service Token is the safer choice because it needs no per-token edits.
Assumes Cloudflare Access as of 2026-10-10: strict service token authentication (October 2026, mandatory for organisations created from 5 October 2026), rotation grace periods (August 2026) and the cfast_ secret format (26 August 2026) are recent.