-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clean up a bunch of bogus subslects and join tables instead.
- Loading branch information
Showing
2 changed files
with
27 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,14 +7,14 @@ $$; | |
|
||
|
||
CREATE OR REPLACE FUNCTION url_decode(data text) RETURNS bytea LANGUAGE sql AS $$ | ||
WITH t AS (SELECT translate(data, '-_', '+/')), | ||
rem AS (SELECT length((SELECT * FROM t)) % 4) -- compute padding size | ||
WITH t AS (SELECT translate(data, '-_', '+/') AS trans), | ||
rem AS (SELECT length(t.trans) % 4 AS remainder FROM t) -- compute padding size | ||
SELECT decode( | ||
(SELECT * FROM t) || | ||
CASE WHEN (SELECT * FROM rem) > 0 | ||
THEN repeat('=', (4 - (SELECT * FROM rem))) | ||
t.trans || | ||
CASE WHEN rem.remainder > 0 | ||
THEN repeat('=', (4 - rem.remainder)) | ||
ELSE '' END, | ||
'base64'); | ||
'base64') FROM t, rem; | ||
$$; | ||
|
||
|
||
|
@@ -26,27 +26,26 @@ WITH | |
WHEN algorithm = 'HS256' THEN 'sha256' | ||
WHEN algorithm = 'HS384' THEN 'sha384' | ||
WHEN algorithm = 'HS512' THEN 'sha512' | ||
ELSE '' END) -- hmac throws error | ||
SELECT @[email protected]_encode(hmac(signables, secret, (select * FROM alg))); | ||
ELSE '' END AS id) -- hmac throws error | ||
SELECT @[email protected]_encode(hmac(signables, secret, alg.id)) FROM alg; | ||
$$; | ||
|
||
|
||
CREATE OR REPLACE FUNCTION sign(payload json, secret text, algorithm text DEFAULT 'HS256') | ||
RETURNS text LANGUAGE sql AS $$ | ||
WITH | ||
header AS ( | ||
SELECT @[email protected]_encode(convert_to('{"alg":"' || algorithm || '","typ":"JWT"}', 'utf8')) | ||
SELECT @[email protected]_encode(convert_to('{"alg":"' || algorithm || '","typ":"JWT"}', 'utf8')) AS data | ||
), | ||
payload AS ( | ||
SELECT @[email protected]_encode(convert_to(payload::text, 'utf8')) | ||
SELECT @[email protected]_encode(convert_to(payload::text, 'utf8')) AS data | ||
), | ||
signables AS ( | ||
SELECT (SELECT * FROM header) || '.' || (SELECT * FROM payload) | ||
SELECT header.data || '.' || payload.data AS data FROM header, payload | ||
) | ||
SELECT | ||
(SELECT * FROM signables) | ||
|| '.' || | ||
@[email protected]_sign((SELECT * FROM signables), secret, algorithm); | ||
signables.data || '.' || | ||
@[email protected]_sign(signables.data, secret, algorithm) FROM signables; | ||
$$; | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters