Fix custom pre-request handler
Uses Ruslan's technique
This commit is contained in:
Vendored
+2
@@ -2,3 +2,5 @@ DROP DATABASE IF EXISTS postgrest_test;
|
||||
DROP ROLE IF EXISTS postgrest_test;
|
||||
CREATE USER postgrest_test createdb createrole;
|
||||
CREATE DATABASE postgrest_test OWNER postgrest_test;
|
||||
|
||||
ALTER DATABASE postgrest_test SET postgrest.claims.id = '-1';
|
||||
|
||||
Vendored
+1
-1
@@ -4,7 +4,7 @@ GRANT USAGE ON SCHEMA
|
||||
, test
|
||||
, jwt
|
||||
, "تست"
|
||||
TO postgrest_test_anonymous, bad_role;
|
||||
TO postgrest_test_anonymous;
|
||||
|
||||
-- Schema test objects
|
||||
SET search_path = test, "تست", pg_catalog;
|
||||
|
||||
Vendored
+2
-3
@@ -1,8 +1,7 @@
|
||||
DROP ROLE IF EXISTS postgrest_test_authenticator, postgrest_test_anonymous, postgrest_test_default_role, postgrest_test_author, bad_role;
|
||||
DROP ROLE IF EXISTS postgrest_test_authenticator, postgrest_test_anonymous, postgrest_test_default_role, postgrest_test_author;
|
||||
CREATE ROLE postgrest_test_authenticator WITH login noinherit;
|
||||
CREATE ROLE postgrest_test_anonymous;
|
||||
CREATE ROLE postgrest_test_default_role;
|
||||
CREATE ROLE postgrest_test_author;
|
||||
CREATE ROLE bad_role;
|
||||
|
||||
GRANT postgrest_test_anonymous, postgrest_test_default_role, postgrest_test_author, bad_role TO postgrest_test_authenticator;
|
||||
GRANT postgrest_test_anonymous, postgrest_test_default_role, postgrest_test_author TO postgrest_test_authenticator;
|
||||
|
||||
Vendored
+21
-4
@@ -237,15 +237,32 @@ SELECT jwt.sign(
|
||||
) r;
|
||||
$$;
|
||||
|
||||
create function block_bad_role() returns void
|
||||
language plpgsql as $$
|
||||
|
||||
CREATE OR REPLACE FUNCTION switch_role() RETURNS void
|
||||
LANGUAGE plpgsql
|
||||
AS $$
|
||||
declare
|
||||
user_id text;
|
||||
begin
|
||||
if current_role = 'bad_role' then
|
||||
raise invalid_password using message = 'role is not allowed';
|
||||
user_id = current_setting('postgrest.claims.id')::text;
|
||||
if user_id = '1'::text then
|
||||
execute 'set local role postgrest_test_author';
|
||||
elseif user_id = '2'::text then
|
||||
execute 'set local role postgrest_test_default_role';
|
||||
elseif user_id = '3'::text then
|
||||
RAISE EXCEPTION 'Disabled ID --> %', user_id USING HINT = 'Please contact administrator';
|
||||
/* else */
|
||||
/* execute 'set local role postgrest_test_anonymous'; */
|
||||
end if;
|
||||
end
|
||||
$$;
|
||||
|
||||
CREATE FUNCTION get_current_user() RETURNS text
|
||||
LANGUAGE sql
|
||||
AS $$
|
||||
SELECT current_user::text;
|
||||
$$;
|
||||
|
||||
--
|
||||
-- Name: reveal_big_jwt(); Type: FUNCTION; Schema: test; Owner: -
|
||||
--
|
||||
|
||||
Reference in New Issue
Block a user