Replies: 9 comments 16 replies
-
You can definitely use the However I wouldn't recommend it. JWT is great for Authentication, but not so great for Authorization. What happens if a user is removed from an organization (in your example above)? Their token is still valid for 1 hour, and the Policies will continue to allow the user until the token is refreshed. It's a tradeoff between simplicity for security. I do think we need to set up a good multi-tenant solution, something which is simple and secure. I'll transfer this over to a Discussion, because I'd love to get comments/ideas. Here is a good article which could seed the discussion: |
Beta Was this translation helpful? Give feedback.
-
Did anyone implement something that can be recommended to achieve this? |
Beta Was this translation helpful? Give feedback.
-
Following up on my post from yesterday since my multi tenancy custom claims setup is now fully working. Here are the steps I took to implement it. Step 1: Make sure to add Step 2: Create a postgres function to extract create or replace function auth.orgid() returns uuid as $$
select nullif(
((current_setting('request.jwt.claims')::jsonb ->> 'app_metadata')::jsonb ->> 'organization_id'),
'')::uuid
$$ language sql; Step 3: Set up your policies. Here is an example policy for my create policy "allow read/write access to boards for users of the same organization"
on public.boards for all using (
(auth.orgid() = boards.organization_id)
); Depending on your policies, you may or may not run into authorization issues when trying to set up a new user. E.g. if you have an I haven't fully implemented it yet, but to use custom claims for role based authorization, you can create a postgres function similar to the create or replace function auth.authz_role() returns integer as $$
select nullif(
((current_setting('request.jwt.claims')::jsonb ->> 'app_metadata')::jsonb ->> 'authz_role')::integer,
0)::integer
$$ language sql; |
Beta Was this translation helpful? Give feedback.
-
I implemented this function: create or replace function auth.my_role() returns text security definer as $$
select nullif((select raw_app_meta_data from auth.users where id = (select nullif(current_setting('request.jwt.claims', true)::json->>'sub', '')::uuid))->>'role', '')::text;
$$ language sql stable; so I still store user role inside that And for the part needing some server to change it, you actually don't need it anymore, you can do it inside RPCs. |
Beta Was this translation helpful? Give feedback.
-
Is there any update here? I just came across db-pre-request functions in postgREST. If we would be able to "enrich" the request once it hits the database similar to how the jwt is set to current_setting that would simplify a lot. EDIT: ALTER ROLE authenticator SET pgrst.db_pre_request to 'your_custom_function';
NOTIFY pgrst, 'reload config'; and to reset ALTER ROLE authenticator RESET pgrst.db_pre_request;
NOTIFY pgrst, 'reload config'; more details here! |
Beta Was this translation helpful? Give feedback.
-
Has any further progress been made with multi-tenancy, basically going through this kind of setup now and it seems overly complex and really should be built in. Hopefully some news in his upcoming release week. |
Beta Was this translation helpful? Give feedback.
-
nhost is having a decent solution for custom jwt claims where we can add any data which is some how related to the auth.users table. |
Beta Was this translation helpful? Give feedback.
-
const firebaseTenantId = 'org1-y6bqo'; // Taken from Cloud Console, where this tenant already exists. |
Beta Was this translation helpful? Give feedback.
-
Relevant repository: https://github.com/supabase-community/supabase-custom-claims |
Beta Was this translation helpful? Give feedback.
-
Feature request
Is your feature request related to a problem? Please describe.
Adding custom claims on the JWT token like in firebase.
This would make multi tenancy and user roles easier to implement with RLS
Describe the solution you'd like
The ability to add custom claims to the JWT token send to the user upon sign in.
For example one or multiple organisation ID's a user belongs to.
This would greatly simplify and speed up RLS policy creation, since the org_id from the token can be directly compared to the org_id of the row without doing an additional query.
eg. what currently uses an additional select query
Would become a simple policy we could add in the web ui
A realtime listener or polling system could be implemented in the client library to automatically refresh the token in case a users custom claims would change.
Beta Was this translation helpful? Give feedback.
All reactions