edit-icon download-icon

Authentication code example

Last Updated: Jul 11, 2018

For details on URL Authentication rules, see Live authetication. Through this demo you can authenticate your URLs according to your needs. The following Python demo contains A authentication.

Python

  1. import re
  2. import time
  3. import hashlib
  4. import datetime
  5. def md5sum(src):
  6. m = hashlib.md5()
  7. m.update(src)
  8. return m.hexdigest()
  9. def a_auth(uri, key, exp):
  10. p = re.compile("^(rtmp://)?([^/?]+)(/[^?]*)?(\\?.*)?$")
  11. if not p:
  12. return None
  13. m = p.match(uri)
  14. scheme, host, path, args = m.groups()
  15. if not scheme: scheme = "rtmp://"
  16. if not path: path = "/"
  17. if not args: args = ""
  18. rand = "0" # "0" by default, other value is ok
  19. uid = "0" # "0" by default, other value is ok
  20. sstring = "%s-%s-%s-%s-%s" %(path, exp, rand, uid, key)
  21. hashvalue = md5sum(sstring)
  22. auth_key = "%s-%s-%s-%s" %(exp, rand, uid, hashvalue)
  23. if args:
  24. return "%s%s%s%s&auth_key=%s" %(scheme, host, path, args, auth_key)
  25. else:
  26. return "%s%s%s%s?auth_key=%s" %(scheme, host, path, args, auth_key)
  27. def main():
  28. uri = "rtmp://video-center.alivecdn.com/test/test?vhost=xc.cdnpe.com" # original uri
  29. key = "<input private key>" # private key of authorization
  30. exp = int(time.time()) + 1 * 3600 # expiration time: 1 hour after current itme
  31. authuri = a_auth(uri, key, exp) # auth type:
  32. print("URL : %s\nAUTH: %s" %(uri, authuri))
  33. if __name__ == "__main__":
  34. main()
Thank you! We've received your feedback.