mirror of
https://github.com/bitechdev/ResolveSpec.git
synced 2026-10-01 12:31:59 +00:00
pkg/security no longer contains SQL. Every provider calls a store interface
from lookup, implemented by a procedure backend (Postgres stored procedures,
the default there) and a direct backend (dialect-driven SQL for postgres,
sqlite, mysql and mssql with configurable table and column names).
- add sectypes, lookup, lookup/{dialect,procedure,direct,backends,ddl,conformance}
- split totp and providers sub packages out of the core package
- replace SQLNames/TableNames/QueryMode with lookup.Config (see breaking_changes.md)
- direct backend now covers column/row security and API-key login
- move txsettings SQL to lookup.ApplyTxSettings; remove password.go
- move schema scripts under lookup/, add reference DDL per dialect
- add a shared conformance suite; run it on sqlite, and on Postgres in a
podman/docker container (RESOLVESPEC_TEST_CONTAINERS=1)
- fix procedure schema bugs found on real Postgres: duplicate p_data
parameter, JSON null arrays, expires_at timezone casts, passkey list
GROUP BY, missing resolvespec_passkey_login; accept zone-less timestamps
229 lines
6.8 KiB
SQL
229 lines
6.8 KiB
SQL
-- Reference schema for the lookup direct backend: SQLite. Direct backend only.
|
|
-- Table and column names are the lookup.DefaultSchema defaults, override them with lookup.Config.Schema.
|
|
-- Generated by the project, edit freely for your deployment (types, collations, extra columns).
|
|
|
|
-- password: bcrypt hash (nullable for OAuth2 users), legacy cleartext is accepted at login
|
|
-- roles: comma-separated roles
|
|
|
|
CREATE TABLE IF NOT EXISTS users (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
username VARCHAR(255) NOT NULL UNIQUE,
|
|
email VARCHAR(255) NOT NULL UNIQUE,
|
|
password VARCHAR(255),
|
|
user_level INTEGER DEFAULT 0,
|
|
roles VARCHAR(500),
|
|
is_active BOOLEAN DEFAULT 1,
|
|
created_at TIMESTAMP,
|
|
updated_at TIMESTAMP,
|
|
last_login_at TIMESTAMP,
|
|
program_user_id INTEGER DEFAULT 0,
|
|
program_user_table VARCHAR(255) DEFAULT '',
|
|
remote_id VARCHAR(255),
|
|
auth_provider VARCHAR(50),
|
|
totp_secret VARCHAR(255),
|
|
totp_enabled BOOLEAN DEFAULT 0,
|
|
totp_enabled_at TIMESTAMP
|
|
);
|
|
|
|
|
|
CREATE TABLE IF NOT EXISTS user_sessions (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
session_token VARCHAR(500) NOT NULL UNIQUE,
|
|
user_id INTEGER NOT NULL,
|
|
expires_at TIMESTAMP NOT NULL,
|
|
created_at TIMESTAMP,
|
|
last_activity_at TIMESTAMP,
|
|
ip_address VARCHAR(45),
|
|
user_agent TEXT,
|
|
access_token TEXT,
|
|
refresh_token TEXT,
|
|
token_type VARCHAR(50) DEFAULT 'Bearer',
|
|
auth_provider VARCHAR(50)
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_user_sessions_user_id ON user_sessions(user_id);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_user_sessions_expires_at ON user_sessions(expires_at);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_user_sessions_refresh_token ON user_sessions(refresh_token);
|
|
|
|
|
|
CREATE TABLE IF NOT EXISTS token_blacklist (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
token VARCHAR(500) NOT NULL,
|
|
user_id INTEGER,
|
|
expires_at TIMESTAMP NOT NULL,
|
|
created_at TIMESTAMP
|
|
);
|
|
|
|
|
|
-- code_hash: SHA-256 hex of the backup code
|
|
|
|
CREATE TABLE IF NOT EXISTS user_totp_backup_codes (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
user_id INTEGER NOT NULL,
|
|
code_hash VARCHAR(64) NOT NULL,
|
|
used BOOLEAN DEFAULT 0,
|
|
used_at TIMESTAMP,
|
|
created_at TIMESTAMP
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_totp_user_id ON user_totp_backup_codes(user_id);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_totp_code_hash ON user_totp_backup_codes(code_hash);
|
|
|
|
|
|
-- credential_id: base64 text
|
|
-- public_key: base64 text
|
|
-- aaguid: base64 text
|
|
-- transports: JSON-encoded array
|
|
|
|
CREATE TABLE IF NOT EXISTS user_passkey_credentials (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
user_id INTEGER NOT NULL,
|
|
credential_id TEXT NOT NULL UNIQUE,
|
|
public_key TEXT NOT NULL,
|
|
attestation_type VARCHAR(50) DEFAULT 'none',
|
|
aaguid TEXT,
|
|
sign_count INTEGER DEFAULT 0,
|
|
clone_warning BOOLEAN DEFAULT 0,
|
|
transports TEXT,
|
|
backup_eligible BOOLEAN DEFAULT 0,
|
|
backup_state BOOLEAN DEFAULT 0,
|
|
name VARCHAR(255),
|
|
created_at TIMESTAMP,
|
|
last_used_at TIMESTAMP
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_passkey_user_id ON user_passkey_credentials(user_id);
|
|
|
|
|
|
-- token_hash: SHA-256 hex of the raw token
|
|
|
|
CREATE TABLE IF NOT EXISTS user_password_resets (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
user_id INTEGER NOT NULL,
|
|
token_hash VARCHAR(64) NOT NULL UNIQUE,
|
|
expires_at TIMESTAMP NOT NULL,
|
|
created_at TIMESTAMP,
|
|
used BOOLEAN DEFAULT 0,
|
|
used_at TIMESTAMP
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_pw_reset_user_id ON user_password_resets(user_id);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_pw_reset_expires_at ON user_password_resets(expires_at);
|
|
|
|
|
|
-- redirect_uris: JSON-encoded array
|
|
-- grant_types: JSON-encoded array
|
|
-- allowed_scopes: JSON-encoded array
|
|
-- client_secret_hash: SHA-256 hex of the confidential-client secret, NULL for public clients
|
|
|
|
CREATE TABLE IF NOT EXISTS oauth_clients (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
client_id VARCHAR(255) NOT NULL UNIQUE,
|
|
redirect_uris TEXT NOT NULL,
|
|
client_name VARCHAR(255),
|
|
grant_types TEXT,
|
|
allowed_scopes TEXT,
|
|
client_secret_hash TEXT,
|
|
token_endpoint_auth_method VARCHAR(30) DEFAULT 'none',
|
|
is_active BOOLEAN DEFAULT 1,
|
|
created_at TIMESTAMP
|
|
);
|
|
|
|
|
|
-- scopes: JSON-encoded array
|
|
|
|
CREATE TABLE IF NOT EXISTS oauth_codes (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
code VARCHAR(255) NOT NULL UNIQUE,
|
|
client_id VARCHAR(255) NOT NULL,
|
|
redirect_uri TEXT NOT NULL,
|
|
client_state TEXT,
|
|
code_challenge VARCHAR(255) NOT NULL,
|
|
code_challenge_method VARCHAR(10) DEFAULT 'S256',
|
|
session_token TEXT NOT NULL,
|
|
refresh_token TEXT,
|
|
scopes TEXT,
|
|
expires_at TIMESTAMP NOT NULL,
|
|
created_at TIMESTAMP
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_oauth_codes_expires ON oauth_codes(expires_at);
|
|
|
|
|
|
-- key_hash: SHA-256 hex
|
|
-- scopes: JSON-encoded array
|
|
-- meta: JSON-encoded object
|
|
|
|
CREATE TABLE IF NOT EXISTS user_keys (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
user_id INTEGER NOT NULL,
|
|
key_type VARCHAR(50) NOT NULL,
|
|
key_hash VARCHAR(64) NOT NULL UNIQUE,
|
|
name VARCHAR(255) NOT NULL DEFAULT '',
|
|
scopes TEXT,
|
|
meta TEXT,
|
|
expires_at TIMESTAMP,
|
|
created_at TIMESTAMP,
|
|
last_used_at TIMESTAMP,
|
|
is_active BOOLEAN DEFAULT 1
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_user_keys_user_id ON user_keys(user_id);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_user_keys_key_type ON user_keys(key_type);
|
|
|
|
|
|
-- Optional: omit to use per-user rules only.
|
|
|
|
CREATE TABLE IF NOT EXISTS sec_group_members (
|
|
group_id INTEGER NOT NULL,
|
|
user_id INTEGER NOT NULL,
|
|
PRIMARY KEY (group_id, user_id)
|
|
);
|
|
|
|
|
|
-- column_path: dot path under the table: col or col.sub.field
|
|
-- access_type: mask, hide, read, ...
|
|
-- extra_filters: JSON object
|
|
|
|
CREATE TABLE IF NOT EXISTS sec_column_rules (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
user_id INTEGER,
|
|
group_id INTEGER,
|
|
schema_name TEXT NOT NULL,
|
|
table_name TEXT NOT NULL,
|
|
column_path TEXT NOT NULL,
|
|
access_type VARCHAR(50) NOT NULL,
|
|
mask_start INTEGER DEFAULT 0,
|
|
mask_end INTEGER DEFAULT 0,
|
|
mask_invert BOOLEAN DEFAULT 0,
|
|
mask_char VARCHAR(10) DEFAULT '*',
|
|
extra_filters TEXT,
|
|
is_active BOOLEAN NOT NULL DEFAULT 1,
|
|
CHECK ((user_id IS NULL AND group_id IS NOT NULL) OR (user_id IS NOT NULL AND group_id IS NULL))
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_sec_column_rules_table ON sec_column_rules(schema_name, table_name);
|
|
|
|
|
|
-- template: SQL fragment, e.g. user_id = {UserID}
|
|
|
|
CREATE TABLE IF NOT EXISTS sec_row_rules (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
user_id INTEGER,
|
|
group_id INTEGER,
|
|
schema_name TEXT NOT NULL,
|
|
table_name TEXT NOT NULL,
|
|
template TEXT,
|
|
has_block BOOLEAN NOT NULL DEFAULT 0,
|
|
is_active BOOLEAN NOT NULL DEFAULT 1,
|
|
CHECK ((user_id IS NULL AND group_id IS NOT NULL) OR (user_id IS NOT NULL AND group_id IS NULL))
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_sec_row_rules_table ON sec_row_rules(schema_name, table_name);
|
|
|