Create helper.py
Browse files
helper.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import jwt
|
2 |
+
import uuid
|
3 |
+
from datetime import datetime, timedelta
|
4 |
+
import random
|
5 |
+
import string
|
6 |
+
|
7 |
+
github_username_zed_userid_list = [
|
8 |
+
("aaa", 123123),
|
9 |
+
]
|
10 |
+
|
11 |
+
|
12 |
+
def generate_random_tuple():
|
13 |
+
"""
|
14 |
+
Generate a random tuple containing a string and an integer.
|
15 |
+
|
16 |
+
Returns:
|
17 |
+
tuple: A tuple containing:
|
18 |
+
- str: A random string of length 6-12 consisting of ASCII letters and digits.
|
19 |
+
- int: A random 6-digit integer between 100000 and 999999.
|
20 |
+
"""
|
21 |
+
# Generate a random string of length 6-12
|
22 |
+
random_string = ''.join(random.choices(string.ascii_letters + string.digits, k=random.randint(6, 12)))
|
23 |
+
|
24 |
+
# Generate a random 6-digit integer
|
25 |
+
random_int = random.randint(100000, 999999)
|
26 |
+
|
27 |
+
return (random_string, random_int)
|
28 |
+
|
29 |
+
|
30 |
+
def create_jwt(github_user_login: str, user_id: int) -> str:
|
31 |
+
"""
|
32 |
+
Create a JSON Web Token (JWT) for a given GitHub user.
|
33 |
+
|
34 |
+
Args:
|
35 |
+
github_user_login (str): The GitHub username of the user.
|
36 |
+
user_id (int): The user's ID.
|
37 |
+
|
38 |
+
Returns:
|
39 |
+
str: A JWT encoded string containing user information and authentication details.
|
40 |
+
|
41 |
+
Note:
|
42 |
+
The token has a lifetime of 1 hour and includes the following claims:
|
43 |
+
- iat: Issued at time
|
44 |
+
- exp: Expiration time
|
45 |
+
- jti: Unique token identifier
|
46 |
+
- userId: User's ID
|
47 |
+
- githubUserLogin: GitHub username
|
48 |
+
- isStaff: Boolean indicating staff status (default: False)
|
49 |
+
- hasLlmClosedBetaFeatureFlag: Boolean for LLM closed beta feature (default: False)
|
50 |
+
- plan: User's plan (default: "Free")
|
51 |
+
"""
|
52 |
+
LLM_TOKEN_LIFETIME = timedelta(hours=1)
|
53 |
+
now = datetime.utcnow()
|
54 |
+
|
55 |
+
payload = {
|
56 |
+
"iat": int(now.timestamp()),
|
57 |
+
"exp": int((now + LLM_TOKEN_LIFETIME).timestamp()),
|
58 |
+
"jti": str(uuid.uuid4()),
|
59 |
+
"userId": user_id,
|
60 |
+
"githubUserLogin": github_user_login,
|
61 |
+
"isStaff": False,
|
62 |
+
"hasLlmClosedBetaFeatureFlag": False,
|
63 |
+
"plan": "Free"
|
64 |
+
}
|
65 |
+
|
66 |
+
return jwt.encode(payload, 'llm-secret', algorithm='HS256')
|