38 lines
633 B
PL/PgSQL
38 lines
633 B
PL/PgSQL
CREATE FUNCTION _api.verify_jwt(
|
|
_token TEXT
|
|
)
|
|
RETURNS INTEGER
|
|
LANGUAGE plpgsql VOLATILE
|
|
AS $BODY$
|
|
DECLARE
|
|
_payload JSON;
|
|
_valid BOOLEAN;
|
|
_jwt_secret TEXT;
|
|
BEGIN
|
|
SELECT jwt_secret INTO _jwt_secret
|
|
FROM sys.database_info
|
|
WHERE name = current_database();
|
|
|
|
SELECT payload, valid
|
|
INTO _payload, _valid
|
|
FROM public.verify(
|
|
_token,
|
|
_jwt_secret
|
|
);
|
|
|
|
IF NOT FOUND THEN
|
|
RETURN NULL;
|
|
END IF;
|
|
|
|
IF _valid <> TRUE THEN
|
|
RETURN NULL;
|
|
END IF;
|
|
|
|
RETURN _payload->>'user_id';
|
|
END
|
|
$BODY$;
|
|
|
|
GRANT EXECUTE ON FUNCTION _api.verify_jwt(TEXT)
|
|
TO rest_anon, rest_user;
|
|
GRANT SELECT ON TABLE sys.database_info
|
|
TO rest_anon, rest_user;
|