From be9d4f2a1b09289ab13cc7cad408b27d2e07cea8 Mon Sep 17 00:00:00 2001 From: LandaMm Date: Sun, 8 Jun 2025 22:59:24 +0200 Subject: [PATCH] feat: user and service sessions --- migrations/00011_add_user_sessions_table.sql | 33 +++++++++++++++++ migrations/00012_add_service_sessions.sql | 37 ++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 migrations/00011_add_user_sessions_table.sql create mode 100644 migrations/00012_add_service_sessions.sql diff --git a/migrations/00011_add_user_sessions_table.sql b/migrations/00011_add_user_sessions_table.sql new file mode 100644 index 0000000..fcb45e4 --- /dev/null +++ b/migrations/00011_add_user_sessions_table.sql @@ -0,0 +1,33 @@ +-- +goose Up +-- +goose StatementBegin +CREATE TABLE user_sessions ( + id UUID PRIMARY KEY DEFAULT gen_random_uuid (), + user_id UUID REFERENCES users (id) NOT NULL, + session_type VARCHAR(32) NOT NULL DEFAULT 'user', -- e.g. 'user', 'admin' + issued_at TIMESTAMP + WITH + TIME ZONE NOT NULL DEFAULT NOW (), + expires_at TIMESTAMP + WITH + TIME ZONE, + last_active TIMESTAMP + WITH + TIME ZONE, + ip_address VARCHAR(45), -- supports IPv4/IPv6 + user_agent TEXT, + refresh_token TEXT, + device_info JSONB, -- optional: structured info (browser, OS, etc.) + is_active BOOLEAN NOT NULL DEFAULT TRUE, + revoked_at TIMESTAMP + WITH + TIME ZONE +); + +CREATE INDEX IF NOT EXISTS idx_user_sessions_user_id ON user_sessions (user_id); + +-- +goose StatementEnd +-- +goose Down +-- +goose StatementBegin +DROP TABLE IF EXISTS user_sessions; + +-- +goose StatementEnd diff --git a/migrations/00012_add_service_sessions.sql b/migrations/00012_add_service_sessions.sql new file mode 100644 index 0000000..e2144de --- /dev/null +++ b/migrations/00012_add_service_sessions.sql @@ -0,0 +1,37 @@ +-- +goose Up +-- +goose StatementBegin +CREATE TABLE service_sessions ( + id UUID PRIMARY KEY DEFAULT gen_random_uuid (), + service_id UUID REFERENCES api_services (id) NOT NULL, + client_id TEXT NOT NULL, + user_id UUID REFERENCES users (id), -- user on behalf of whom the service is acting, nullable for direct use with client creds + issued_at TIMESTAMP + WITH + TIME ZONE NOT NULL DEFAULT NOW (), + expires_at TIMESTAMP + WITH + TIME ZONE, + last_active TIMESTAMP + WITH + TIME ZONE, + ip_address VARCHAR(45), + user_agent TEXT, + refresh_token TEXT, + is_active BOOLEAN NOT NULL DEFAULT TRUE, + revoked_at TIMESTAMP + WITH + TIME ZONE, + scope TEXT, -- what scopes/permissions this session was issued for + claims JSONB -- snapshot of claims at session start, optional +); + +CREATE INDEX IF NOT EXISTS idx_service_sessions_client_id ON service_sessions (client_id); + +CREATE INDEX IF NOT EXISTS idx_service_sessions_user_id ON service_sessions (user_id); + +-- +goose StatementEnd +-- +goose Down +-- +goose StatementBegin +DROP TABLE IF EXISTS service_sessions; + +-- +goose StatementEnd