http-auth-hmac #
Checks HMAC-signed URLs so a link works only with a valid signature and, optionally, only until it expires. Use it to hand out private download or media links without a login: the application signs the URL and nginx verifies it. The result is in $secure_link_hmac.
Source: upstream source
Directives
secure_link_hmac #
syntax: secure_link_hmac 1 arg (on/off flag); · context: http, server, location
Enables or disables HMAC verification. When disabled, $secure_link_hmac is empty.
secure_link_hmac_algorithm #
syntax: secure_link_hmac_algorithm 1 arg (string); · context: http, server, location
Specifies an OpenSSL digest algorithm, such as md5, sha256, sha512, or sha3-256. Available algorithms depend on the OpenSSL build. Only fixed-length digests are supported; extendable-output functions such as SHAKE are not supported.
secure_link_hmac_check_time #
syntax: secure_link_hmac_check_time 1+ args; · context: http, server, location
Specifies the token timestamp and its valid range. The timestamp, range_start, and range_end may contain variables. The bounds apply to the token's age, current time - timestamp, in seconds. Both bounds are inclusive. An omitted bound is unrestricted; if neither is configured, expiration is not checked.
secure_link_hmac_check_token #
syntax: secure_link_hmac_check_token 1 arg; · context: http, server, location
Specifies the supplied HMAC. The value may contain variables. The default encoding is hex.
secure_link_hmac_message #
syntax: secure_link_hmac_message 1 arg; · context: http, server, location
Specifies the message whose HMAC is verified. The value may contain variables. Include the timestamp and any client-supplied lifetime in the message when using time-limited links.
secure_link_hmac_secret #
syntax: secure_link_hmac_secret 1 arg; · context: http, server, location
Specifies the HMAC key. The value may contain variables. An empty key is rejected.
Example
# Include in a dedicated server {}.
# Requires ngx_var_module for base64url_decode, unescape_uri, len and substr.
# Replace example-key, the Base64URL key, and the document root.
location / {
if ($request_method !~ "^(GET|HEAD)$") {
return 403;
}
if ($args ~ "(^|&)(Expires|KeyName|Signature|URLPrefix)=") {
return 403;
}
if ($http_cookie ~ "(^|;[[:blank:]]*)Cloud-CDN-Cookie=[^;]*;[[:blank:]]*([^;]*;[[:blank:]]*)*Cloud-CDN-Cookie=") {
return 403;
}
if ($http_cookie !~ "(?:^|;[[:blank:]]*)Cloud-CDN-Cookie=(?<gcdn_cookie_policy>URLPrefix=(?<gcdn_cookie_prefix_encoded>[A-Za-z0-9_-]+={0,2}):Expires=(?<gcdn_cookie_expires>[0-9]+):KeyName=example-key):Signature=(?<gcdn_cookie_signature>[A-Za-z0-9_-]{27}=?)[[:blank:]]*(?:;|$)") {
return 403;
}
var $gcdn_cookie_key base64url_decode REPLACE_WITH_YOUR_BASE64URL_KEY;
secure_link_hmac on;
secure_link_hmac_algorithm sha1;
secure_link_hmac_secret $gcdn_cookie_key;
secure_link_hmac_message $gcdn_cookie_policy;
secure_link_hmac_check_token $gcdn_cookie_signature digest=base64url;
secure_link_hmac_check_time $gcdn_cookie_expires range_end=0 format=%s;
if ($secure_link_hmac != 1) {
return 403;
}
var $gcdn_cookie_prefix base64url_decode $gcdn_cookie_prefix_encoded;
if ($gcdn_cookie_prefix !~ "^https?://[^/?#]+/[^?#]*$") {
return 403;
}
var $gcdn_cookie_prefix_length len $gcdn_cookie_prefix;
var $gcdn_cookie_request_prefix substr "$scheme://$host$request_uri" 0 $gcdn_cookie_prefix_length;
if ($gcdn_cookie_request_prefix != $gcdn_cookie_prefix) {
return 403;
}
var $gcdn_cookie_decoded_prefix unescape_uri $gcdn_cookie_prefix;
var $gcdn_cookie_decoded_length len $gcdn_cookie_decoded_prefix;
var $gcdn_cookie_resource_prefix substr "$scheme://$host$uri" 0 $gcdn_cookie_decoded_length;
if ($gcdn_cookie_resource_prefix != $gcdn_cookie_decoded_prefix) {
return 403;
}
root /srv/files;
}