From 3522d3b9d7d9648bff33295126f4073f174030e8 Mon Sep 17 00:00:00 2001 From: xbgmsharp Date: Wed, 30 Nov 2022 21:12:49 +0100 Subject: [PATCH] Update email type to CITEXT, https://www.postgresql.org/docs/current/citext.html --- initdb/02_4_signalk_auth.sql | 6 +++--- initdb/02_5_signalk_auth_otp.sql | 11 ++++++----- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/initdb/02_4_signalk_auth.sql b/initdb/02_4_signalk_auth.sql index 33d85f6..38fd78e 100644 --- a/initdb/02_4_signalk_auth.sql +++ b/initdb/02_4_signalk_auth.sql @@ -22,7 +22,7 @@ CREATE EXTENSION IF NOT EXISTS "pgcrypto"; -- provides cryptographic functions DROP TABLE IF EXISTS auth.accounts CASCADE; CREATE TABLE IF NOT EXISTS auth.accounts ( userid UUID NOT NULL UNIQUE DEFAULT uuid_generate_v4(), - email text primary key check ( email ~* '^.+@.+\..+$' ), + email CITEXT primary key check ( email ~* '^.+@.+\..+$' ), first text not null check (length(pass) < 512), last text not null check (length(pass) < 512), pass text not null check (length(pass) < 512), @@ -55,7 +55,7 @@ COMMENT ON TRIGGER accounts_moddatetime DROP TABLE IF EXISTS auth.vessels; CREATE TABLE IF NOT EXISTS auth.vessels ( vesselid TEXT NOT NULL UNIQUE DEFAULT RIGHT(gen_random_uuid()::text, 12), - owner_email TEXT PRIMARY KEY REFERENCES auth.accounts(email) ON DELETE RESTRICT, + owner_email CITEXT PRIMARY KEY REFERENCES auth.accounts(email) ON DELETE RESTRICT, mmsi TEXT UNIQUE, -- Should be a numeric range between 100000000 and 800000000. -- mmsi NUMERIC UNIQUE, -- MMSI can be optional but if present must be a valid one name TEXT NOT NULL CHECK (length(name) >= 3 AND length(name) < 512), @@ -73,7 +73,7 @@ COMMENT ON TABLE -- Indexes CREATE INDEX vessels_role_idx ON auth.vessels (role); CREATE INDEX vessels_name_idx ON auth.vessels (name); -CREATE INDEX vessels_vesseid_idx ON auth.vessels (vesseid); +CREATE INDEX vessels_vesselid_idx ON auth.vessels (vesselid); CREATE TRIGGER vessels_moddatetime BEFORE UPDATE ON auth.vessels diff --git a/initdb/02_5_signalk_auth_otp.sql b/initdb/02_5_signalk_auth_otp.sql index 22ca768..df3ae6a 100644 --- a/initdb/02_5_signalk_auth_otp.sql +++ b/initdb/02_5_signalk_auth_otp.sql @@ -10,7 +10,8 @@ select current_database(); DROP TABLE IF EXISTS auth.otp; CREATE TABLE IF NOT EXISTS auth.otp ( - user_email TEXT NOT NULL PRIMARY KEY REFERENCES auth.accounts(email) ON DELETE RESTRICT, + -- update type to CITEXT, https://www.postgresql.org/docs/current/citext.html + user_email CITEXT NOT NULL PRIMARY KEY REFERENCES auth.accounts(email) ON DELETE RESTRICT, otp_pass VARCHAR(10) NOT NULL, otp_timestamp TIMESTAMP WITHOUT TIME ZONE DEFAULT NOW(), otp_tries SMALLINT NOT NULL DEFAULT '0' @@ -51,14 +52,14 @@ DROP FUNCTION IF EXISTS api.generate_otp_fn; CREATE OR REPLACE FUNCTION api.generate_otp_fn(IN email TEXT) RETURNS TEXT AS $generate_otp$ DECLARE - _email TEXT := email; + _email CITEXT := email; _email_check TEXT := NULL; otp_pass VARCHAR(10) := NULL; BEGIN IF email IS NULL OR _email IS NULL OR _email = '' THEN RAISE EXCEPTION 'invalid input' USING HINT = 'check your parameter'; END IF; - SELECT lower(a.email) INTO _email_check FROM auth.accounts a WHERE lower(a.email) = lower(_email); + SELECT lower(a.email) INTO _email_check FROM auth.accounts a WHERE a.email = _email; IF _email_check IS NULL THEN RETURN NULL; END IF; @@ -227,7 +228,7 @@ DROP FUNCTION IF EXISTS auth.telegram_user_exists_fn; CREATE OR REPLACE FUNCTION auth.telegram_user_exists_fn(IN email TEXT, IN chat_id BIGINT) RETURNS BOOLEAN AS $telegram_user_exists$ declare - _email TEXT := email; + _email CITEXT := email; _chat_id BIGINT := chat_id; BEGIN IF _email IS NULL OR _chat_id IS NULL THEN @@ -236,7 +237,7 @@ AS $telegram_user_exists$ -- Does user and telegram obj SELECT preferences->'telegram'->'id' INTO _chat_id FROM auth.accounts a - WHERE lower(a.email) = lower(_email) + WHERE a.email = _email AND cast(preferences->'telegram'->'id' as BIGINT) = _chat_id::BIGINT; IF FOUND THEN RETURN TRUE;