This repository has been archived by the owner on Oct 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
rlkeygen.py
62 lines (54 loc) · 1.86 KB
/
rlkeygen.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
51
52
53
54
55
56
57
58
59
60
61
62
"""
generate and verify keys for rl-proxy
>>> key_generate("weee", 1, 1, 500, date(1980, 7, 12), 3)
'MMGLFFCSIPWEYDXYAEAAAAAAAAAQB4DZZUJQAAAAAAB7IAIA'
>>> key_verify("weee", 'MMGLFFCSIPWEYDXYAEAAAAAAAAAQB4DZZUJQAAAAAAB7IAIA')
(1L, 1, 500L, datetime.date(1980, 7, 12), 3)
"""
from ctypes import *
from datetime import datetime
import time
__all__ = ['key_generate', 'key_verify']
try:
lib = cdll.LoadLibrary("./libkeygen.so")
except OSError:
lib = cdll.LoadLibrary("libkeygen.so")
KEY_LENGTH = lib.key_length()
def key_generate(secret, org_id, app_id, credits=0, expire_date=None, flags=0):
"""generate an api key with the supplied meta data"""
secret_p = c_char_p(secret)
key = create_string_buffer(KEY_LENGTH)
expire_time = 0L
if expire_date:
expire_time = long(time.mktime(expire_date.timetuple()))
status = lib.key_generate(secret_p, long(org_id), app_id, credits, expire_time, flags,
key)
if status != 0:
raise Exception("bad")
return key.raw
def key_verify(secret, key):
"""verify that an api is valid, returns the embedded meta data if valid, otherwise None"""
secret_p = c_char_p(secret)
key_p = c_char_p(key)
org_id = c_ulonglong()
app_id = c_ushort()
credits = c_ulong()
expire_time = c_ulonglong()
flags = c_ubyte()
status = lib.key_verify(secret_p, key_p, byref(org_id), byref(app_id),
byref(credits), byref(expire_time), byref(flags))
if status:
expires = None
if expire_time.value:
expires = datetime.fromtimestamp(expire_time.value)
return dict(
org_id=org_id.value,
app_id=app_id.value,
credits=credits.value,
expires=expires,
flags=flags.value
)
return None
if __name__ == '__main__':
import doctest
doctest.testmod()