feat: Make GUC names for headers, cookies and jwt claims compatible with PostgreSQL v14
Getting the value for a header GUC on PostgreSQL v14 is done using `current_setting('request.headers')::json->>'name-of-header'` and in a similar way for `request.cookies` and `request.jwt.claims`
PostgreSQL versions below 14 can opt in to the new JSON GUCs by setting the `db-use-legacy-gucs` config option to false (true by default)
This commit is contained in:
Vendored
+52
-15
@@ -82,7 +82,10 @@ CREATE FUNCTION set_authors_only_owner() RETURNS trigger
|
||||
LANGUAGE plpgsql
|
||||
AS $$
|
||||
begin
|
||||
NEW.owner = current_setting('request.jwt.claim.id');
|
||||
NEW.owner = case when current_setting('server_version_num')::int >= 140000
|
||||
then current_setting('request.jwt.claims')::json->>'id'
|
||||
else current_setting('request.jwt.claim.id')
|
||||
end;
|
||||
RETURN NEW;
|
||||
end
|
||||
$$;
|
||||
@@ -301,7 +304,10 @@ CREATE OR REPLACE FUNCTION switch_role() RETURNS void
|
||||
declare
|
||||
user_id text;
|
||||
Begin
|
||||
user_id = current_setting('request.jwt.claim.id')::text;
|
||||
user_id = case when current_setting('server_version_num')::int >= 140000
|
||||
then (current_setting('request.jwt.claims')::json->>'id')::text
|
||||
else current_setting('request.jwt.claim.id')::text
|
||||
end;
|
||||
if user_id = '1'::text then
|
||||
execute 'set local role postgrest_test_author';
|
||||
elseif user_id = '2'::text then
|
||||
@@ -329,18 +335,33 @@ CREATE FUNCTION reveal_big_jwt() RETURNS TABLE (
|
||||
iss text, sub text, exp bigint,
|
||||
nbf bigint, iat bigint, jti text, "http://postgrest.com/foo" boolean
|
||||
)
|
||||
LANGUAGE sql SECURITY DEFINER
|
||||
LANGUAGE plpgsql SECURITY DEFINER
|
||||
STABLE
|
||||
AS $$
|
||||
SELECT current_setting('request.jwt.claim.iss') as iss,
|
||||
current_setting('request.jwt.claim.sub') as sub,
|
||||
current_setting('request.jwt.claim.exp')::bigint as exp,
|
||||
current_setting('request.jwt.claim.nbf')::bigint as nbf,
|
||||
current_setting('request.jwt.claim.iat')::bigint as iat,
|
||||
current_setting('request.jwt.claim.jti') as jti,
|
||||
-- role is not included in the claims list
|
||||
current_setting('request.jwt.claim.http://postgrest.com/foo')::boolean
|
||||
as "http://postgrest.com/foo";
|
||||
BEGIN
|
||||
-- JWT claims are set in JSON format since v14
|
||||
IF (current_setting('server_version_num')::INT >= 140000) THEN
|
||||
RETURN QUERY
|
||||
SELECT current_setting('request.jwt.claims')::json->>'iss' as iss,
|
||||
current_setting('request.jwt.claims')::json->>'sub' as sub,
|
||||
(current_setting('request.jwt.claims')::json->>'exp')::bigint as exp,
|
||||
(current_setting('request.jwt.claims')::json->>'nbf')::bigint as nbf,
|
||||
(current_setting('request.jwt.claims')::json->>'iat')::bigint as iat,
|
||||
current_setting('request.jwt.claims')::json->>'jti' as jti,
|
||||
(current_setting('request.jwt.claims')::json->>'http://postgrest.com/foo')::boolean
|
||||
as "http://postgrest.com/foo";
|
||||
ELSE
|
||||
RETURN QUERY
|
||||
SELECT current_setting('request.jwt.claim.iss') as iss,
|
||||
current_setting('request.jwt.claim.sub') as sub,
|
||||
current_setting('request.jwt.claim.exp')::bigint as exp,
|
||||
current_setting('request.jwt.claim.nbf')::bigint as nbf,
|
||||
current_setting('request.jwt.claim.iat')::bigint as iat,
|
||||
current_setting('request.jwt.claim.jti') as jti,
|
||||
current_setting('request.jwt.claim.http://postgrest.com/foo')::boolean
|
||||
as "http://postgrest.com/foo";
|
||||
END IF;
|
||||
END;
|
||||
$$;
|
||||
|
||||
|
||||
@@ -1093,6 +1114,11 @@ create function test.get_guc_value(name text) returns text as $$
|
||||
select nullif(current_setting(name), '')::text;
|
||||
$$ language sql;
|
||||
|
||||
-- Get the GUC values for Postgres v14.0 and up
|
||||
create function test.get_guc_value(prefix text, name text) returns text as $$
|
||||
select nullif(current_setting(prefix)::json->>name, '')::text;
|
||||
$$ language sql;
|
||||
|
||||
create table w_or_wo_comma_names ( name text );
|
||||
|
||||
create table items_with_different_col_types (
|
||||
@@ -1784,8 +1810,13 @@ openapi json = $$
|
||||
}
|
||||
}
|
||||
$$;
|
||||
accept text;
|
||||
begin
|
||||
case current_setting('request.header.accept', true)
|
||||
accept = case when current_setting('server_version_num')::int >= 140000
|
||||
then current_setting('request.headers', true)::json->>'accept'
|
||||
else current_setting('request.header.accept', true)
|
||||
end;
|
||||
case accept
|
||||
when 'application/openapi+json' then
|
||||
return openapi;
|
||||
when 'application/json' then
|
||||
@@ -1958,9 +1989,15 @@ add constraint snd_shift foreign key (snd_shift_activity_id, snd_shift
|
||||
-- for a pre-request function
|
||||
create or replace function custom_headers() returns void as $$
|
||||
declare
|
||||
user_agent text := current_setting('request.header.user-agent', true);
|
||||
user_agent text := case when current_setting('server_version_num')::int >= 140000
|
||||
then current_setting('request.headers', true)::json->>'user-agent'
|
||||
else current_setting('request.header.user-agent', true)
|
||||
end;
|
||||
req_path text := current_setting('request.path', true);
|
||||
req_accept text := current_setting('request.header.accept', true);
|
||||
req_accept text := case when current_setting('server_version_num')::int >= 140000
|
||||
then current_setting('request.headers', true)::json->>'accept'
|
||||
else current_setting('request.header.accept', true)
|
||||
end;
|
||||
req_method text := current_setting('request.method', true);
|
||||
begin
|
||||
if user_agent similar to 'MSIE (6.0|7.0)' then
|
||||
|
||||
Reference in New Issue
Block a user