-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken-generator.py
More file actions
102 lines (82 loc) · 2.8 KB
/
token-generator.py
File metadata and controls
102 lines (82 loc) · 2.8 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"""
FCS API - Token Generator (Python)
Generate secure tokens for frontend JavaScript authentication
Usage:
1. Set your access_key and public_key
2. Call generate_token()
3. Pass the token data to your frontend JavaScript
@package FcsApi
@author FCS API <support@fcsapi.com>
@link https://fcsapi.com
"""
import hmac
import hashlib
import time
import json
class FcsTokenGenerator:
"""
Token generator for FCS API frontend authentication
"""
def __init__(self, access_key: str = 'YOUR_ACCESS_KEY_HERE',
public_key: str = 'YOUR_PUBLIC_KEY_HERE',
token_expiry: int = 3600):
"""
Initialize token generator
Args:
access_key: Your API access key (get from https://fcsapi.com/dashboard)
public_key: Your public key (get from https://fcsapi.com/dashboard)
token_expiry: Token expiry in seconds (default: 3600 = 1 hour)
Options: 300 (5min), 900 (15min), 1800 (30min),
3600 (1hr), 86400 (24hr)
"""
self.access_key = access_key
self.public_key = public_key
self.token_expiry = token_expiry
def generate_token(self) -> dict:
"""
Generate token for frontend authentication
Returns:
dict: Token data with _token, _expiry, _public_key
"""
expiry = int(time.time()) + self.token_expiry
message = f"{self.public_key}{expiry}"
token = hmac.new(
self.access_key.encode('utf-8'),
message.encode('utf-8'),
hashlib.sha256
).hexdigest()
return {
'_token': token,
'_expiry': expiry,
'_public_key': self.public_key
}
def to_json(self) -> str:
"""
Generate token and return as JSON string
Returns:
str: JSON string of token data
"""
return json.dumps(self.generate_token())
def get_meta_tags(self) -> str:
"""
Generate HTML meta tags for token
Returns:
str: HTML meta tags
"""
token = self.generate_token()
return f'''<meta name="fcs-public-key" content="{token['_public_key']}">
<meta name="fcs-token" content="{token['_token']}">
<meta name="fcs-token-expiry" content="{token['_expiry']}">'''
# ============================================
# USAGE EXAMPLES
# ============================================
if __name__ == '__main__':
# Example 1: Generate token
generator = FcsTokenGenerator('your_access_key', 'your_public_key')
token_data = generator.generate_token()
print("Token Data:", token_data)
# Example 2: Get as JSON
print("\nJSON:", generator.to_json())
# Example 3: Get meta tags
print("\nMeta Tags:")
print(generator.get_meta_tags())