@open-core/identity 1.0.0
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/LICENSE +7 -0
- package/README.md +682 -0
- package/dist/entities/account.entity.d.ts +34 -0
- package/dist/entities/account.entity.js +2 -0
- package/dist/entities/role.entity.d.ts +35 -0
- package/dist/entities/role.entity.js +2 -0
- package/dist/events/identity.events.d.ts +24 -0
- package/dist/events/identity.events.js +2 -0
- package/dist/index.d.ts +70 -0
- package/dist/index.js +100 -0
- package/dist/repositories/account.repository.d.ts +60 -0
- package/dist/repositories/account.repository.js +185 -0
- package/dist/repositories/role.repository.d.ts +50 -0
- package/dist/repositories/role.repository.js +79 -0
- package/dist/services/account.service.d.ts +78 -0
- package/dist/services/account.service.js +207 -0
- package/dist/services/auth/api-auth.provider.d.ts +30 -0
- package/dist/services/auth/api-auth.provider.js +134 -0
- package/dist/services/auth/credentials-auth.provider.d.ts +27 -0
- package/dist/services/auth/credentials-auth.provider.js +214 -0
- package/dist/services/auth/local-auth.provider.d.ts +28 -0
- package/dist/services/auth/local-auth.provider.js +135 -0
- package/dist/services/cache/memory-cache.service.d.ts +47 -0
- package/dist/services/cache/memory-cache.service.js +108 -0
- package/dist/services/identity-auth.provider.d.ts +18 -0
- package/dist/services/identity-auth.provider.js +125 -0
- package/dist/services/identity-principal.provider.d.ts +29 -0
- package/dist/services/identity-principal.provider.js +104 -0
- package/dist/services/principal/api-principal.provider.d.ts +27 -0
- package/dist/services/principal/api-principal.provider.js +141 -0
- package/dist/services/principal/local-principal.provider.d.ts +39 -0
- package/dist/services/principal/local-principal.provider.js +114 -0
- package/dist/services/role.service.d.ts +73 -0
- package/dist/services/role.service.js +145 -0
- package/dist/setup.d.ts +58 -0
- package/dist/setup.js +93 -0
- package/dist/types/auth.types.d.ts +48 -0
- package/dist/types/auth.types.js +2 -0
- package/dist/types/index.d.ts +36 -0
- package/dist/types/index.js +2 -0
- package/migrations/001_accounts_table.sql +16 -0
- package/migrations/002_roles_table.sql +21 -0
- package/migrations/003_alter_accounts_add_role.sql +24 -0
- package/migrations/004_rename_uuid_to_linked_id.sql +12 -0
- package/migrations/005_add_password_hash.sql +7 -0
- package/package.json +59 -0
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration for API-based authentication
|
|
3
|
+
*/
|
|
4
|
+
export interface ApiAuthConfig {
|
|
5
|
+
/** Base URL for auth API */
|
|
6
|
+
authUrl: string;
|
|
7
|
+
/** Base URL for principal/permissions API */
|
|
8
|
+
principalUrl?: string;
|
|
9
|
+
/** Custom headers to include in all requests */
|
|
10
|
+
headers?: Record<string, string>;
|
|
11
|
+
/** Request timeout in milliseconds (default: 5000) */
|
|
12
|
+
timeoutMs?: number;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Response from API authentication endpoint
|
|
16
|
+
*/
|
|
17
|
+
export interface ApiAuthResponse {
|
|
18
|
+
/** Whether authentication was successful */
|
|
19
|
+
success: boolean;
|
|
20
|
+
/** Linked ID from external system */
|
|
21
|
+
linkedId?: string;
|
|
22
|
+
/** Error message if authentication failed */
|
|
23
|
+
error?: string;
|
|
24
|
+
/** Whether this is a new account */
|
|
25
|
+
isNewAccount?: boolean;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Response from API principal/permissions endpoint
|
|
29
|
+
*/
|
|
30
|
+
export interface ApiPrincipalResponse {
|
|
31
|
+
/** Display name of the role/principal */
|
|
32
|
+
name?: string;
|
|
33
|
+
/** Rank/weight for hierarchical checks */
|
|
34
|
+
rank?: number;
|
|
35
|
+
/** Array of permission strings */
|
|
36
|
+
permissions: string[];
|
|
37
|
+
/** Additional metadata */
|
|
38
|
+
meta?: Record<string, unknown>;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Cache configuration options
|
|
42
|
+
*/
|
|
43
|
+
export interface CacheOptions {
|
|
44
|
+
/** TTL in milliseconds (default: 300000 = 5 min) */
|
|
45
|
+
ttl?: number;
|
|
46
|
+
/** Maximum number of entries to cache */
|
|
47
|
+
maxEntries?: number;
|
|
48
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export type IdentifierType = "license" | "discord" | "steam";
|
|
2
|
+
export interface AccountIdentifiers {
|
|
3
|
+
license?: string | null;
|
|
4
|
+
discord?: string | null;
|
|
5
|
+
steam?: string | null;
|
|
6
|
+
}
|
|
7
|
+
export interface CreateAccountInput extends AccountIdentifiers {
|
|
8
|
+
linkedId?: string | null;
|
|
9
|
+
externalSource?: string;
|
|
10
|
+
username?: string | null;
|
|
11
|
+
roleId?: number | null;
|
|
12
|
+
}
|
|
13
|
+
export interface BanOptions {
|
|
14
|
+
/**
|
|
15
|
+
* Ban duration in milliseconds. When omitted, the ban is permanent.
|
|
16
|
+
*/
|
|
17
|
+
durationMs?: number;
|
|
18
|
+
reason?: string;
|
|
19
|
+
}
|
|
20
|
+
export interface AuthSession {
|
|
21
|
+
accountId: string;
|
|
22
|
+
isNew: boolean;
|
|
23
|
+
}
|
|
24
|
+
export interface CreateRoleInput {
|
|
25
|
+
name: string;
|
|
26
|
+
displayName: string;
|
|
27
|
+
rank: number;
|
|
28
|
+
permissions?: string[];
|
|
29
|
+
isDefault?: boolean;
|
|
30
|
+
}
|
|
31
|
+
export interface UpdateRoleInput {
|
|
32
|
+
displayName?: string;
|
|
33
|
+
rank?: number;
|
|
34
|
+
permissions?: string[];
|
|
35
|
+
isDefault?: boolean;
|
|
36
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
CREATE TABLE IF NOT EXISTS accounts (
|
|
2
|
+
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
3
|
+
linked_id VARCHAR(255) UNIQUE NULL,
|
|
4
|
+
external_source VARCHAR(32) NULL,
|
|
5
|
+
license VARCHAR(64) UNIQUE,
|
|
6
|
+
discord VARCHAR(32) UNIQUE,
|
|
7
|
+
steam VARCHAR(32) UNIQUE,
|
|
8
|
+
username VARCHAR(64),
|
|
9
|
+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
10
|
+
last_login_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
11
|
+
banned BOOLEAN DEFAULT FALSE,
|
|
12
|
+
ban_reason TEXT,
|
|
13
|
+
ban_expires TIMESTAMP NULL,
|
|
14
|
+
permissions JSON DEFAULT '[]'
|
|
15
|
+
);
|
|
16
|
+
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
-- Migration 002: Create roles table
|
|
2
|
+
-- This table stores security roles/ranks with hierarchical permissions.
|
|
3
|
+
|
|
4
|
+
CREATE TABLE IF NOT EXISTS roles (
|
|
5
|
+
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
6
|
+
name VARCHAR(64) NOT NULL UNIQUE,
|
|
7
|
+
display_name VARCHAR(128) NOT NULL,
|
|
8
|
+
rank INT NOT NULL DEFAULT 0,
|
|
9
|
+
permissions JSON DEFAULT '[]',
|
|
10
|
+
is_default BOOLEAN DEFAULT FALSE,
|
|
11
|
+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
12
|
+
|
|
13
|
+
INDEX idx_name (name),
|
|
14
|
+
INDEX idx_is_default (is_default)
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
-- Insert default role for new accounts
|
|
18
|
+
INSERT INTO roles (name, display_name, rank, permissions, is_default)
|
|
19
|
+
VALUES ('user', 'Player', 0, '[]', TRUE)
|
|
20
|
+
ON DUPLICATE KEY UPDATE name=name;
|
|
21
|
+
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
-- Migration 003: Alter accounts table to use role-based permissions
|
|
2
|
+
-- IMPORTANT: Run this AFTER creating roles and inserting at least a default role.
|
|
3
|
+
|
|
4
|
+
-- Add role_id foreign key column
|
|
5
|
+
ALTER TABLE accounts
|
|
6
|
+
ADD COLUMN role_id INT NULL AFTER username;
|
|
7
|
+
|
|
8
|
+
-- Add custom_permissions column for per-account overrides
|
|
9
|
+
ALTER TABLE accounts
|
|
10
|
+
ADD COLUMN custom_permissions JSON DEFAULT '[]' AFTER role_id;
|
|
11
|
+
|
|
12
|
+
-- Drop old flat permissions column
|
|
13
|
+
ALTER TABLE accounts
|
|
14
|
+
DROP COLUMN IF EXISTS permissions;
|
|
15
|
+
|
|
16
|
+
-- Add foreign key constraint to roles
|
|
17
|
+
ALTER TABLE accounts
|
|
18
|
+
ADD CONSTRAINT fk_accounts_role
|
|
19
|
+
FOREIGN KEY (role_id) REFERENCES roles(id)
|
|
20
|
+
ON DELETE SET NULL;
|
|
21
|
+
|
|
22
|
+
-- Add index for faster role lookups
|
|
23
|
+
CREATE INDEX idx_role_id ON accounts(role_id);
|
|
24
|
+
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
-- Migration to rename uuid column to linked_id and add external_source
|
|
2
|
+
-- This migration is for existing installations that already have the accounts table
|
|
3
|
+
|
|
4
|
+
-- Rename uuid column to linked_id and increase length to support various ID formats
|
|
5
|
+
ALTER TABLE accounts CHANGE COLUMN uuid linked_id VARCHAR(255) UNIQUE NULL;
|
|
6
|
+
|
|
7
|
+
-- Add external_source column to track account origin
|
|
8
|
+
ALTER TABLE accounts ADD COLUMN external_source VARCHAR(32) NULL AFTER linked_id;
|
|
9
|
+
|
|
10
|
+
-- Update existing records to mark them as 'local' source
|
|
11
|
+
UPDATE accounts SET external_source = 'local' WHERE external_source IS NULL;
|
|
12
|
+
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
-- Add password_hash column for credentials-based authentication
|
|
2
|
+
-- This is optional and only needed if using CredentialsAuthProvider
|
|
3
|
+
|
|
4
|
+
ALTER TABLE accounts ADD COLUMN password_hash VARCHAR(255) NULL AFTER username;
|
|
5
|
+
|
|
6
|
+
-- Note: For existing systems, passwords can be set via AccountService.setPassword()
|
|
7
|
+
|
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@open-core/identity",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Flexible identity and authentication system for OpenCore Framework",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc -p tsconfig.json",
|
|
9
|
+
"clean": "rimraf dist",
|
|
10
|
+
"lint": "eslint . --ext .ts",
|
|
11
|
+
"lint:fix": "eslint . --ext .ts --fix",
|
|
12
|
+
"test": "echo \"No tests yet\""
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"opencore",
|
|
16
|
+
"fivem",
|
|
17
|
+
"framework",
|
|
18
|
+
"identity",
|
|
19
|
+
"authentication",
|
|
20
|
+
"authorization",
|
|
21
|
+
"permissions",
|
|
22
|
+
"roles",
|
|
23
|
+
"rbac"
|
|
24
|
+
],
|
|
25
|
+
"author": "OpenCore Team",
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "https://github.com/newcore-network/opencore-identity.git"
|
|
30
|
+
},
|
|
31
|
+
"packageManager": "pnpm@10.13.1",
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@open-core/framework": "^1.0.1-beta.1",
|
|
34
|
+
"bcrypt": "^6.0.0",
|
|
35
|
+
"reflect-metadata": "^0.2.2",
|
|
36
|
+
"tsyringe": "^4.10.0",
|
|
37
|
+
"uuid": "^13.0.0",
|
|
38
|
+
"zod": "^4.1.13"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@types/bcrypt": "^6.0.0",
|
|
42
|
+
"@types/node": "^22.7.5",
|
|
43
|
+
"@typescript-eslint/eslint-plugin": "^8.48.1",
|
|
44
|
+
"@typescript-eslint/parser": "^8.48.1",
|
|
45
|
+
"eslint": "^9.39.1",
|
|
46
|
+
"eslint-config-prettier": "^10.1.8",
|
|
47
|
+
"eslint-plugin-import": "^2.32.0",
|
|
48
|
+
"eslint-plugin-prettier": "^5.5.4",
|
|
49
|
+
"prettier": "3.7.1",
|
|
50
|
+
"rimraf": "^6.0.1",
|
|
51
|
+
"typescript": "^5.9.3"
|
|
52
|
+
},
|
|
53
|
+
"files": [
|
|
54
|
+
"dist",
|
|
55
|
+
"migrations",
|
|
56
|
+
"LICENSE",
|
|
57
|
+
"README.md"
|
|
58
|
+
]
|
|
59
|
+
}
|