|
| 1 | +-- Fix all OIDC tables: reference merchants(id) instead of auth.users(id) |
| 2 | +-- The app uses a custom merchants table with its own JWT auth |
| 3 | + |
| 4 | +-- Drop dependent tables first (FK ordering) |
| 5 | +DROP TABLE IF EXISTS oauth_consents; |
| 6 | +DROP TABLE IF EXISTS oauth_refresh_tokens; |
| 7 | +DROP TABLE IF EXISTS oauth_authorization_codes; |
| 8 | +DROP TABLE IF EXISTS oauth_clients; |
| 9 | + |
| 10 | +-- Recreate oauth_clients |
| 11 | +CREATE TABLE oauth_clients ( |
| 12 | + id UUID PRIMARY KEY DEFAULT gen_random_uuid(), |
| 13 | + client_id TEXT UNIQUE NOT NULL DEFAULT 'cp_' || substr(md5(random()::text), 1, 24), |
| 14 | + client_secret TEXT NOT NULL, |
| 15 | + name TEXT NOT NULL, |
| 16 | + description TEXT, |
| 17 | + redirect_uris TEXT[] NOT NULL DEFAULT '{}', |
| 18 | + scopes TEXT[] NOT NULL DEFAULT '{openid,profile,email}', |
| 19 | + owner_id UUID REFERENCES merchants(id) ON DELETE CASCADE, |
| 20 | + is_active BOOLEAN NOT NULL DEFAULT true, |
| 21 | + created_at TIMESTAMPTZ DEFAULT NOW(), |
| 22 | + updated_at TIMESTAMPTZ DEFAULT NOW() |
| 23 | +); |
| 24 | + |
| 25 | +-- Authorization codes (short-lived, exchanged for tokens) |
| 26 | +CREATE TABLE oauth_authorization_codes ( |
| 27 | + id UUID PRIMARY KEY DEFAULT gen_random_uuid(), |
| 28 | + code TEXT UNIQUE NOT NULL, |
| 29 | + client_id TEXT NOT NULL REFERENCES oauth_clients(client_id) ON DELETE CASCADE, |
| 30 | + user_id UUID NOT NULL REFERENCES merchants(id) ON DELETE CASCADE, |
| 31 | + redirect_uri TEXT NOT NULL, |
| 32 | + scopes TEXT[] NOT NULL DEFAULT '{}', |
| 33 | + code_challenge TEXT, |
| 34 | + code_challenge_method TEXT DEFAULT 'S256', |
| 35 | + expires_at TIMESTAMPTZ NOT NULL, |
| 36 | + used BOOLEAN NOT NULL DEFAULT false, |
| 37 | + created_at TIMESTAMPTZ DEFAULT NOW() |
| 38 | +); |
| 39 | + |
| 40 | +-- Refresh tokens |
| 41 | +CREATE TABLE oauth_refresh_tokens ( |
| 42 | + id UUID PRIMARY KEY DEFAULT gen_random_uuid(), |
| 43 | + token TEXT UNIQUE NOT NULL, |
| 44 | + client_id TEXT NOT NULL REFERENCES oauth_clients(client_id) ON DELETE CASCADE, |
| 45 | + user_id UUID NOT NULL REFERENCES merchants(id) ON DELETE CASCADE, |
| 46 | + scopes TEXT[] NOT NULL DEFAULT '{}', |
| 47 | + expires_at TIMESTAMPTZ NOT NULL, |
| 48 | + revoked BOOLEAN NOT NULL DEFAULT false, |
| 49 | + created_at TIMESTAMPTZ DEFAULT NOW() |
| 50 | +); |
| 51 | + |
| 52 | +-- Consents (remember user's approval per client) |
| 53 | +CREATE TABLE oauth_consents ( |
| 54 | + id UUID PRIMARY KEY DEFAULT gen_random_uuid(), |
| 55 | + user_id UUID NOT NULL REFERENCES merchants(id) ON DELETE CASCADE, |
| 56 | + client_id TEXT NOT NULL REFERENCES oauth_clients(client_id) ON DELETE CASCADE, |
| 57 | + scopes TEXT[] NOT NULL DEFAULT '{}', |
| 58 | + created_at TIMESTAMPTZ DEFAULT NOW(), |
| 59 | + updated_at TIMESTAMPTZ DEFAULT NOW(), |
| 60 | + UNIQUE(user_id, client_id) |
| 61 | +); |
| 62 | + |
| 63 | +-- Re-enable RLS on all tables |
| 64 | +ALTER TABLE oauth_clients ENABLE ROW LEVEL SECURITY; |
| 65 | +ALTER TABLE oauth_authorization_codes ENABLE ROW LEVEL SECURITY; |
| 66 | +ALTER TABLE oauth_refresh_tokens ENABLE ROW LEVEL SECURITY; |
| 67 | +ALTER TABLE oauth_consents ENABLE ROW LEVEL SECURITY; |
| 68 | + |
| 69 | +-- Indexes |
| 70 | +CREATE INDEX idx_oauth_codes_client ON oauth_authorization_codes(client_id); |
| 71 | +CREATE INDEX idx_oauth_codes_user ON oauth_authorization_codes(user_id); |
| 72 | +CREATE INDEX idx_oauth_refresh_client ON oauth_refresh_tokens(client_id); |
| 73 | +CREATE INDEX idx_oauth_refresh_user ON oauth_refresh_tokens(user_id); |
| 74 | +CREATE INDEX idx_oauth_consents_user ON oauth_consents(user_id); |
0 commit comments