Skip to content

Commit f00296f

Browse files
authored
Merge pull request #18 from maparent/immutable
Mark functions as immutable (and bump version)
2 parents 6edf63c + 3d54364 commit f00296f

File tree

4 files changed

+66
-7
lines changed

4 files changed

+66
-7
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
EXTENSION = pgjwt
2-
DATA = pgjwt--0.1.0.sql
2+
DATA = pgjwt--0.1.1.sql pgjwt--0.1.0--0.1.1.sql
33

44
# postgres build stuff
55
PG_CONFIG = pg_config

pgjwt--0.1.0.sql pgjwt--0.1.0--0.1.1.sql

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
CREATE OR REPLACE FUNCTION url_encode(data bytea) RETURNS text LANGUAGE sql AS $$
55
SELECT translate(encode(data, 'base64'), E'+/=\n', '-_');
6-
$$;
6+
$$ IMMUTABLE;
77

88

99
CREATE OR REPLACE FUNCTION url_decode(data text) RETURNS bytea LANGUAGE sql AS $$
@@ -15,7 +15,7 @@ WITH t AS (SELECT translate(data, '-_', '+/') AS trans),
1515
THEN repeat('=', (4 - rem.remainder))
1616
ELSE '' END,
1717
'base64') FROM t, rem;
18-
$$;
18+
$$ IMMUTABLE;
1919

2020

2121
CREATE OR REPLACE FUNCTION algorithm_sign(signables text, secret text, algorithm text)
@@ -28,7 +28,7 @@ WITH
2828
WHEN algorithm = 'HS512' THEN 'sha512'
2929
ELSE '' END AS id) -- hmac throws error
3030
SELECT @[email protected]_encode(@[email protected](signables, secret, alg.id)) FROM alg;
31-
$$;
31+
$$ IMMUTABLE;
3232

3333

3434
CREATE OR REPLACE FUNCTION sign(payload json, secret text, algorithm text DEFAULT 'HS256')
@@ -46,7 +46,7 @@ WITH
4646
SELECT
4747
signables.data || '.' ||
4848
@[email protected]_sign(signables.data, secret, algorithm) FROM signables;
49-
$$;
49+
$$ IMMUTABLE;
5050

5151

5252
CREATE OR REPLACE FUNCTION verify(token text, secret text, algorithm text DEFAULT 'HS256')
@@ -56,4 +56,4 @@ RETURNS table(header json, payload json, valid boolean) LANGUAGE sql AS $$
5656
convert_from(@[email protected]_decode(r[2]), 'utf8')::json AS payload,
5757
r[3] = @[email protected]_sign(r[1] || '.' || r[2], secret, algorithm) AS valid
5858
FROM regexp_split_to_array(token, '\.') r;
59-
$$;
59+
$$ IMMUTABLE;

pgjwt--0.1.1.sql

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
\echo Use "CREATE EXTENSION pgjwt" to load this file. \quit
2+
3+
4+
CREATE OR REPLACE FUNCTION url_encode(data bytea) RETURNS text LANGUAGE sql AS $$
5+
SELECT translate(encode(data, 'base64'), E'+/=\n', '-_');
6+
$$ IMMUTABLE;
7+
8+
9+
CREATE OR REPLACE FUNCTION url_decode(data text) RETURNS bytea LANGUAGE sql AS $$
10+
WITH t AS (SELECT translate(data, '-_', '+/') AS trans),
11+
rem AS (SELECT length(t.trans) % 4 AS remainder FROM t) -- compute padding size
12+
SELECT decode(
13+
t.trans ||
14+
CASE WHEN rem.remainder > 0
15+
THEN repeat('=', (4 - rem.remainder))
16+
ELSE '' END,
17+
'base64') FROM t, rem;
18+
$$ IMMUTABLE;
19+
20+
21+
CREATE OR REPLACE FUNCTION algorithm_sign(signables text, secret text, algorithm text)
22+
RETURNS text LANGUAGE sql AS $$
23+
WITH
24+
alg AS (
25+
SELECT CASE
26+
WHEN algorithm = 'HS256' THEN 'sha256'
27+
WHEN algorithm = 'HS384' THEN 'sha384'
28+
WHEN algorithm = 'HS512' THEN 'sha512'
29+
ELSE '' END AS id) -- hmac throws error
30+
SELECT @[email protected]_encode(@[email protected](signables, secret, alg.id)) FROM alg;
31+
$$ IMMUTABLE;
32+
33+
34+
CREATE OR REPLACE FUNCTION sign(payload json, secret text, algorithm text DEFAULT 'HS256')
35+
RETURNS text LANGUAGE sql AS $$
36+
WITH
37+
header AS (
38+
SELECT @[email protected]_encode(convert_to('{"alg":"' || algorithm || '","typ":"JWT"}', 'utf8')) AS data
39+
),
40+
payload AS (
41+
SELECT @[email protected]_encode(convert_to(payload::text, 'utf8')) AS data
42+
),
43+
signables AS (
44+
SELECT header.data || '.' || payload.data AS data FROM header, payload
45+
)
46+
SELECT
47+
signables.data || '.' ||
48+
@[email protected]_sign(signables.data, secret, algorithm) FROM signables;
49+
$$ IMMUTABLE;
50+
51+
52+
CREATE OR REPLACE FUNCTION verify(token text, secret text, algorithm text DEFAULT 'HS256')
53+
RETURNS table(header json, payload json, valid boolean) LANGUAGE sql AS $$
54+
SELECT
55+
convert_from(@[email protected]_decode(r[1]), 'utf8')::json AS header,
56+
convert_from(@[email protected]_decode(r[2]), 'utf8')::json AS payload,
57+
r[3] = @[email protected]_sign(r[1] || '.' || r[2], secret, algorithm) AS valid
58+
FROM regexp_split_to_array(token, '\.') r;
59+
$$ IMMUTABLE;

pgjwt.control

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# pgjwt extension
22
comment = 'JSON Web Token API for Postgresql'
3-
default_version = '0.1.0'
3+
default_version = '0.1.1'
44
relocatable = false
55
requires = pgcrypto
66
superuser = false

0 commit comments

Comments
 (0)