-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtokengen.py
50 lines (37 loc) · 1.29 KB
/
tokengen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/env/python
#############################################
# Vivox Token Generation Example - Python
#############################################
import base64
import hashlib
import hmac
import json
def b64url(value):
"""Return a base64url-encoded str without trailing ='s"""
return base64.urlsafe_b64encode(value).rstrip('=')
def vx_generate_token(key, issuer, exp, vxa, vxi, f, t):
# Create dictionary of claims
claims = {
'iss': issuer,
'exp': exp,
'vxa': vxa,
'vxi': vxi,
'f': f,
't': t
}
# Header is static - base64url encoded {}
header = b64url('{}') # Can also be defined as a constant "e30"
# Encode claims payload
json_payload = b64url(json.dumps(claims))
# Join segments to prepare for signing
segments = [header, json_payload]
to_sign = b'.'.join(segments)
# Sign token with key and HMACSHA256
sig = hmac.new(key, to_sign, hashlib.sha256).digest()
segments.append(b64url(sig))
# Join all 3 parts of token with . and return
return b'.'.join(segments)
if __name__ == '__main__':
# Example usage
token = vx_generate_token('secret', 'issuer', 1559359105, 'join', 123456, 'sip:.username.@domain.vivox.com', 'sip:confctl-g-channelname@domain.vivox.com')
print(token)