api.py
1.23 KB
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
# todo: tunggu update di opensipkd.jsonrpc.auth karena belum support python 3
import sys
import hmac
import hashlib
import base64
from opensipkd.jsonrpc.auth import get_seconds
from random import choice
from string import (
ascii_uppercase,
ascii_lowercase,
digits,
)
def get_random_number(width=12):
return ''.join(choice(digits) for _ in range(width))
def get_jsonrpc(method, params):
return dict(jsonrpc='2.0', method=method, params=params, id=get_random_number(6))
# fungsi ini digunakan untuk membuat data yang akan dikirim ke server
def get_header(userid, password, time_stamp=None):
if not time_stamp:
time_stamp = str(get_seconds())
value = '&'.join([str(userid), str(time_stamp)])
password = str(password)
signature = hmac.new(key=str.encode(password), msg=str.encode(value),
digestmod=hashlib.sha256).digest()
if sys.version < '3':
encoded_signature = base64.encodestring(signature).replace('\n', '')
else:
encoded_signature = base64.encodebytes(signature).decode().replace('\n', '')
# signature = hmac.new(key=password, msg=value, digestmod=hashlib.sha256).digest()
return dict(userid=userid, signature=encoded_signature, key=time_stamp)