zed2api / helper.py
smgc's picture
Update helper.py
bb03080 verified
raw
history blame
1.67 kB
import jwt
import uuid
from datetime import datetime, timedelta
import random
import string
import os
def generate_random_tuple():
"""
Generate a random tuple containing a string and an integer.
Returns:
tuple: A tuple containing:
- str: A random string of length 6-12 consisting of ASCII letters and digits.
- int: A random 6-digit integer between 100000 and 999999.
"""
# Generate a random string of length 6-12
random_string = ''.join(random.choices(string.ascii_letters + string.digits, k=random.randint(6, 12)))
# Generate a random 6-digit integer
random_int = random.randint(100000, 999999)
return (random_string, random_int)
def create_jwt(github_user_login: str, user_id: int) -> str:
"""
Create a JSON Web Token (JWT) for a given GitHub user.
Args:
github_user_login (str): The GitHub username of the user.
user_id (int): The user's ID.
Returns:
str: A JWT encoded string containing user information and authentication details.
"""
LLM_TOKEN_LIFETIME = timedelta(hours=1)
now = datetime.utcnow()
payload = {
"iat": int(now.timestamp()),
"exp": int((now + LLM_TOKEN_LIFETIME).timestamp()),
"jti": str(uuid.uuid4()),
"userId": user_id,
"githubUserLogin": github_user_login,
"isStaff": False,
"hasLlmClosedBetaFeatureFlag": False,
"plan": "Free"
}
# 使用环境变量获取密钥,如果没有设置,则使用默认值
secret_key = os.environ.get('JWT_SECRET_KEY', 'llm-secret')
return jwt.encode(payload, secret_key, algorithm='HS256')