supabase: Cannot Mint Custom JWT in Python
Bug report
- [ x] I confirm this is a bug with Supabase, not with my own application.
- [ x] I confirm I have searched the Docs, GitHub Discussions, and Discord.
Describe the bug
Supabase does not accept any modifications to the tokens
To Reproduce
See these snippets, using your own configuration:
import jwt # pyjwt library
from supabase import create client
key = # insert service key from supabase UI
secrete = # insert jwt secret from supabase UI
url = # insert url
This key works just fine with the client.
# works!
supabase.create_client(my_key).table("mytable").select("*").limit(5).execute()
And we can inspect the claims using pyjwt:
claims = jwt.decode(key,secret, algorithms=["HS256"])
{'iss': 'supabase',
'ref': '<redacted>',
'role': 'service_role',
'iat': 1702682963,
'exp': 2018258963}
Pyjwt can take these exact same claims to create an identical key, which validates that it is working just fine.
my_key = jwt.encode(claims, secret, algorithm="HS256")
assert key == my_key
# works just fine
supabase.create_client(my_key).table("mytable").select("*").limit(5).execute()
But even the most minor change to the claims invalidates this process. For example, if we round off the exp a bit (which should not impact auth at all), the key no longer works.
claims = {'iss': 'supabase',
'ref': '<redacted>',
'role': 'service_role',
'iat': 1702682963,
'exp': 2018258900} # note the change to the 'exp'
}
my_key = jwt.encode(claims, secret, algorithm="HS256")
# Does NOT work.
supabase.create_client(my_key).table("mytable").select("*").limit(5).execute()
# APIError: {'message': 'Invalid API key', 'hint': 'Double check your Supabase `anon` or `service_role` API key.'}
Other small changes (I tried several), also break the key. In this case, I just added a ‘test’ claim.
# now a small change
claims = {'iss': 'supabase',
'ref': '<redacted>',
'role': 'service_role',
'iat': 1702682963,
'exp': 2018258963},
'test': 1. # new key
}
my_key = jwt.encode(claims, secret, algorithm="HS256")
# Does NOT work.
supabase.create_client(my_key).table("mytable").select("*").limit(5).execute()
# APIError: {'message': 'Invalid API key', 'hint': 'Double check your Supabase `anon` or `service_role` API key.'}
This problem arises with really any change made to the key’s claims. So it is not possible to create custom JWTs.
Expected behavior
Expected behavior, consistent with documentation, custom claims can be added to keys and still work on the API.
System information
- OS: macOS
- Version of supabase-js: [e.g. 2.4.5]
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 19 (10 by maintainers)
If this is the case, then I’d swap “perfectly fine” with “unneeded” and use the anon key. I’d say best practice is to only use the service role key when needed.
Yeah, this is expected; as the
roleis important on the backend. As you’ve discovered, you can create your own service role JWTs.Agreed.
Looks like you’ve got a solution! One thing to keep your eye on is this PR. I forgot about it, until someone randomly approved it this morning. I’m not sure if/when it’ll drop.
Hi @swamidass - Just checking in here, did you get this all working?
Awesome!
I don’t believe you’d want to use the service role key in these situations, as it bypasses RLS - only use the anon key.
Okay, it seems that I now have some working code. In python (or nodejs), first make the key.
Then make a supabase client that has this key in the auth header, but uses either the anon or service_role key,
Now postgres see the claims just fine. These claims can be really anything you like. Moreover,
This works painlessly. I just wish it was documented somewhere!!!
I’m not familiar with the Python implementation of Supabase. It looks like
create_clienthas aheadersoption though, so you’d set theAuthorizationheader toBearer <jwt-value>. This is enough to make Supabase calls as the user. However, things likeget_session(),get_user(), andrefresh_session()will not work because the Supabase client is not aware of any session - that is, if the Python implementation works similarly to the Javascript one.This initializing page has a “With timeout option” tab, in case you’re not familiar with how to attach the
headersoption to the client.