Limiting access by a secret key is a security feature that can protect your web resource from bandwidth theft by prohibiting hotlinking and sharing your content to other websites or applications.
To limit the access using a secret key, follow the steps outlined below:
1. Log in to the Universal CDN Control Panel at my.ucdn.com.
2. Navigate to the My Zones tab located in the top left corner.
3. Click on the cogwheel icon next to the CDN zone which access will be restricted.
4. Navigate to the Limit Access tab.
5. Scroll down to the Limit access using secret key feature and check the box next to it.
6. Choose the desired hash algorithm (md5 or sha1), enter the password you want to use, and specify the default time each link will be valid.
The secret key entered in the Control Panel must be used when generating links to your resources.
After making any changes in the Universal CDN Control Panel do not forget to click on the Save Changes button.
Generating URLs to your resources using a secret key
Limiting access using a secret key requires including two mandatory parameters (cdn_hash and cdn_creation_time) and may include several optional parameters (cdn_ttl, cdn_net, cdn_bw, cdn_bw_fs, cdn_cv_ ).
In case of the incomplete, wrong, or missing hash value, the CDN edge servers return 405 “Method Not Allowed” HTTP status code.
The parameter is mandatory. The value of this parameter is the returned hash value (called a checksum).
The checksum is a unique digital fingerprint of the data of the included parameters generated by using the hash function of the chosen md5 or sha1 algorithm.
The following part of the PHP script illustrates the algorithm in which order the hash value should be generated:
$cdn_hash = md5( $request_path .$secret_key .$cdn_creation_time .$cdn_ttl .$cdn_net .$cdn_bw .$cdn_bw_fs .$cdn_cv_user_id );
It needs to be added as described below:
This is a mandatory parameter only if the time for which the hash will be valid is specified from the Universal CDN Control Panel or by including it with the cdn_ttl parameter. It specifies the time when the hash was generated in Unix timestamp format. It needs to be added as described below:
This parameter specifies the time for which the hash will be valid. The value of the parameter must be specified in seconds. It needs to be added as described below:
The TTL value can be specified through the Universal CDN Control Panel as well (as shown on the screenshot above). Adding the cdn_ttl parameter when a hash value for your asset is generated will override the value specified in the Universal Control Panel if such has been set.
This parameter is used to allow access only from an IP address or from an entire IP Network. It needs to be added as described below:
Where “188.8.131.52” is an example IP address.
Where “184.108.40.206.24” is an example IP network (220.127.116.11/24). Тhe Slash notation “/” must be replaced with dot “.” since the forward “/” is a reserved character used to separate different parts of the URL.
This parameter limits the rate of downloading speed. The values must be specified in bytes per second. It needs to be added as described below:
This parameter sets the amount of data that will be served at full speed (support “k”, “m”, and “g” for KB, MB, and GB respectively). This parameter should be used along with the cdn_bw parameter that limits the rate of the downloading speed after serving the data at full speed. It needs to be added as described below:
The cdn_cv_ prefix allows adding a custom parameter. It needs to be added as described below:
Where “cdn_cv_” is an integral part of the parameter – it cannot be changed or omitted, “user_id” is the custom part of the parameter (it can be changed to whatever value you define), “1997” is the value of the parameter.
Example PHP script for generating a secret key
Let’s assume that the CNAME you have added to your CDN zone is with the following name test.example.com, the URI path the example file is /video/example-video.mp4, you have selected md5 for the encryption type, and the password you have entered is “sfKlt1!54hF4_%”.
For the purposes of this tutorial, we have created a simple PHP script that allows us to generate the required hash for your URLs. We have added the following values for the parameters specified in the script below:
<?php $secret_key = '<place your secret key here>'; $request_path = '/video/example-video.mp4'; /* Current Unix timestamp */ $cdn_creation_time = time(); /* For how long the generated URL will be active in seconds */ $cdn_ttl = 86400; /* Allow access only to specified ip address or IP network */ $cdn_net = "18.104.22.168"; /* Limit download speed to bytes per second (10KB) */ $cdn_bw = "10240"; /* The amount of data served at maximum speed MB */ $cdn_bw_fs = "10m"; /* Generating the hash */ $hash = md5($request_path . $secret_key . $cdn_creation_time . $cdn_ttl . $cdn_net . $cdn_bw . $cdn_bw_fs); /* Generating the URL with a secret key by concatenating host, path, and parameters */ $access_url = 'https://test.example.com' .$request_path .'?cdn_hash='.$hash .'&cdn_creation_time='.$cdn_creation_time .'&cdn_ttl='.$cdn_ttl .'&cdn_net='.$cdn_net .'&cdn_bw='.$cdn_bw .'&cdn_bw_fs='.$cdn_bw_fs; echo $access_url . "\n"; ?>
The following URL has been generated as output after running the script above: