Step 4: Enable Access Authentication
If you want to secure your Streaming PlayURL: rtmp://www.mysite.com/showtime/someone, you may the use Access Authentication function, which will give your Streaming PlayURL an expiry time, anyone who tries to play the Live Streaming PlayURL after expiry time will fail.
Under the Streaming Domains page, click the Streaming Domain you want to use the Access Authentication function.
Upon clicking the Enable Access Authentication button, you will see the Access Authentication Key. Please keep the Key secure as you can generate a Streaming PlayURL with authentication using this key.
You can use Generate Play URL to generate a Streaming PlayURL for demo or use the signature algorithm to generate it by yourself.
Live Stream URL: rtmp://www.mysite.com/showtime/someone Expire Time: 120 (Minutes)
- Get the Streaming PlayURL with authentication in the pop-up notice:
rtmp://www.mysite.com/showtime/someone?ts=1549996982&sign=9c3a348a847dcfd0484839ca536fe9ad
Signed URL Generation Steps
Extract pure URL from play URL, e.g. rtmp://www.mysite.com/showtime/someone
www.mysite.com/showtime/someone
Populate the expiry UTC timestamp and key parameter
expire_in_seconds = 120000 timestamp=utc_timestamp_now + expire_in_seconds url = extrac_url + '?ts=' + timestamp + '&key=' + auth_key e.g. www.mysite.com/showtime/someone?ts=1549996982&key=ABCD
Use M5 and hex to to sign the URL in order to generate signed string
signed_str = md5_hex(url) e.g. 9c3a348a847dcfd0484839ca536fe9ad
Populate signed str to URL as output URL
url = extrac_url + '?ts=' + timestamp + '&sign=' + signed_str e.g. rtmp://www.mysite.com/showtime/someone?ts=1549996982&sign=9c3a348a847dcfd0484839ca536fe9ad
Python Example Code
```
import calendar
import datetime
import hashlib
def md5_hex(url):
m2 = hashlib.md5()
m2.update(url)
return m2.hexdigest()
def generate_signed_url(origin_url, expired_in_seconds, auth_key):
timestamp_now = calendar.timegm(datetime.datetime.utcnow().utctimetuple())
timestamp = timestamp_now + expired_in_seconds
temp_signed_url = '%s?ts=%s&key=%s' % (origin_url, timestamp, auth_key)
temp_signed_url = temp_signed_url[(temp_signed_url.find('://') + 3):]
print(temp_signed_url)
signed_str = md5_hex(temp_signed_url)
return '%s?ts=%s&sign=%s' % (origin_url, timestamp, signed_str)
o_url = 'rtmp://www.mysite.com/showtime/someone'
expired_in = 120000 \# seconds
authentication_key = 'ABCD'
output_signed_url = generate_signed_url(o_url, expired_in, authentication_key)
print(output_signed_url)
```