Stream url tokens
To secure the link to your media we have developed a token system. You can calculate a hash using a secret token you can find in de API menu in the Jet-Stream platform interface.
By keeping your secure token private you will be the only one that can hand out player or media URL's to your viewers, preventing deeplinking and other unauthorized use.
Code examples
- JavaScript
- Python
- PHP
import MD5 from "crypto-js/md5";
function calculateToken(filename, secret_token) {
let timestamp = Math.floor(Date.now() / 1000);
let raw_hash = MD5(timestamp.toString() + filename + secret_token);
return timestamp.toString(16) + "|" + raw_hash.toString();
}
def calculate_token(filename, secret_token):
time = int(datetime.datetime.now().strftime('%s'))
timehex = hex(time).replace('0x','')
token = hashlib.md5(''.join([str(time), str(filename), secret_token]))
return timehex + '|' + token.hexdigest()
function calculate_token($filename, $secret_token) {
$timestamp = time();
$timestamp_hex = sprintf("%08x", $timestamp);
return $timestamp_hex . "|" . md5($timestamp . $filename . $secret_token);
}
You can copy the examples right into your own codebase. Please make sure you don't expose the secure token in public. Like when checking your sourcecode in to Git.
Interactive code example
This code example runs local in the browser. It is safe to use this to double check your implementation using your own secret token.
The provided example is in React because we use Docusaurus as our documentation tool.
Check your implementation
You can actually use the above code sample to check the tokens you have generated yourself.
The input fields in the example are actually working, so if you enter your own parameters it will update the result section.
Token validity
The token you generate will be valid for 5 minutes. If you need a longer validity, lets say, 10 minutes, you can generate the timestamp with an additional 600 seconds. Our platform will validate the token since it is not more than 5 minutes (300 seconds) in the past.
- JavaScript
- Python
- PHP
import MD5 from "crypto-js/md5";
function calculateToken(filename, secret_token) {
let timestamp = Math.floor(Date.now() / 1000);
// Adding the 10 minutes
timestamp = timestamp + (10 * 60);
let raw_hash = MD5(timestamp.toString() + filename + secret_token);
return timestamp.toString(16) + "|" + raw_hash.toString();
}
def calculate_token(filename, secret_token):
time = int(datetime.datetime.now().strftime('%s'))
# Adding the 10 minutes
time = time + (10 * 60)
timehex = hex(time).replace('0x','')
token = hashlib.md5(''.join([str(time), str(filename), secret_token]))
return timehex + '|' + token.hexdigest()
function calculate_token($filename, $secret_token) {
$timestamp = time();
// Adding the 10 minutes
$timestamp = $timestamp + (10 * 60);
$timestamp_hex = sprintf("%08x", $timestamp);
return $timestampHex . "|" . md5($timestamp . $filename . $secret_token);
}