From ff90b1959f8afc8537656788ca1561e87846c80b Mon Sep 17 00:00:00 2001 From: Lucien Cartier-Tilet Date: Sat, 10 Aug 2024 11:06:18 +0200 Subject: [PATCH] feat: authentication through Discord OAuth2 --- migrations/20240809173617_users.down.sql | 5 ++++ migrations/20240809173617_users.up.sql | 29 ++++++++++++++++++++++++ settings/base.yaml | 4 ++++ src/settings.rs | 7 ++++++ 4 files changed, 45 insertions(+) create mode 100644 migrations/20240809173617_users.down.sql create mode 100644 migrations/20240809173617_users.up.sql diff --git a/migrations/20240809173617_users.down.sql b/migrations/20240809173617_users.down.sql new file mode 100644 index 0000000..7f7549a --- /dev/null +++ b/migrations/20240809173617_users.down.sql @@ -0,0 +1,5 @@ +-- Add down migration script here +ALTER TABLE IF EXISTS public.sessions DROP CONSTRAINT IF EXISTS sessions_user_id_users_fk; +DROP TABLE IF EXISTS public.sessions; +DROP TABLE IF EXISTS public.users; +DROP EXTENSION IF EXISTS "uuid-ossp"; diff --git a/migrations/20240809173617_users.up.sql b/migrations/20240809173617_users.up.sql new file mode 100644 index 0000000..1a35bcc --- /dev/null +++ b/migrations/20240809173617_users.up.sql @@ -0,0 +1,29 @@ +-- Add up migration script here +CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; + +CREATE TABLE IF NOT EXISTS public.users +( + id uuid NOT NULL DEFAULT uuid_generate_v4(), + email character varying(255) NOT NULL, + created_at timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP, + last_updated timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (id), + CONSTRAINT users_email_unique UNIQUE (email) +); + +CREATE TABLE IF NOT EXISTS public.sessions +( + id uuid NOT NULL DEFAULT uuid_generate_v4(), + user_id uuid NOT NULL, + session_id character varying NOT NULL, + expires_at timestamp with time zone NOT NULL, + PRIMARY KEY (id), + CONSTRAINT sessions_user_id_unique UNIQUE (user_id) +); + +ALTER TABLE IF EXISTS public.sessions + ADD CONSTRAINT sessions_user_id_users_fk FOREIGN KEY (user_id) + REFERENCES public.users (id) MATCH SIMPLE + ON UPDATE CASCADE + ON DELETE CASCADE + NOT VALID; diff --git a/settings/base.yaml b/settings/base.yaml index c649ef1..851147c 100644 --- a/settings/base.yaml +++ b/settings/base.yaml @@ -16,3 +16,7 @@ email: user: user@gege-jdr-backend.example from: GegeJdrBackend password: hunter2 + +discord: + client_id: changeme + client_secret: changeme diff --git a/src/settings.rs b/src/settings.rs index 97e9d52..c362473 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -4,6 +4,7 @@ use sqlx::ConnectOptions; pub struct Settings { pub application: ApplicationSettings, pub database: Database, + pub discord: Discord, pub debug: bool, pub email: EmailSettings, pub frontend_url: String, @@ -164,6 +165,12 @@ pub struct EmailSettings { pub from: String, } +#[derive(Debug, serde::Deserialize, Clone, Default)] +pub struct Discord { + client_id: String, + client_secret: String, +} + #[cfg(test)] mod tests { use super::*;