Remote authentication
Remote authentication has the CDN send information about a visitor's request to your own authentication service before returning content, and decides whether to allow or deny the request based on the status code the service returns. Use it when access depends on your own business logic, such as sign-in status, membership level, purchase history, or your own token checks.
It doesn't depend on advanced authentication, and the two can be used together.
Entry point: CDN › CDN domains (open) › click the domain › Access control tab › Rule settings › Remote authentication rules. Authentication services are managed in the top-right corner of this card, and like rules, they take effect after you click Save and deploy.
How it works
- A visitor's request matches a remote authentication rule set to Send to “a service”;
- The CDN sends one request to the authentication service with the URL, request method, query parameters, and headers you set;
- The request is handled based on the status code the service returns:
| Service returns | Result |
|---|---|
| 2xx | Allowed; the request goes on to the cache or the origin |
| 401, 403, and any other 3xx or 4xx | Denied; the visitor gets a 403 |
| 5xx, a timeout, or a connection failure (after about 5 seconds) | Handled by the service's When the service fails setting: allow all or deny all |
- Each request is checked once at the edge node. Requests served from the CDN cache are checked too; the cache doesn't bypass authentication.
- Requests with image processing parameters are checked too.
- Remote authentication runs after hotlink protection rules and advanced authentication. Its result only decides allow or deny and can't be used as a condition in other rules. For the order of evaluation, see Edge rules.
Add an authentication service
Each CDN domain can have up to 5 authentication services.
Open the management window
In the top-right corner of the Remote authentication rules card, click Manage services, then click Add service.
Fill in the settings
| Setting | Description |
|---|---|
| Name | For identification, for example "Membership check" |
| Service URL | A public domain name or IP address starting with http:// or https://. It can include a port and path, but no query string, and can't be this CDN domain itself. The certificate is verified for https |
| Request method | GET, HEAD, or POST, sent without a request body |
| Query parameters | Appended to the service URL; values are encoded automatically. Up to 10 |
| Request headers | For example, a fixed key header. Up to 10 |
| When the service fails | Allow all: requests are served while the service is unavailable. Deny all: safer, but visitors are affected |
| Result caching | Set cache times separately for Allow results and Deny results, from 0 to 1 day. Leave empty to disable caching |
Values of query parameters and request headers can be fixed, or use the same expressions as rules, for example:
| Expression | Value |
|---|---|
http.referer | The visitor's Referer |
ip.src | The visitor's IP address |
raw.http.request.uri | The visitor's original path and query string |
http.request.uri.path | The decoded path |
Test the connection
Click Test connection. The console takes values from a sample visitor request, sends one real request to the authentication service, and shows Allowed, Denied, or Error.
You can save the service only after a successful test (the service returns allow or deny). After changing an existing service's settings, test it again.
The test request is sent from the console server. If the authentication service restricts access by source IP, the test result may differ from the actual result on CDN nodes. Make sure CDN nodes are allowed to reach it.
What the authentication service receives
- The query parameters and request headers you set;
- The visitor's own request headers, such as
CookieandUser-Agent, are forwarded as well and can be used to check sign-in status.
With result caching on, results are cached only by the query parameters and request headers you set: requests with the same values don't call the service again until the cache expires. The visitor's other headers aren't part of the cache key, so with caching on, the service shouldn't rely on those headers for its decision. To vary by a cookie, token, and so on, extract it with an expression and send it explicitly as a query parameter or request header.
Remote authentication rules
Once the authentication service is added, use Remote authentication rules to decide which requests go to which service:
| Handling | Description |
|---|---|
| Send to “a service” | That authentication service decides whether matching requests are allowed or denied |
| No remote authentication | Matching requests aren't checked; use this to make exceptions to earlier rules |
- The condition defaults to all requests. By default no request goes through remote authentication; rules are evaluated in order, and a later matching rule overrides earlier ones.
- When the path or parameters are ambiguous, rules that send requests to a service are treated as matching, and No remote authentication doesn't apply. This keeps anyone from bypassing authentication with unusual encodings.
- With an S4 bucket origin, if the path the visitor requests and the object referenced by an image watermark would go to different authentication services, the request is denied (403).
Example
Send files under /private/ to "Membership check", except /private/preview/, which isn't checked:
| Order | Expression | Handling |
|---|---|---|
| 1 | starts_with(http.request.uri.path, "/private/") | Send to “Membership check” |
| 2 | starts_with(http.request.uri.path, "/private/preview/") | No remote authentication |
You can also choose Remote authentication for a directory in Add from template.
Write an authentication service
The authentication service only needs to check the request and return a status code; no response body is needed:
import http from "node:http";
http.createServer((req, res) => {
const token = req.headers["x-token"];
// Check it with your own business logic, such as looking up a session or verifying a signature
res.statusCode = token && isValid(token) ? 204 : 403;
res.end();
}).listen(8080);Recommendations:
- Respond within a few hundred milliseconds if possible. After about 5 seconds, the When the service fails setting applies;
- Use a fixed key header to check that requests really come from the CDN, so the service can't be called directly;
- Return 403 to deny, and 200 or 204 to allow;
- Turn on result caching for high traffic to reduce requests to the service.
Limits
| Item | Limit |
|---|---|
| Authentication services | 5 per CDN domain |
| Query parameters, request headers | 10 each per service |
| Result cache time | 0 to 1 day |
| Authentication timeout | About 5 seconds |
Authentication service settings count toward the configuration size.
FAQ
Deleting a service says enabled rules still use it
Enabled remote authentication rules still send requests to this service. Disable or change those rules first, then delete the service.
Test connection says it couldn't connect
Check the URL, port, and protocol; whether the service accepts requests from the internet; and, with https, whether the certificate is valid. If the service restricts access by source IP, allow both the console server and CDN nodes.
After turning it on, every visitor gets a 403
Check the failed requests in Live logs and confirm the status code the service returned for them. Only 2xx counts as allowed; 3xx is treated as denied too. If the service fails and is set to Deny all, every request is denied.
I changed the service's logic, but results didn't change right away
With result caching on, after you change the service's settings or logic, cached results stay valid until the cache time expires, and redeploying doesn't clear them. To make changes take effect sooner, use a shorter cache time.