Secure Token Integration For Python (Secondary)
Publisher: Psychz Networks, August 23,2021The following article will help you to integrate secure token (Secondary) generated using a secret key in your Python code. First, you need to login to the dashboard and enable Secure Token option for the desired location (domain). Once you enable Secure Token option, you can create a 'Secret Key' which will be then passed on to our backend. With this, you can generate your own tokens with md5 using the Python script below. The newly generated URL then will hold all the necessary information which will match the Secure key saved at our backend and will allow access to the user.
In secondary secure token, in secure url generated uses file name, expiry time(optional), compared to IP and expiry time and not the file name in Primary method.
Sample security token looks like
http://domain.com/TimeID/HashID/FileName
Once the security token is generated, you can use it in Python code to create the URL token.
#!/usr/bin/env python3
import hashlib
from time import time
def generate_secure_url(security_key, path,
expire_timeframe="",
base_url=str(),
file_name):
expire_timestamp = int(time()) + 3600
token_content = '{key}{path}{timestamp}{file_name}'.format(key=security_key,path=path,timestamp=expire_timeframe,file_name=file_name)
md5sum = hashlib.md5()
md5sum.update(token_content.encode('ascii'))
token_digest = md5sum.digest()
token_formatted = token_digest.replace('\n', '').replace('+', '-').replace('/', '_').replace('=', '')
# Build the URL
if expire_timeframe:
url = '{base_url}{path}/{token}/{file_name}'.format(
base_url=base_url,
path=path,
token=token_formatted,
file_name=file_name)
if not expire_timeframe:
url = '{base_url}{path}/{token}/{file_name}'.format(
base_url=base_url,
path=path,
token=token_formatted,
file_name=file_name)
return url
# Example usage:
# Returns: ' https://test.youdomain.com/index.html/xn3MXhSmjhMvAlE__w5nGQ/abc.jpg'
print(' https://test.youdomain.com'+generate_secure_url('secret-key', '/index.html','','','abc.jpg'))
# Returns: ' https://yourdomain.com/index.html/-yTNoTfS_NiwhPxH2xYcZg/abc.jpg'
print(generate_secure_url('secret-key', '/index.html', 31536000, ' https://yourdomain.com', 'abc.jpg'))