From: Daniel Verite <daniel@manitou-mail.org>
To: Masih Tavassoli <mtavasso@yahoo.co.uk>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: JWT decoder
Date: Wed, 11 Aug 2021 13:24:57 +0200
Message-ID: <1cbbbbde-fced-4091-acdf-a97471e3a2f4@manitou-mail.org> (raw)
In-Reply-To: <2138331050.291272.1628475372080@mail.yahoo.com>
Masih Tavassoli wrote:
> But there doesn't seem to be a way doing it in postgres.
> Has anyone got any suggesions?
RFC 7519 says:
A JWT is represented as a sequence of URL-safe parts separated by
period ('.') characters. Each part contains a base64url-encoded
value. The number of parts in the JWT is dependent upon the
representation of the resulting JWS using the JWS Compact
Serialization or JWE using the JWE Compact Serialization.
base64url is similar to base64 except that the two characters
+ and / are replaced by - and _
Postgres provides decode(..., 'base64') but it's stricter than the
Oracle version showed in your sample code (which seems to ignore the
dot character that is illegal in base64 whereas Postgres would reject
it).
The JWT may be decoded with built-in Postgres functions by
splitting the dot-separated parts with regexp_split_to_table(),
converting them from base64url into binary, then into UTF-8,
and then the results could be cast into the json type if needed.
So the SQL code could be:
create function decode_base64url(text) returns bytea as $$
select decode(
rpad(translate($1, '-_', '+/') -- pad to the next multiple of 4 bytes
,4*((length($1)+3)/4)
,'=')
,'base64');
$$ language sql strict immutable;
with parts(x,n) as (
select * from regexp_split_to_table('<insert the JWT here>', '\.')
with ordinality
)
select n, convert_from(decode_base64url(x), 'utf-8')
from parts where n in (1,2);
"n" in the query is the part number, 1 for the header, 2 for the
payload, 3 for the signature which is ignored here.
Best regards,
--
Daniel Vérité
PostgreSQL-powered mailer: https://www.manitou-mail.org
Twitter: @DanielVerite
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: daniel@manitou-mail.org, mtavasso@yahoo.co.uk
Subject: Re: JWT decoder
In-Reply-To: <1cbbbbde-fced-4091-acdf-a97471e3a2f4@manitou-mail.org>
References: <2138331050.291272.1628475372080@mail.yahoo.com> <1cbbbbde-fced-4091-acdf-a97471e3a2f4@manitou-mail.org>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox