@accelerate/authorization-postgres 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +28 -2
- package/admin.sql +11 -0
- package/dist/admin.d.ts +37 -0
- package/dist/admin.js +90 -0
- package/dist/admin.test.d.ts +1 -0
- package/dist/admin.test.js +45 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +40 -0
- package/dist/index.test.d.ts +1 -0
- package/dist/index.test.js +20 -0
- package/package.json +44 -4
- package/schema.sql +18 -0
package/README.md
CHANGED
|
@@ -1,3 +1,29 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @accelerate/authorization-postgres
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
PostgreSQL storage adapter for `@accelerate/authorization-core`.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { Pool } from "pg";
|
|
7
|
+
import { PostgresAuthorizationStore } from "@accelerate/authorization-postgres";
|
|
8
|
+
const pool = new Pool({ connectionString: process.env.DATABASE_URL });
|
|
9
|
+
const store = new PostgresAuthorizationStore(pool);
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
Apply `schema.sql` before using the adapter. The caller owns the pool lifecycle. The adapter uses parameterized queries and idempotent grants and assignments. Do not expose mutation methods directly to untrusted clients. This is a single-tenant baseline: tenant isolation, audit logging, migration versioning, and transactional administrative operations remain future work.
|
|
13
|
+
|
|
14
|
+
## Audited administrative operations
|
|
15
|
+
|
|
16
|
+
Apply `admin.sql` after `schema.sql`. Import `PostgresAuthorizationAdmin` from `@accelerate/authorization-postgres/admin`. Supply a server-controlled `AdminPolicy` that validates each administrative action against the authenticated actor. The actor ID must come from trusted authentication middleware, not the request body.
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { PostgresAuthorizationAdmin } from "@accelerate/authorization-postgres/admin";
|
|
20
|
+
const admin = new PostgresAuthorizationAdmin(pool, permissions, {
|
|
21
|
+
authorize: async (context, action) => {
|
|
22
|
+
// Implement your application's privileged server-side policy.
|
|
23
|
+
return await isAuthorizedAdministrator(context.actorId, action);
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
await admin.grant({ actorId: authenticatedActorId }, roleId, "documents.read");
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Administrative mutations and audit inserts share one PostgreSQL transaction. The lower-level `PostgresAuthorizationStore` still exposes unaudited mutation methods for compatibility; production administrative routes should use the audited service instead. This does not yet implement multi-tenant isolation, a separately scoped audit-read permission, or tamper-resistant external audit storage.
|
package/admin.sql
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
-- Additive migration for audited administrative mutations.
|
|
2
|
+
CREATE TABLE IF NOT EXISTS authorization_audit_events (
|
|
3
|
+
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
|
4
|
+
occurred_at timestamptz NOT NULL DEFAULT now(),
|
|
5
|
+
actor_id text NOT NULL,
|
|
6
|
+
action text NOT NULL CHECK (action IN ('role.create', 'role.activate', 'role.deactivate', 'permission.grant', 'permission.revoke', 'role.assign', 'role.unassign')),
|
|
7
|
+
target_id text NOT NULL,
|
|
8
|
+
details jsonb NOT NULL DEFAULT '{}'::jsonb
|
|
9
|
+
);
|
|
10
|
+
CREATE INDEX IF NOT EXISTS authorization_audit_events_occurred_at_idx ON authorization_audit_events (occurred_at DESC);
|
|
11
|
+
CREATE INDEX IF NOT EXISTS authorization_audit_events_actor_idx ON authorization_audit_events (actor_id, occurred_at DESC);
|
package/dist/admin.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { Pool } from "pg";
|
|
2
|
+
import type { PermissionRegistry, Role } from "@accelerate/authorization-core";
|
|
3
|
+
export type AdminAction = "role.create" | "role.activate" | "role.deactivate" | "permission.grant" | "permission.revoke" | "role.assign" | "role.unassign";
|
|
4
|
+
export interface AdminContext {
|
|
5
|
+
/** Identity resolved by trusted server-side authentication, never from request input. */
|
|
6
|
+
actorId: string;
|
|
7
|
+
}
|
|
8
|
+
export interface AdminPolicy {
|
|
9
|
+
/** Server-owned policy; deny by default. */
|
|
10
|
+
authorize(context: AdminContext, action: AdminAction): Promise<boolean>;
|
|
11
|
+
}
|
|
12
|
+
export interface AuditEvent {
|
|
13
|
+
id: string;
|
|
14
|
+
occurredAt: Date;
|
|
15
|
+
actorId: string;
|
|
16
|
+
action: AdminAction;
|
|
17
|
+
targetId: string;
|
|
18
|
+
details: Record<string, string | boolean>;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Transactional administrative mutations and append-only audit records.
|
|
22
|
+
* Run schema.sql and admin.sql before use. Application owns the pool lifecycle.
|
|
23
|
+
*/
|
|
24
|
+
export declare class PostgresAuthorizationAdmin {
|
|
25
|
+
private readonly pool;
|
|
26
|
+
private readonly policy;
|
|
27
|
+
private readonly known;
|
|
28
|
+
constructor(pool: Pick<Pool, "connect">, registry: PermissionRegistry, policy: AdminPolicy);
|
|
29
|
+
private mutate;
|
|
30
|
+
createRole(context: AdminContext, role: Role): Promise<void>;
|
|
31
|
+
setRoleActive(context: AdminContext, roleId: string, active: boolean): Promise<void>;
|
|
32
|
+
grant(context: AdminContext, roleId: string, permission: string): Promise<void>;
|
|
33
|
+
revoke(context: AdminContext, roleId: string, permission: string): Promise<void>;
|
|
34
|
+
assign(context: AdminContext, userId: string, roleId: string): Promise<void>;
|
|
35
|
+
unassign(context: AdminContext, userId: string, roleId: string): Promise<void>;
|
|
36
|
+
listAuditEvents(context: AdminContext, limit?: number): Promise<readonly AuditEvent[]>;
|
|
37
|
+
}
|
package/dist/admin.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Transactional administrative mutations and append-only audit records.
|
|
3
|
+
* Run schema.sql and admin.sql before use. Application owns the pool lifecycle.
|
|
4
|
+
*/
|
|
5
|
+
export class PostgresAuthorizationAdmin {
|
|
6
|
+
constructor(pool, registry, policy) {
|
|
7
|
+
this.pool = pool;
|
|
8
|
+
this.policy = policy;
|
|
9
|
+
this.known = new Set(registry.keys);
|
|
10
|
+
}
|
|
11
|
+
async mutate(context, action, targetId, details, apply) {
|
|
12
|
+
if (!context.actorId?.trim())
|
|
13
|
+
throw new Error("Authenticated actor is required");
|
|
14
|
+
if (!await this.policy.authorize(context, action))
|
|
15
|
+
throw new Error("Forbidden");
|
|
16
|
+
const client = await this.pool.connect();
|
|
17
|
+
try {
|
|
18
|
+
await client.query("BEGIN");
|
|
19
|
+
await apply(client);
|
|
20
|
+
await client.query("INSERT INTO authorization_audit_events (actor_id, action, target_id, details) VALUES ($1, $2, $3, $4::jsonb)", [context.actorId, action, targetId, JSON.stringify(details)]);
|
|
21
|
+
await client.query("COMMIT");
|
|
22
|
+
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
try {
|
|
25
|
+
await client.query("ROLLBACK");
|
|
26
|
+
}
|
|
27
|
+
catch { /* preserve original failure */ }
|
|
28
|
+
throw error;
|
|
29
|
+
}
|
|
30
|
+
finally {
|
|
31
|
+
client.release();
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
async createRole(context, role) {
|
|
35
|
+
await this.mutate(context, "role.create", role.id, { key: role.key, name: role.name, active: role.active }, async (client) => {
|
|
36
|
+
await client.query("INSERT INTO authorization_roles (id, role_key, name, active) VALUES ($1, $2, $3, $4)", [role.id, role.key, role.name, role.active]);
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
async setRoleActive(context, roleId, active) {
|
|
40
|
+
await this.mutate(context, active ? "role.activate" : "role.deactivate", roleId, { active }, async (client) => {
|
|
41
|
+
const result = await client.query("UPDATE authorization_roles SET active = $2 WHERE id = $1", [roleId, active]);
|
|
42
|
+
if (!result.rowCount)
|
|
43
|
+
throw new Error("Unknown role");
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
async grant(context, roleId, permission) {
|
|
47
|
+
if (!this.known.has(permission))
|
|
48
|
+
throw new Error("Unregistered permission");
|
|
49
|
+
await this.mutate(context, "permission.grant", roleId, { permission }, async (client) => {
|
|
50
|
+
await client.query("INSERT INTO authorization_role_permissions (role_id, permission_key) VALUES ($1, $2) ON CONFLICT DO NOTHING", [roleId, permission]);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
async revoke(context, roleId, permission) {
|
|
54
|
+
if (!this.known.has(permission))
|
|
55
|
+
throw new Error("Unregistered permission");
|
|
56
|
+
await this.mutate(context, "permission.revoke", roleId, { permission }, async (client) => {
|
|
57
|
+
await client.query("DELETE FROM authorization_role_permissions WHERE role_id = $1 AND permission_key = $2", [roleId, permission]);
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
async assign(context, userId, roleId) {
|
|
61
|
+
await this.mutate(context, "role.assign", userId, { roleId }, async (client) => {
|
|
62
|
+
await client.query("INSERT INTO authorization_user_roles (user_id, role_id) VALUES ($1, $2) ON CONFLICT DO NOTHING", [userId, roleId]);
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
async unassign(context, userId, roleId) {
|
|
66
|
+
await this.mutate(context, "role.unassign", userId, { roleId }, async (client) => {
|
|
67
|
+
await client.query("DELETE FROM authorization_user_roles WHERE user_id = $1 AND role_id = $2", [userId, roleId]);
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
async listAuditEvents(context, limit = 100) {
|
|
71
|
+
if (!context.actorId?.trim() || !await this.policy.authorize(context, "role.create"))
|
|
72
|
+
throw new Error("Forbidden");
|
|
73
|
+
const size = Math.max(1, Math.min(500, Math.trunc(limit) || 100));
|
|
74
|
+
const client = await this.pool.connect();
|
|
75
|
+
try {
|
|
76
|
+
const result = await client.query("SELECT id::text, occurred_at, actor_id, action, target_id, details FROM authorization_audit_events ORDER BY id DESC LIMIT $1", [size]);
|
|
77
|
+
return result.rows.map(row => ({
|
|
78
|
+
id: row.id,
|
|
79
|
+
occurredAt: row.occurred_at,
|
|
80
|
+
actorId: row.actor_id,
|
|
81
|
+
action: row.action,
|
|
82
|
+
targetId: row.target_id,
|
|
83
|
+
details: row.details
|
|
84
|
+
}));
|
|
85
|
+
}
|
|
86
|
+
finally {
|
|
87
|
+
client.release();
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { PostgresAuthorizationAdmin } from "./admin.js";
|
|
3
|
+
import { definePermissions } from "@accelerate/authorization-core";
|
|
4
|
+
const permissions = definePermissions({ documents: { read: "Read documents" } });
|
|
5
|
+
function setup(allowed, failAudit = false) {
|
|
6
|
+
const query = vi.fn(async (sql) => {
|
|
7
|
+
if (failAudit && sql.startsWith("INSERT INTO authorization_audit_events"))
|
|
8
|
+
throw new Error("Audit unavailable");
|
|
9
|
+
return { rowCount: 1, rows: [] };
|
|
10
|
+
});
|
|
11
|
+
const release = vi.fn();
|
|
12
|
+
const connect = vi.fn(async () => ({ query, release }));
|
|
13
|
+
const authorize = vi.fn(async () => allowed);
|
|
14
|
+
return { admin: new PostgresAuthorizationAdmin({ connect }, permissions, { authorize }), query, release, connect, authorize };
|
|
15
|
+
}
|
|
16
|
+
describe("PostgresAuthorizationAdmin", () => {
|
|
17
|
+
it("denies mutations before opening a connection", async () => {
|
|
18
|
+
const { admin, connect } = setup(false);
|
|
19
|
+
await expect(admin.grant({ actorId: "actor" }, "r", "documents.read")).rejects.toThrow("Forbidden");
|
|
20
|
+
expect(connect).not.toHaveBeenCalled();
|
|
21
|
+
});
|
|
22
|
+
it("commits mutation and audit in one transaction", async () => {
|
|
23
|
+
const { admin, query, release } = setup(true);
|
|
24
|
+
await admin.assign({ actorId: "actor" }, "user", "r");
|
|
25
|
+
expect(query.mock.calls.map(([sql]) => sql)).toEqual([
|
|
26
|
+
"BEGIN",
|
|
27
|
+
expect.stringContaining("INSERT INTO authorization_user_roles"),
|
|
28
|
+
expect.stringContaining("INSERT INTO authorization_audit_events"),
|
|
29
|
+
"COMMIT"
|
|
30
|
+
]);
|
|
31
|
+
expect(release).toHaveBeenCalledOnce();
|
|
32
|
+
});
|
|
33
|
+
it("rolls back when audit insertion fails", async () => {
|
|
34
|
+
const { admin, query, release } = setup(true, true);
|
|
35
|
+
await expect(admin.grant({ actorId: "actor" }, "r", "documents.read")).rejects.toThrow("Audit unavailable");
|
|
36
|
+
expect(query.mock.calls.map(([sql]) => sql)).toContain("ROLLBACK");
|
|
37
|
+
expect(query.mock.calls.map(([sql]) => sql)).not.toContain("COMMIT");
|
|
38
|
+
expect(release).toHaveBeenCalledOnce();
|
|
39
|
+
});
|
|
40
|
+
it("rejects unregistered permissions before a connection", async () => {
|
|
41
|
+
const { admin, connect } = setup(true);
|
|
42
|
+
await expect(admin.grant({ actorId: "actor" }, "r", "documents.write")).rejects.toThrow("Unregistered permission");
|
|
43
|
+
expect(connect).not.toHaveBeenCalled();
|
|
44
|
+
});
|
|
45
|
+
});
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Pool } from "pg";
|
|
2
|
+
import type { AuthorizationStore, Role } from "@accelerate/authorization-core";
|
|
3
|
+
/**
|
|
4
|
+
* A PostgreSQL-backed store. Run schema.sql before use.
|
|
5
|
+
* Pass an existing pg Pool; the calling application owns its lifecycle.
|
|
6
|
+
* Administrative access control must be enforced before calling mutating methods.
|
|
7
|
+
*/
|
|
8
|
+
export declare class PostgresAuthorizationStore implements AuthorizationStore {
|
|
9
|
+
private readonly pool;
|
|
10
|
+
constructor(pool: Pick<Pool, "query">);
|
|
11
|
+
createRole(role: Role): Promise<void>;
|
|
12
|
+
getRole(id: string): Promise<Role | undefined>;
|
|
13
|
+
getRolesForUser(userId: string): Promise<readonly Role[]>;
|
|
14
|
+
getPermissionKeysForRole(roleId: string): Promise<readonly string[]>;
|
|
15
|
+
grant(roleId: string, permission: string): Promise<void>;
|
|
16
|
+
revoke(roleId: string, permission: string): Promise<void>;
|
|
17
|
+
assign(userId: string, roleId: string): Promise<void>;
|
|
18
|
+
unassign(userId: string, roleId: string): Promise<void>;
|
|
19
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A PostgreSQL-backed store. Run schema.sql before use.
|
|
3
|
+
* Pass an existing pg Pool; the calling application owns its lifecycle.
|
|
4
|
+
* Administrative access control must be enforced before calling mutating methods.
|
|
5
|
+
*/
|
|
6
|
+
export class PostgresAuthorizationStore {
|
|
7
|
+
constructor(pool) {
|
|
8
|
+
this.pool = pool;
|
|
9
|
+
}
|
|
10
|
+
async createRole(role) {
|
|
11
|
+
await this.pool.query("INSERT INTO authorization_roles (id, role_key, name, active) VALUES ($1, $2, $3, $4)", [role.id, role.key, role.name, role.active]);
|
|
12
|
+
}
|
|
13
|
+
async getRole(id) {
|
|
14
|
+
const result = await this.pool.query("SELECT id, role_key AS key, name, active FROM authorization_roles WHERE id = $1", [id]);
|
|
15
|
+
return result.rows[0];
|
|
16
|
+
}
|
|
17
|
+
async getRolesForUser(userId) {
|
|
18
|
+
const result = await this.pool.query(`SELECT r.id, r.role_key AS key, r.name, r.active
|
|
19
|
+
FROM authorization_roles r
|
|
20
|
+
JOIN authorization_user_roles ur ON ur.role_id = r.id
|
|
21
|
+
WHERE ur.user_id = $1`, [userId]);
|
|
22
|
+
return result.rows;
|
|
23
|
+
}
|
|
24
|
+
async getPermissionKeysForRole(roleId) {
|
|
25
|
+
const result = await this.pool.query("SELECT permission_key FROM authorization_role_permissions WHERE role_id = $1", [roleId]);
|
|
26
|
+
return result.rows.map((row) => row.permission_key);
|
|
27
|
+
}
|
|
28
|
+
async grant(roleId, permission) {
|
|
29
|
+
await this.pool.query("INSERT INTO authorization_role_permissions (role_id, permission_key) VALUES ($1, $2) ON CONFLICT DO NOTHING", [roleId, permission]);
|
|
30
|
+
}
|
|
31
|
+
async revoke(roleId, permission) {
|
|
32
|
+
await this.pool.query("DELETE FROM authorization_role_permissions WHERE role_id = $1 AND permission_key = $2", [roleId, permission]);
|
|
33
|
+
}
|
|
34
|
+
async assign(userId, roleId) {
|
|
35
|
+
await this.pool.query("INSERT INTO authorization_user_roles (user_id, role_id) VALUES ($1, $2) ON CONFLICT DO NOTHING", [userId, roleId]);
|
|
36
|
+
}
|
|
37
|
+
async unassign(userId, roleId) {
|
|
38
|
+
await this.pool.query("DELETE FROM authorization_user_roles WHERE user_id = $1 AND role_id = $2", [userId, roleId]);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { PostgresAuthorizationStore } from "./index.js";
|
|
3
|
+
describe("PostgresAuthorizationStore", () => {
|
|
4
|
+
it("uses parameterized SQL when assigning a user", async () => {
|
|
5
|
+
const query = vi.fn().mockResolvedValue({ rows: [] });
|
|
6
|
+
const store = new PostgresAuthorizationStore({ query });
|
|
7
|
+
await store.assign("user'; DROP TABLE users; --", "reader");
|
|
8
|
+
expect(query).toHaveBeenCalledOnce();
|
|
9
|
+
expect(query.mock.calls[0][0]).toContain("VALUES ($1, $2)");
|
|
10
|
+
expect(query.mock.calls[0][1]).toEqual(["user'; DROP TABLE users; --", "reader"]);
|
|
11
|
+
});
|
|
12
|
+
it("maps roles and permissions from query results", async () => {
|
|
13
|
+
const query = vi.fn()
|
|
14
|
+
.mockResolvedValueOnce({ rows: [{ id: "r", key: "reader", name: "Reader", active: true }] })
|
|
15
|
+
.mockResolvedValueOnce({ rows: [{ permission_key: "documents.read" }] });
|
|
16
|
+
const store = new PostgresAuthorizationStore({ query });
|
|
17
|
+
expect(await store.getRolesForUser("user")).toEqual([{ id: "r", key: "reader", name: "Reader", active: true }]);
|
|
18
|
+
expect(await store.getPermissionKeysForRole("r")).toEqual(["documents.read"]);
|
|
19
|
+
});
|
|
20
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,46 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@accelerate/authorization-postgres",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"sideEffects": false,
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./admin": {
|
|
14
|
+
"types": "./dist/admin.d.ts",
|
|
15
|
+
"default": "./dist/admin.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist",
|
|
20
|
+
"schema.sql",
|
|
21
|
+
"admin.sql"
|
|
22
|
+
],
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"@accelerate/authorization-core": "^0.1.1",
|
|
25
|
+
"pg": "^8.11.0"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@accelerate/authorization-core": "workspace:*",
|
|
29
|
+
"@types/pg": "^8.11.11",
|
|
30
|
+
"pg": "^8.13.1"
|
|
31
|
+
},
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "tsc -p tsconfig.json",
|
|
34
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
35
|
+
"test": "vitest run"
|
|
36
|
+
},
|
|
37
|
+
"repository": {
|
|
38
|
+
"type": "git",
|
|
39
|
+
"url": "git+https://github.com/archubbuck/devkit.git",
|
|
40
|
+
"directory": "packages/authorization-postgres"
|
|
41
|
+
},
|
|
42
|
+
"publishConfig": {
|
|
43
|
+
"access": "public",
|
|
44
|
+
"registry": "https://registry.npmjs.org/"
|
|
45
|
+
}
|
|
46
|
+
}
|
package/schema.sql
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
-- Apply this migration before using PostgresAuthorizationStore.
|
|
2
|
+
CREATE TABLE IF NOT EXISTS authorization_roles (
|
|
3
|
+
id text PRIMARY KEY,
|
|
4
|
+
role_key text NOT NULL UNIQUE,
|
|
5
|
+
name text NOT NULL,
|
|
6
|
+
active boolean NOT NULL DEFAULT true
|
|
7
|
+
);
|
|
8
|
+
CREATE TABLE IF NOT EXISTS authorization_role_permissions (
|
|
9
|
+
role_id text NOT NULL REFERENCES authorization_roles(id) ON DELETE CASCADE,
|
|
10
|
+
permission_key text NOT NULL,
|
|
11
|
+
PRIMARY KEY (role_id, permission_key)
|
|
12
|
+
);
|
|
13
|
+
CREATE TABLE IF NOT EXISTS authorization_user_roles (
|
|
14
|
+
user_id text NOT NULL,
|
|
15
|
+
role_id text NOT NULL REFERENCES authorization_roles(id) ON DELETE CASCADE,
|
|
16
|
+
PRIMARY KEY (user_id, role_id)
|
|
17
|
+
);
|
|
18
|
+
CREATE INDEX IF NOT EXISTS authorization_user_roles_role_idx ON authorization_user_roles(role_id);
|