-
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.
Merge pull request #21 from burrscurr/master
Check nbf and exp claims
- Loading branch information
Showing
4 changed files
with
128 additions
and
3 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
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
CREATE OR REPLACE FUNCTION try_cast_double(inp text) | ||
RETURNS double precision AS $$ | ||
BEGIN | ||
BEGIN | ||
RETURN inp::double precision; | ||
EXCEPTION | ||
WHEN OTHERS THEN RETURN NULL; | ||
END; | ||
END; | ||
$$ language plpgsql IMMUTABLE; | ||
|
||
|
||
CREATE OR REPLACE FUNCTION verify(token text, secret text, algorithm text DEFAULT 'HS256') | ||
RETURNS table(header json, payload json, valid boolean) LANGUAGE sql AS $$ | ||
SELECT | ||
jwt.header AS header, | ||
jwt.payload AS payload, | ||
jwt.signature_ok AND tstzrange( | ||
to_timestamp(try_cast_double(jwt.payload->>'nbf')), | ||
to_timestamp(try_cast_double(jwt.payload->>'exp')) | ||
) @> CURRENT_TIMESTAMP AS valid | ||
FROM ( | ||
SELECT | ||
convert_from(@extschema@.url_decode(r[1]), 'utf8')::json AS header, | ||
convert_from(@extschema@.url_decode(r[2]), 'utf8')::json AS payload, | ||
r[3] = @extschema@.algorithm_sign(r[1] || '.' || r[2], secret, algorithm) AS signature_ok | ||
FROM regexp_split_to_array(token, '\.') r | ||
) jwt | ||
$$ IMMUTABLE; |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
# pgjwt extension | ||
comment = 'JSON Web Token API for Postgresql' | ||
default_version = '0.1.1' | ||
default_version = '0.2.0' | ||
relocatable = false | ||
requires = pgcrypto | ||
superuser = false |
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