File size: 2,825 Bytes
cb0fce3
 
 
0af4546
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cb0fce3
 
 
 
62a814c
cb0fce3
 
 
62a814c
cb0fce3
 
62a814c
 
 
 
 
 
 
 
 
 
 
cb0fce3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0af4546
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
import jwt
import uuid
from datetime import datetime, timedelta
import random
import string


def get_github_username_zed_userid_list():
    """
    Read GitHub usernames and Zed user IDs from gh_username_zed_userid.txt file.

    Returns:
        list: A list of tuples, each containing a GitHub username (str) and a Zed user ID (int).
    """
    result = []
    try:
        with open('gh_username_zed_userid.txt', 'r') as file:
            for line in file:
                username, user_id = line.strip().split(',')
                result.append((username, user_id))
    except FileNotFoundError:
        print("Warning: gh_username_zed_userid.txt file not found. Using default values.")
        result = [("aaa", "123123")]
    except ValueError:
        print("Warning: Invalid format in gh_username_zed_userid.txt. Using default values.")
        result = [("aaa", "123123")]
    
    return result

# 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.

    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')