<commit_before>BEGIN;
DROP SCHEMA IF EXISTS jwt CASCADE;
CREATE SCHEMA jwt;


CREATE OR REPLACE FUNCTION jwt.url_encode(data bytea) RETURNS text LANGUAGE sql AS $$
    SELECT translate(encode(data, 'base64'), '+/=', '-_');
$$;


CREATE OR REPLACE FUNCTION jwt.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
    SELECT decode(
        (SELECT * FROM t) || 
        CASE WHEN (SELECT * FROM rem) > 0 
           THEN repeat('=', (4 - (SELECT * FROM rem))) 
           ELSE '' END, 
    'base64');
$$;


CREATE OR REPLACE FUNCTION jwt.sign(payload json, secret text, algorithm text DEFAULT 'sha256')
RETURNS text LANGUAGE sql AS $$
WITH header AS (SELECT jwt.url_encode(convert_to('{"alg":"HS256","typ":"JWT"}', 'utf8'))),
     payload AS (SELECT jwt.url_encode(convert_to(payload::text, 'utf8'))),
     signables AS (SELECT (SELECT * FROM header) || '.' || (SELECT * FROM payload))
SELECT
    (SELECT * FROM signables)
    || '.' ||
    jwt.url_encode(hmac((SELECT * FROM signables), secret, algorithm));
$$;


CREATE OR REPLACE FUNCTION jwt.verify(token text, secret text, algorithm text DEFAULT 'sha256')
RETURNS table(header json, payload json, valid boolean) LANGUAGE sql AS $$<commit_msg>cleanup<commit_after>BEGIN;
DROP SCHEMA IF EXISTS jwt CASCADE;
CREATE SCHEMA jwt;


CREATE OR REPLACE FUNCTION jwt.url_encode(data bytea) RETURNS text LANGUAGE sql AS $$
    SELECT translate(encode(data, 'base64'), '+/=', '-_');
$$;


CREATE OR REPLACE FUNCTION jwt.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
    SELECT decode(
        (SELECT * FROM t) ||
        CASE WHEN (SELECT * FROM rem) > 0
           THEN repeat('=', (4 - (SELECT * FROM rem)))
           ELSE '' END,
    'base64');
$$;


CREATE OR REPLACE FUNCTION jwt.sign(payload json, secret text, algorithm text DEFAULT 'sha256')
RETURNS text LANGUAGE sql AS $$
WITH header AS (SELECT jwt.url_encode(convert_to('{"alg":"HS256","typ":"JWT"}', 'utf8'))),
     payload AS (SELECT jwt.url_encode(convert_to(payload::text, 'utf8'))),
     signables AS (SELECT (SELECT * FROM header) || '.' || (SELECT * FROM payload))
SELECT
    (SELECT * FROM signables)
    || '.' ||
    jwt.url_encode(hmac((SELECT * FROM signables), secret, algorithm));
$$;


CREATE OR REPLACE FUNCTION jwt.verify(token text, secret text, algorithm text DEFAULT 'sha256')
RETURNS table(header json, payload json, valid boolean) LANGUAGE sql AS $$
{'fraction_non_alphanumeric': 0.14654517843583903, 'fraction_numerical': 0.01366742596810934, 'mean_word_length': 3.852670349907919, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '148104', 'n_tokens_mistral': 905, 'n_tokens_neox': 854, 'n_words': 276}