CineDev commited on
Commit
38601df
·
1 Parent(s): 431721c

Delete backend/debug_token.py

Browse files
Files changed (1) hide show
  1. debug_token.py +0 -152
debug_token.py DELETED
@@ -1,152 +0,0 @@
1
- # backend/debug_token.py
2
- """
3
- Run this script to debug JWT token issues:
4
- python debug_token.py
5
-
6
- This will help you understand what's in your token and verify your JWT secret.
7
- """
8
-
9
- import jwt
10
- import json
11
- import sys
12
- import os
13
-
14
- # Add the parent directory to the path so we can import settings
15
- sys.path.append(os.path.dirname(os.path.abspath(__file__)))
16
-
17
- # Try to load Django settings
18
- try:
19
- os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'aureon_backend.settings')
20
- import django
21
- django.setup()
22
- from django.conf import settings
23
- SUPABASE_JWT_SECRET = settings.SUPABASE_JWT_SECRET
24
- print(f"✅ Loaded JWT_SECRET from Django settings")
25
- print(f" JWT_SECRET (first 20 chars): {SUPABASE_JWT_SECRET[:20]}...")
26
- except Exception as e:
27
- print(f"⚠️ Could not load Django settings: {e}")
28
- print(f" Please enter your JWT_SECRET manually below")
29
- SUPABASE_JWT_SECRET = None
30
-
31
- print("\n" + "="*60)
32
- print("SUPABASE JWT TOKEN DEBUGGER")
33
- print("="*60)
34
-
35
- # Get token from user
36
- print("\nPaste your Supabase token here:")
37
- print("(You can get it from localStorage in your browser console)")
38
- print("localStorage.getItem('supabase_token')")
39
- print()
40
- token = input("Token: ").strip()
41
-
42
- if not token:
43
- print("❌ No token provided!")
44
- sys.exit(1)
45
-
46
- print("\n" + "-"*60)
47
- print("STEP 1: Decode Header (unverified)")
48
- print("-"*60)
49
-
50
- try:
51
- header = jwt.get_unverified_header(token)
52
- print("✅ Token Header:")
53
- print(json.dumps(header, indent=2))
54
- algorithm = header.get('alg', 'UNKNOWN')
55
- print(f"\n🔍 Algorithm: {algorithm}")
56
- except Exception as e:
57
- print(f"❌ Could not decode header: {e}")
58
- sys.exit(1)
59
-
60
- print("\n" + "-"*60)
61
- print("STEP 2: Decode Payload (unverified)")
62
- print("-"*60)
63
-
64
- try:
65
- # Decode without verification to see the contents
66
- payload = jwt.decode(token, options={"verify_signature": False})
67
- print("✅ Token Payload:")
68
- print(json.dumps(payload, indent=2))
69
-
70
- print(f"\n🔍 Key Information:")
71
- print(f" User ID (sub): {payload.get('sub', 'NOT FOUND')}")
72
- print(f" Email: {payload.get('email', 'NOT FOUND')}")
73
- print(f" Audience: {payload.get('aud', 'NOT FOUND')}")
74
- print(f" Issuer: {payload.get('iss', 'NOT FOUND')}")
75
-
76
- # Check expiration
77
- import time
78
- exp = payload.get('exp')
79
- if exp:
80
- exp_date = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(exp))
81
- is_expired = exp < time.time()
82
- print(f" Expires: {exp_date} {'(EXPIRED!)' if is_expired else '(valid)'}")
83
-
84
- except Exception as e:
85
- print(f"❌ Could not decode payload: {e}")
86
- sys.exit(1)
87
-
88
- print("\n" + "-"*60)
89
- print("STEP 3: Verify Signature")
90
- print("-"*60)
91
-
92
- if not SUPABASE_JWT_SECRET:
93
- print("Enter your SUPABASE_JWT_SECRET:")
94
- SUPABASE_JWT_SECRET = input("JWT_SECRET: ").strip()
95
-
96
- if not SUPABASE_JWT_SECRET:
97
- print("⚠️ No JWT_SECRET provided - skipping verification")
98
- else:
99
- try:
100
- # Try to decode with verification
101
- verified_payload = jwt.decode(
102
- token,
103
- SUPABASE_JWT_SECRET,
104
- algorithms=[algorithm],
105
- audience="authenticated"
106
- )
107
- print("✅ Signature verification PASSED!")
108
- print(" Your JWT_SECRET is CORRECT!")
109
-
110
- except jwt.InvalidSignatureError:
111
- print("❌ Signature verification FAILED!")
112
- print(" Your JWT_SECRET is WRONG!")
113
- print("\n How to find the correct JWT_SECRET:")
114
- print(" 1. Go to your Supabase Dashboard")
115
- print(" 2. Settings → API")
116
- print(" 3. Look for 'JWT Secret' (NOT the service key)")
117
- print(" 4. Copy that value to your .env file as SUPABASE_JWT_SECRET")
118
-
119
- except jwt.ExpiredSignatureError:
120
- print("❌ Token has EXPIRED!")
121
- print(" Solution: Logout and login again to get a new token")
122
-
123
- except jwt.InvalidAudienceError:
124
- print("❌ Invalid audience!")
125
- print(" The token audience doesn't match 'authenticated'")
126
-
127
- except jwt.InvalidAlgorithmError as e:
128
- print(f"❌ Algorithm error: {e}")
129
- print(f" The token uses {algorithm} but it's not allowed")
130
-
131
- except Exception as e:
132
- print(f"❌ Verification error: {type(e).__name__}: {e}")
133
-
134
- print("\n" + "="*60)
135
- print("SUMMARY")
136
- print("="*60)
137
-
138
- print(f"\n✓ Token structure: Valid")
139
- print(f"✓ Algorithm: {algorithm}")
140
- print(f"✓ User ID present: {'Yes' if payload.get('sub') else 'No'}")
141
- print(f"✓ Email present: {'Yes' if payload.get('email') else 'No'}")
142
-
143
- if SUPABASE_JWT_SECRET:
144
- print(f"\nNext steps:")
145
- print(f"1. Make sure backend/aureon_backend/settings.py uses:")
146
- print(f" algorithms=['{algorithm}']")
147
- print(f"2. Make sure your .env has the correct JWT_SECRET")
148
- print(f"3. Restart Django server")
149
- else:
150
- print(f"\n⚠️ Could not verify signature - JWT_SECRET not provided")
151
-
152
- print("\n")