With Cloudflare Token Authentication, we can restrict access to documents, files, and media to selected users without them registering.
This will eventually protect paid/restricted content from leeching and unauthorized sharing.
As part of our Server Management Services, we assist our customers with several Cloudflare queries.
Today, let us see how to enable token authentication in Cloudflare.
Cloudflare Token Authentication
Recently, one of our customers came across a “Sorry, you have been blocked” message while accessing sites using Cloudflare.
Generally, this happens when we try to access a restricted resource without a valid token.
Moving ahead, let us see how our Support Techs authenticate a token via Firewall Rules.
-
Configure using Firewall Rules
To do so, we require a Business or Enterprise account.
- Initially, we log in to the Cloudflare dashboard.
- Click the account for the domain we want to enable Token Authentication.
- Then we click on the Firewall app.
- In the Firewall Rules tab, we click Create a Firewall Rule.
- To switch to the Expression Preview editor, we click the Edit expression link.
Now, let us see a rule that blocks any visitor that does not pass the HMAC key validation on a specific hostname and URL path.
For instance, consider the below URL:
test.domain.com/download/cat.jpg?verify=1484063787-9JQB8vP1z0yc5DEBnH6JGWM3mBmvIeMrnnxFi3WtJLE%3D
The example Firewall Rule for this will look like this:
(http.host eq “test.domain.com” and not is_timed_hmac_valid_v0(“mysecrettoken”, http.request.uri,10800, http.request.timestamp.sec,8))
The components of this example Firewall Rule include:
- Token key = mysecrettoken
- Token expiration time = 10800 seconds
- Http.request.uri = /download/cat.jpg
- Http.request.timestamp.sec = 1484063787
- Separator: len(?verify=) = 8
Moving ahead, let us see how to generate tokens for the paths using this Firewall Rule:
Python 3.8
import hmac
import base64
import urllib.parse
import time
from hashlib import sha256
message = "/download/cat.jpg"
secret = "mysecrettoken"
separator = "?verify="
timestamp = str(int(time.time()))
digest = hmac.new((secret).encode('utf8'), "{}{}".format(message,timestamp).encode('utf8'), sha256)
token = urllib.parse.quote_plus(base64.b64encode(digest.digest()))
print("{}{}{}-{}".format(message, separator, timestamp, token))
Python 2.7
import hmac
import base64
import time
import urllib
from hashlib import sha256
message = "/download/cat.jpg"
secret = "mysecrettoken"
separator = "verify"
timestamp = str(int(time.time()))
digest = hmac.new(secret, message + timestamp, sha256)
param = urllib.urlencode({separator: '%s-%s' % (timestamp, base64.b64encode(digest.digest()))})
print("{}{}".format(message, param))
PHP
$message = "/download/cat.jpg";
$secret = "mysecrettoken";
$separator = "?verify=";
$time = time();
$token = $time . "-" . urlencode(base64_encode(hash_hmac("sha256", $message . $time, $secret, true)));
echo($message . $separator . $token);
Implement token creation
To implement the token creation we need to enter the following code at the origin server:
PHP Version
<?php
// Generate valid URL token
$secret = "thisisasharedsecret";
$time = time();
$token = $time . "-" . urlencode(base64_encode(hash_hmac("sha256", "/download/private.jpg$time", $secret, true)));
param = "verify=" . $token;
?>
Python Version
import hmac
import base64
import time
import urllib
from hashlib import sha256
secret = "thisisasharedsecret"
time = str(int(time.time()))
digest = hmac.new(secret, "/download/cat.jpg" + time, sha256)
param = urllib.urlencode({'verify': '%s-%s' % (time, base64.b64encode(digest.digest()))})
This will generate a URL parameter:
verify=1484063137-IaLGSmELTvlhfd0ItdN6PhhHTFhzx73EX8uy%2FcSDiIU%3D
We will then need to append it to any URL under the domain.com/download/* path. For example,
/download/cat.jpg?verify=1484063787-9JQB8vP1z0yc5DEBnH6JGWM3mBmvIeMrnnxFi3WtJLE%3D
In order to check if URLs generate correctly on the server, we can enable the WAF rules. It will simulate and monitor the WAF logs and the Firewallapp in the Cloudflare dashboard.
[Need help with the procedure? We’d be happy to assist]
Conclusion
In short, we saw how our Support Techs authenticate token in Cloudflare.
0 Comments