-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtoken_generator.py
More file actions
61 lines (49 loc) · 1.88 KB
/
Copy pathtoken_generator.py
File metadata and controls
61 lines (49 loc) · 1.88 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
import os
import json
import urllib.request
import urllib.error
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
SUPABASE_URL = os.getenv("SUPABASE_URL")
SUPABASE_KEY = os.getenv("SUPABASE_KEY")
if not SUPABASE_URL or not SUPABASE_KEY:
raise ValueError("Missing SUPABASE_URL or SUPABASE_KEY in environment variables. Please check your .env file.")
def generate_jwt_for_user(email: str, password: str):
"""
Generate a valid JWT by authenticating with Supabase.
This ensures the token is correctly signed and added to JWKS.
"""
url = f"{SUPABASE_URL}/auth/v1/token?grant_type=password"
headers = {
"apikey": SUPABASE_KEY,
"Content-Type": "application/json"
}
data = json.dumps({
"email": email,
"password": password
}).encode("utf-8")
req = urllib.request.Request(url, data=data, headers=headers, method="POST")
try:
with urllib.request.urlopen(req) as response:
result = json.loads(response.read().decode())
access_token = result.get("access_token")
user_id = result.get("user", {}).get("id")
print(f"Successfully generated JWT for {email}")
print(f"User ID: {user_id}")
print("-" * 40)
print("ACCESS TOKEN:")
print(access_token)
print("-" * 40)
return access_token
except urllib.error.HTTPError as e:
error_msg = e.read().decode()
print(f"Failed to generate JWT. Status: {e.code}")
print(f"Error: {error_msg}")
return None
if __name__ == "__main__":
# Replace with the actual password for the fisherman_example account
EMAIL = "fishermen@gmail.com"
PASSWORD = "123Fish"
print(f"Attempting to generate token for {EMAIL}...")
generate_jwt_for_user(EMAIL, PASSWORD)