\q
--
-- PostgreSQL database dump
--

-- Dumped from database version 15.13
-- Dumped by pg_dump version 15.13

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;

--
-- Name: basic_auth; Type: SCHEMA; Schema: -; Owner: postgres
--

CREATE SCHEMA basic_auth;


ALTER SCHEMA basic_auth OWNER TO postgres;

--
-- Name: check_user_pass(text, text); Type: FUNCTION; Schema: basic_auth; Owner: postgres
--

CREATE FUNCTION basic_auth.check_user_pass(id text, password text) RETURNS name
    LANGUAGE sql
    AS $_$
  SELECT user_id AS username
  FROM legacy.all_user
  -- parse SCRAM hash:
  CROSS JOIN LATERAL regexp_match(password, '^SCRAM-SHA-256\$(.*):(.*)\$(.*):(.*)$') AS rm
  CROSS JOIN LATERAL (
    SELECT 
      rm[1]::integer AS iteration_count,
      decode(rm[2], 'base64') AS salt,
      decode(rm[3], 'base64') AS stored_key,
      decode(rm[4], 'base64') AS server_key,
      32 AS digest_length
  ) AS stored_password_part
  -- compute hash from input:
  CROSS JOIN LATERAL (
    SELECT basic_auth.pbkdf2(salt, check_user_pass.password, iteration_count, digest_length, 'sha256')
  ) AS digest_key(digest_key)
  CROSS JOIN LATERAL (
    SELECT 
      digest(hmac('Client Key', digest_key, 'sha256'), 'sha256') AS stored_key,
      hmac('Server Key', digest_key, 'sha256') AS server_key
  ) AS check_password_part
  WHERE user_id = check_user_pass.id
    AND rm IS NOT NULL AND array_length(rm, 1) = 4
    AND check_password_part.stored_key = stored_password_part.stored_key
    AND check_password_part.server_key = stored_password_part.server_key;
$_$;


ALTER FUNCTION basic_auth.check_user_pass(id text, password text) OWNER TO postgres;

--
-- Name: pbkdf2(bytea, text, integer, integer, text); Type: FUNCTION; Schema: basic_auth; Owner: postgres
--

CREATE FUNCTION basic_auth.pbkdf2(salt bytea, pw text, count integer, desired_length integer, algorithm text) RETURNS bytea
    LANGUAGE plpgsql IMMUTABLE
    AS $$
DECLARE
  hash_length integer;
  block_count integer;
  output bytea;
  the_last bytea;
  xorsum bytea;
  i_as_int32 bytea;
  i integer;
  j integer;
  k integer;
BEGIN
  algorithm := lower(algorithm);
  CASE algorithm
  WHEN 'md5' then
    hash_length := 16;
  WHEN 'sha1' then
    hash_length = 20;
  WHEN 'sha256' then
    hash_length = 32;
  WHEN 'sha512' then
    hash_length = 64;
  ELSE
    RAISE EXCEPTION 'Unknown algorithm "%"', algorithm;
  END CASE;
  --
  block_count := ceil(desired_length::real / hash_length::real);
  --
  FOR i in 1 .. block_count LOOP
    i_as_int32 := E'\\000\\000\\000'::bytea || chr(i)::bytea;
    i_as_int32 := substring(i_as_int32, length(i_as_int32) - 3);
    --
    the_last := salt::bytea || i_as_int32;
    --
    xorsum := HMAC(the_last, pw::bytea, algorithm);
    the_last := xorsum;
    --
    FOR j IN 2 .. count LOOP
      the_last := HMAC(the_last, pw::bytea, algorithm);

      -- xor the two
      FOR k IN 1 .. length(xorsum) LOOP
        xorsum := set_byte(xorsum, k - 1, get_byte(xorsum, k - 1) # get_byte(the_last, k - 1));
      END LOOP;
    END LOOP;
    --
    IF output IS NULL THEN
      output := xorsum;
    ELSE
      output := output || xorsum;
    END IF;
  END LOOP;
  --
  RETURN substring(output FROM 1 FOR desired_length);
END $$;


ALTER FUNCTION basic_auth.pbkdf2(salt bytea, pw text, count integer, desired_length integer, algorithm text) OWNER TO postgres;

--
-- PostgreSQL database dump complete
--

