Skip to content

Commit b646b97

Browse files
authored
Merge pull request #99 from constructive-io/feat/jwt-claims-api-id
feat(jwt-claims): add jwt_private.current_api_id()
2 parents 6667cd1 + e1c9cc5 commit b646b97

5 files changed

Lines changed: 70 additions & 1 deletion

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
-- Deploy schemas/jwt_private/procedures/current_api_id to pg
2+
-- Retrieves the current API ID from JWT claims (private/internal use)
3+
4+
-- requires: schemas/jwt_private/schema
5+
6+
BEGIN;
7+
8+
-- Returns the current API UUID from the JWT claims
9+
-- Used for API invocation provenance checks in RLS policies
10+
-- Returns NULL if the claim is not set or invalid
11+
CREATE FUNCTION jwt_private.current_api_id()
12+
RETURNS uuid
13+
AS $$
14+
DECLARE
15+
v_identifier_id uuid;
16+
BEGIN
17+
IF current_setting('jwt.claims.api_id', TRUE)
18+
IS NOT NULL THEN
19+
BEGIN
20+
v_identifier_id = current_setting('jwt.claims.api_id', TRUE)::uuid;
21+
EXCEPTION
22+
WHEN OTHERS THEN
23+
RAISE NOTICE 'Invalid UUID value';
24+
RETURN NULL;
25+
END;
26+
RETURN v_identifier_id;
27+
ELSE
28+
RETURN NULL;
29+
END IF;
30+
END;
31+
$$
32+
LANGUAGE 'plpgsql' STABLE;
33+
34+
COMMIT;

packages/jwt-claims/pgpm.plan

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ schemas/jwt_private/schema 2020-12-17T06:47:34Z Dan Lynch <dlynch@Dans-MBP-3> #
1919
schemas/jwt_private/procedures/current_database_id [schemas/jwt_private/schema] 2020-12-17T23:22:28Z Dan Lynch <dlynch@Dans-MBP-3> # add schemas/jwt_private/procedures/current_database_id
2020
schemas/jwt_private/procedures/current_token_id [schemas/jwt_private/schema] 2017-08-11T08:11:51Z skitch <skitch@5b0c196eeb62> # add schemas/jwt_private/procedures/current_token_id
2121
schemas/jwt_private/procedures/current_session_id [schemas/jwt_private/schema] 2026-01-28T05:44:00Z Dan Lynch <dlynch@constructive.io> # add schemas/jwt_private/procedures/current_session_id
22+
schemas/jwt_private/procedures/current_api_id [schemas/jwt_private/schema] 2026-07-12T13:40:27Z Dan Lynch <dlynch@constructive.io> # add schemas/jwt_private/procedures/current_api_id
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- Revert schemas/jwt_private/procedures/current_api_id from pg
2+
3+
BEGIN;
4+
5+
DROP FUNCTION jwt_private.current_api_id;
6+
7+
COMMIT;

packages/jwt-claims/sql/pgpm-jwt-claims--0.15.5.sql

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,24 @@ $EOFCODE$ LANGUAGE sql STABLE;
177177

178178
CREATE FUNCTION jwt_private.current_session_id() RETURNS uuid AS $EOFCODE$
179179
SELECT nullif(current_setting('jwt.claims.session_id', true), '')::uuid;
180-
$EOFCODE$ LANGUAGE sql STABLE;
180+
$EOFCODE$ LANGUAGE sql STABLE;
181+
182+
CREATE FUNCTION jwt_private.current_api_id() RETURNS uuid AS $EOFCODE$
183+
DECLARE
184+
v_identifier_id uuid;
185+
BEGIN
186+
IF current_setting('jwt.claims.api_id', TRUE)
187+
IS NOT NULL THEN
188+
BEGIN
189+
v_identifier_id = current_setting('jwt.claims.api_id', TRUE)::uuid;
190+
EXCEPTION
191+
WHEN OTHERS THEN
192+
RAISE NOTICE 'Invalid UUID value';
193+
RETURN NULL;
194+
END;
195+
RETURN v_identifier_id;
196+
ELSE
197+
RETURN NULL;
198+
END IF;
199+
END;
200+
$EOFCODE$ LANGUAGE plpgsql STABLE;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- Verify schemas/jwt_private/procedures/current_api_id on pg
2+
3+
BEGIN;
4+
5+
SELECT verify_function ('jwt_private.current_api_id');
6+
7+
ROLLBACK;

0 commit comments

Comments
 (0)