feat: user and service sessions

This commit is contained in:
2025-06-08 22:59:24 +02:00
parent db99236501
commit be9d4f2a1b
2 changed files with 70 additions and 0 deletions

View File

@ -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

View File

@ -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