initial commit
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
CREATE TABLE IF NOT EXISTS public.users (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
updated_at timestamptz NOT NULL DEFAULT now(),
|
||||
email text NOT NULL UNIQUE,
|
||||
password_hash text NOT NULL,
|
||||
first_name text NOT NULL,
|
||||
second_name text NOT NULL,
|
||||
birth_date date,
|
||||
sex text NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS public.oauth_clients (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
client_id text NOT NULL UNIQUE,
|
||||
client_secret text NOT NULL,
|
||||
name text NOT NULL,
|
||||
redirect_uris JSONB NOT NULL DEFAULT '[]'::jsonb,
|
||||
grant_types JSONB NOT NULL DEFAULT '[]'::jsonb,
|
||||
scopes JSONB DEFAULT '[]'::jsonb,
|
||||
is_active boolean DEFAULT TRUE,
|
||||
trusted boolean DEFAULT FALSE,
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
updated_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS public.authorization_codes (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
code_hash text NOT NULL UNIQUE,
|
||||
user_id uuid NOT NULL REFERENCES public.users(id) ON DELETE CASCADE,
|
||||
client_id uuid NOT NULL REFERENCES public.oauth_clients(id) ON DELETE CASCADE,
|
||||
redirect_uri text NOT NULL,
|
||||
scopes jsonb NOT NULL DEFAULT '[]'::jsonb,
|
||||
code_challenge text NOT NULL,
|
||||
code_challenge_method text NOT NULL DEFAULT 'S256',
|
||||
expires_at timestamptz NOT NULL,
|
||||
consumed_at timestamptz,
|
||||
created_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_authorization_codes_code_hash ON public.authorization_codes (code_hash);
|
||||
CREATE INDEX IF NOT EXISTS idx_authorization_codes_expires_at ON public.authorization_codes (expires_at);
|
||||
CREATE INDEX IF NOT EXISTS idx_authorization_codes_consumed_at ON public.authorization_codes (consumed_at);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS public.refresh_tokens (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
token_hash text NOT NULL UNIQUE,
|
||||
user_id uuid NOT NULL REFERENCES public.users(id) ON DELETE CASCADE,
|
||||
client_id uuid NOT NULL REFERENCES public.oauth_clients(id) ON DELETE CASCADE,
|
||||
scopes jsonb NOT NULL DEFAULT '[]'::jsonb,
|
||||
expires_at timestamptz NOT NULL,
|
||||
created_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_refresh_tokens_token_hash ON public.refresh_tokens (token_hash);
|
||||
CREATE INDEX IF NOT EXISTS idx_refresh_tokens_expires_at ON public.refresh_tokens (expires_at);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_authorization_codes_user_id
|
||||
ON public.authorization_codes(user_id);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_authorization_codes_client_id
|
||||
ON public.authorization_codes(client_id);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_refresh_tokens_user_id
|
||||
ON public.refresh_tokens(user_id);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_refresh_tokens_client_id
|
||||
ON public.refresh_tokens(client_id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS public.user_consents (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
user_id uuid NOT NULL REFERENCES public.users(id) ON DELETE CASCADE,
|
||||
client_id uuid NOT NULL REFERENCES public.oauth_clients(id) ON DELETE CASCADE,
|
||||
scopes jsonb NOT NULL,
|
||||
authorized_at timestamptz NOT NULL DEFAULT now(),
|
||||
last_used_at timestamptz,
|
||||
expires_at timestamptz,
|
||||
revoked_at timestamptz,
|
||||
revoked_by uuid REFERENCES public.users(id)
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS unique_active_consent
|
||||
ON public.user_consents (user_id, client_id)
|
||||
WHERE revoked_at IS NULL;
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_user_consents_user_id ON public.user_consents(user_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_user_consents_client_id ON public.user_consents(client_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_user_consents_revoked ON public.user_consents(revoked_at) WHERE revoked_at IS NOT NULL;
|
||||
Reference in New Issue
Block a user