|
import jwt |
|
import uuid |
|
from datetime import datetime, timedelta |
|
|
|
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. |
|
|
|
Note: |
|
The token has a lifetime of 1 hour and includes the following claims: |
|
- iat: Issued at time |
|
- exp: Expiration time |
|
- jti: Unique token identifier |
|
- userId: User's ID |
|
- githubUserLogin: GitHub username |
|
- isStaff: Boolean indicating staff status (default: False) |
|
- hasLlmClosedBetaFeatureFlag: Boolean for LLM closed beta feature (default: False) |
|
- plan: User's plan (default: "Free") |
|
""" |
|
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" |
|
} |
|
|
|
return jwt.encode(payload, 'llm-secret', algorithm='HS256') |
|
|