Configuring Token Authentication
Cloudflare Token Authentication allows you to restrict access to documents, files, and media to selected users without requiring them to register. This helps protect paid/restricted content from leeching and unauthorized sharing.
There are two options to configure Token Authentication, via Cloudflare Workers or WAF custom rules.
 Option 1: Configure using Cloudflare Workers
Review the following Cloudflare Workers documentation to configure Token Authentication:
- Configure a Worker
- Use the Auth with Headers template
 Option 2: Configure using WAF custom rules
To configure Token Authentication:
- Log in to the Cloudflare dashboard, and select your account and domain.
- Go to Security > WAF > Custom rules.
- Select Create rule.
- Select Edit expression above Expression Preview to switch to the Expression Preview editor.
The following example illustrates a rule that blocks any visitor that does not pass your HMAC key validation on a specific hostname and URL path. Details required for Token Authentication include:
- The path you wish to authenticate (for example, test.domain.com/download/cat.jpg)
- The parameter name you wish the token to have (for example, verify)
- The desired token expiration times, if any (for example, five and 20 minutes)
For the following example URL:
The expression for the custom rule would be similar to the following:
The components of this example custom rule (using the previous example URL) include:
- Token key = mysecrettoken
- Token expiration time = 10800seconds
- http.request.uri=- /download/cat.jpg
- http.request.timestamp.sec=- 1484063787
- Separator: len("?verify=")=8
To generate tokens for the paths using this custom rule:
 Python 3.8
 Python 2.7
 PHP
 Implement token creation
Implementing the token creation requires the following code entered at your origin server:
 PHP Version
 Python Version
This will generate a URL parameter such as:
Which you will then need to append to any URL under the domain.com/download/* path. For example:
The token parameter must be the last parameter in the query string.
If you are on an Enterprise plan, you can test if URLs are being generated correctly on the server by:
- Setting the WAF custom rule action to Log.
- Monitoring the activity log in Security > Events.