The URL signing feature protects the content that is uploaded to ApsaraVideo Live from unauthorized downloads and hotlinking. You can configure URL signing in the ApsaraVideo Live console. This topic describes the URL signing feature, how the feature works, how to configure this feature, and the usage notes.
To prevent hotlinking, you can configure a referer blacklist or whitelist to identify and filter users and protect origin resources. However, considering that the referer content can be forged, the URL signing feature is a more effective method to protect origin resources.
CDN nodes for ApsaraVideo Live work with the origin server to implement URL signing. URL signing protects resources on the origin server against hotlinking in a more secure and reliable manner.
- The origin server provides a user with a signed URL that contains authentication information. For more information about the composition of the signed URL, see Signed URLs.
- The user sends a request to a CDN node by using the signed URL.
- The CDN node verifies the authentication information in the signed URL to determine whether the request is valid. If the request is valid, the CDN node returns a success response. Otherwise, the CDN node denies the request. This protects the resources on the origin server.
- Prevent malicious theft
ApsaraVideo Live provides ingest and streaming URLs for you to ingest and play streams. These URLs are public. Without security control, everyone can use these URLs to ingest and play streams. This may cause you unexpected charges.
- Set an expiration timestamp
By default, the cross-domain policy file
crossdomain.xmlgrants access across all domains that are configured in the file. For more information about the configuration of the crossdomain.xml file, see the following code. To protect ingest and streaming URLs from unauthorized access, you can sign the URLs and set an expiration timestamp for them.
<!--Content of the crossdomain.xml file--> <cross-domain-policy> <allow-access-from domain="*"/> </cross-domain-policy>
Signed URLs are supported by third-party stream ingest tools and players on PCs and mobile terminals.
A signed URL consists of
an ingest URL or a streaming URL and an access token. Example:
rtmp://DomainName/AppName/StreamName: the ingest URL or streaming URL. For more information, see Ingest and streaming URLs.
auth_key=timestamp-rand-uid-md5hash: the access token, which contains the timestamp, rand, uid, and md5hash fields.
The following table describes the timestamp, rand, uid, and md5hash fields.
Field Description timestamp The timestamp when the signed URL takes effect. The value is a UNIX timestamp representing the number of seconds that have elapsed since January 1, 1970, 00:00:00 UTC.
Note The timestamp field specifies the time when the signed URL takes effect.
- If the URL is generated by the URL Generator in the ApsaraVideo Live console, the value of the timestamp field is the time when the URL is generated.
- If the URL is generated on the Access Control page in the ApsaraVideo Live console, the value of the timestamp field is the time when the URL is generated plus the specified Validity Period. tab of the
- If the URL is constructed by using code, you can set the timestamp field as needed.
For example, you can use the following Python code to set the timestamp field:
timestamp = int(time.time()) + 1 * 3600.
A signed URL expires after the time that is specified by the timestamp field plus the specified validity period. You can set the validity period on the tab of the Access Control page in the ApsaraVideo Live console.
rand A random number, which is typically set to 0.
If you want to generate a different URL each time, we recommend that you use a UUID as the random number. The value cannot contain hyphens (-). Example:
uid This parameter is an additional parameter, which is not used. It is typically set to 0. md5hash The string that is calculated by using the MD5 algorithm. The string is 32 characters in length and consists of digits and lowercase letters.
sstring = "URI-timestamp-rand-uid-PrivateKey" md5hash = md5sum(sstring)
Note The calculation method of
- URI: the resource identifier in original URL, which is
/AppName/StreamName. The StreamName includes the file extension. The following are some examples:
When generating md5hash value for an FLV streaming URL, the URI is /AppName/StreamName.flv.
When generating md5hash value for an HLS streaming URL, the URI is /AppName/StreamName.m3u8.
When generating md5hash value for an FLV streaming URL that is transcoded, the URI is /AppName/StreamName_ID.flv.
When generating md5hash value for an HLS streaming URL that is transcoded, the URI is /StreamName_ID.m3u8.
- PrivateKey: the primary key or secondary key that is configured in the ApsaraVideo Live console.
- md5sum: the function that is used to calculate the MD5 hash value. Use the MD5 hash calculation function that is provided by your development language.
md5hashis the same no matter the signed URL is generated in the ApsaraVideo Live console or is constructed using code.
How it works
After URL signing is enabled, the server verifies a request by performing the following operations:
- The server checks whether the expiration time in the request is earlier than the current
time. The expiration time is the time that is specified by the timestamp field plus
the specified validity period.
- If the expiration time is earlier than the current time, the server considers that the signed URL expires and returns HTTP error code 403.
- If the expiration time is later than the current time, the server constructs a string by using the same method. The following code shows how the string is constructed.
- The server uses the MD5 algorithm to calculate the hash value of the string and then
compares the hash value with that contained in the request.
- If the two values are the same, the authentication is successful. The server returns the live stream.
- If the two values are different, the authentication fails. The server returns HTTP error code 403.
sstring = "URI-timestamp-rand-uid-PrivateKey" HashValue = md5sum(sstring)
- Request resources by using req_auth.
- Set the cryptographic key to aliyunliveexp1234, which is the primary key or secondary key configured in the ApsaraVideo Live console.
- The time when the signed URL is generated is 16:49:57 on May 28, 2021 (UTC+8).
- In the ApsaraVideo Live console, the validity period of the signed URL is set to 20 minutes. The Validity Period that is set when the signed URL is generated is 40 minutes.
- Set both the rand and uid fields to 0.
- Request resources by using req_auth.
- The calculated UNIX timestamp of the signed URL is 1622194197, which is 17:29:57 on May 28, 2021 (UTC+8).
- The server generates a signature string that is used to calculate the hash value.
- The server calculates the hash value based on the signature string.
HashValue = md5sum("/video/standard-1622194197-0-0-aliyunliveexp1234") = 5552ff52b5e4e20387c6dc18afce206b
- The URL in the request is
rtmp://example.aliyundoc.com/video/standard****?auth_key=1622194197-0-0-5552ff52b5e4e20387c6dc18afce206bNote In the preceding URL, the auth_key field indicates the access token that is contained in the signed URL.
- The calculated hash value is
md5hash = 5552ff52b5e4e20387c6dc18afce206b, which is the same as that contained in the request. In this case, the authentication succeeds.
- In the ApsaraVideo Live console, you can use the default URL signing settings or customize
the URL signing settings.
- In the default URL signing settings, ingest URLs and streaming URLs are generated based on the primary key and validity period that are configured in the Signed URL Settings section on the tab of the Access Control page in the ApsaraVideo Live console. For more information, see Use the URL generator.
- If you do not use the default settings, you can customize the primary cryptographic key, secondary cryptographic key, and validity period. Then, you can enter the original URL to generate a signed URL for stream ingest. For more information, see Configure URL signing.
- You can also use code to construct a signed URL.
You can construct an unsigned streaming URL based on the AppName and StreamName parameters in an ingest URL. For example, you can construct the following URL:
rtmp:/DomainName/AppName/StreamName. Then, generate the signed streaming URL by using an authentication algorithm in your code. For more information about the authentication algorithm, see Signed URLs.
For more information about the sample code that is used to generate a signed URL, see URL signing demos.
- After URL signing is enabled, only the streaming URL that carries the valid access token can be used for playback.
- The primary and secondary keys are equally effective. The secondary key is used to
ensure a smooth switchover.
If the primary key is changed, all the generated streaming URLs that have the original primary key immediately become invalid. When you switch the primary key to the secondary key, the generated streaming URLs that use the original primary key remain valid for a period of time. This ensures a smooth switchover.
- After you specify the default validity period for the required domain name, all URLs
that use the domain name are valid only in the specified period. You can also customize
the validity period for a single URL.
Note If the default validity period is specified, ApsaraVideo Live appends the default validity period to the timestamp to determine the expiration time of a URL.
- By default, URL signing is enabled. We recommend that you keep this feature enabled to prevent illegal recording and distribution. If you want to disable URL signing, make sure that you understand the risk of unauthorized use of your service and agree to Terms for Disabling URL Authentication on the URL Authentication page in the ApsaraVideo Live console.
- You must manually set the auth_key parameter. ApsaraVideo Live provides no API operation for calculating the value of the auth_key parameter.
- After you enable URL signing, you must add the auth_key parameter to the ingest and streaming URLs. Otherwise, live streams cannot be played. You cannot sign only the ingest URL or the streaming URL. You must sign them both.
- Signed URLs remain valid before their expiration timestamp. You can access a signed URL anytime before it expires. ApsaraVideo Live does not support one-time signed URLs.
- The value of the auth_key parameter is the MD5 value of the URI without the queryString parameters. For more information, see the preceding section about setting URL signing parameters. The URIs of both the ingest and streaming URLs are AppName/StreamName. As a result, the values of the auth_key parameters for the ingest and streaming URLs are the same. If the ingest URL is not confidential, we recommend that you set an expiration timestamp as near as possible. This prevents malicious access to the streaming URL.
- Requests are authenticated only when stream ingest or live streaming begins. Ongoing stream ingest or live streaming is not interrupted if the signed URL expires during the process.