Skip to content

Commit 247ed07

Browse files
committed
Solving RLS
* We cannot authenticate users with API keys, because we don't have their password * When other users are authenticated (just created an account, checked their api keys etc...) then authorization for those users might still be active (JWT tokens still on the server), and supabase would use those instead of service_role. So: sign out any user before verifying tokens.
1 parent 8edaf21 commit 247ed07

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

app/api/v1/routes/auth.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,12 @@ async def create_api_key(credentials: UserCredentials) -> ApiKeyResponse:
100100
HTTPException: 400 if creation fails, 401 if authentication fails
101101
"""
102102
try:
103-
print("\nCreate API key endpoint hit!") # See if we reach this endpoint
104103
print(f"Creating API key for {credentials.email}")
105-
print("\nCreating API key...")
106-
print(f"Test environment: {settings.ENVIRONMENT == 'TEST'}; SKIP EMAIL VERIFICATION: {settings.SKIP_EMAIL_VERIFICATION}")
107-
log = f"Authenticating user {credentials.email} with password {credentials.password}"
104+
if settings.ENVIRONMENT == 'TEST':
105+
print(f"Test environment: {settings.ENVIRONMENT == 'TEST'}; SKIP EMAIL VERIFICATION: {settings.SKIP_EMAIL_VERIFICATION}")
106+
log = f"Authenticating user {credentials.email} with password ***"
108107
user = await backend.authenticate_user(credentials.email, credentials.password)
109108
log = f"User authenticated: {user}"
110-
print(f"User authenticated: {user}")
111-
print(f"User metadata: {user.user_metadata}")
112109

113110
# Skip verification check in test environment
114111
if not (settings.ENVIRONMENT == "TEST" and settings.SKIP_EMAIL_VERIFICATION):

app/core/security.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,12 @@ async def list_api_keys(user):
121121
return api_keys
122122

123123
async def verify_token(request: Request, credentials: Optional[HTTPAuthorizationCredentials] = Security(security)) -> dict:
124-
"""Verify JWT token and return user info with credits"""
124+
"""Verify JWT token and return user info with credits"""
125+
126+
# Whatever the logic flow, first we need to make sure that there isn't a different user still authorized:
127+
db.auth.sign_out()
128+
129+
# Now we can do the rest of the logic. run test routines first, then check guest or proper user.
125130
try:
126131
# Skip email verification in test environment
127132
if settings.ENVIRONMENT == "TEST" and settings.SKIP_EMAIL_VERIFICATION:
@@ -227,4 +232,4 @@ def generate_guest_id(request: Request) -> dict:
227232
ip = request.client.host
228233
# Hash the IP to get 32 hex chars
229234
hex_hash = hashlib.sha256(ip.encode()).hexdigest()[:32]
230-
return {"id": f"{UUID(hex_hash)}"}
235+
return {"id": f"{UUID(hex_hash)}"}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
BEGIN;
2+
3+
-- Create new comprehensive service role policies
4+
CREATE POLICY "Service role has full access to API keys"
5+
ON public.api_keys
6+
FOR ALL
7+
TO service_role
8+
USING (true)
9+
WITH CHECK (true);
10+
11+
CREATE POLICY "Service role has full access to credits"
12+
ON public.credits
13+
FOR ALL
14+
TO service_role
15+
USING (true)
16+
WITH CHECK (true);
17+
18+
COMMIT;

0 commit comments

Comments
 (0)