Skip to content

Commit

Permalink
Add some db scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
openint-bot committed Feb 27, 2025
1 parent 7141c3d commit cf4cfd4
Show file tree
Hide file tree
Showing 2 changed files with 581 additions and 0 deletions.
51 changes: 51 additions & 0 deletions packages-v1/db-v1/scripts/decode-jwt-no-verify.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
-- Via https://github.com/leandropls/pgsqljwt/blob/main/decode_jwt.sql
-- Workaround for neon rls authorize issues on local for now...
/**
* Decodes a JWT without verifying its signature
*
* @param {text} token - The JWT to be decoded and verified.
* @returns {jsonb} - The decoded JWT claims in JSONB format if the signature is valid, null otherwise.
*/
create or replace function jwt.decode_jwt_no_verify(token text)
returns jsonb as
$$
declare
-- Variables to store JWT segments.
segments text[]; -- The segments of the JWT (header, claims, signature).
claims_segment text; -- The claims segment of the JWT.
claims jsonb; -- JSONB decoded from the claims segment.
begin
-- Check if the token or keys are null; if so, return null (indicating validation failure).
if token is null then
return null;
end if;

-- Split the token into its segments (header, claims, signature)
segments := string_to_array(token, '.');
assert segments is not null;

if array_length(segments, 1) <> 3 then
return null;
end if;

claims_segment := segments[2];

-- Check if any of the segments are null; if so, return null (indicating validation failure).
if claims_segment is null then
return null;
end if;

-- Attempt to decode and parse the claims segment into JSONB; return null on failure.
begin
claims := convert_from(urlsafe_b64decode(claims_segment), 'UTF-8')::jsonb;
exception
when others then
return null;
end;

assert claims is not null;
return claims;
end;
$$ language plpgsql
immutable
set search_path = jwt, public, pg_temp;
Loading

0 comments on commit cf4cfd4

Please sign in to comment.