feat: user and service sessions
This commit is contained in:
33
migrations/00011_add_user_sessions_table.sql
Normal file
33
migrations/00011_add_user_sessions_table.sql
Normal 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
|
37
migrations/00012_add_service_sessions.sql
Normal file
37
migrations/00012_add_service_sessions.sql
Normal 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
|
Reference in New Issue
Block a user