@open-core/identity 1.2.2 → 1.2.4

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.
@@ -45,7 +45,7 @@ export declare abstract class IdentityStore {
45
45
  * @param id - The internal account ID.
46
46
  * @param data - Partial object containing fields to update.
47
47
  */
48
- abstract update(id: string, data: Partial<IdentityAccount>): Promise<void>;
48
+ abstract update(id: string | number, data: Partial<IdentityAccount>): Promise<void>;
49
49
  /**
50
50
  * Prohibits or allows an account from connecting.
51
51
  *
@@ -54,7 +54,7 @@ export declare abstract class IdentityStore {
54
54
  * @param reason - Optional explanation for the ban.
55
55
  * @param expiresAt - Optional expiration timestamp.
56
56
  */
57
- abstract setBan(id: string, banned: boolean, reason?: string, expiresAt?: Date | null): Promise<void>;
57
+ abstract setBan(id: string | number, banned: boolean, reason?: string, expiresAt?: Date | null): Promise<void>;
58
58
  }
59
59
  /**
60
60
  * Persistence contract for security roles.
@@ -66,12 +66,12 @@ export declare abstract class IdentityStore {
66
66
  */
67
67
  export declare abstract class RoleStore {
68
68
  /**
69
- * Retrieves a role definition by its technical name.
69
+ * Retrieves a role definition by its technical identifier.
70
70
  *
71
- * @param name - Technical name (e.g., 'admin').
71
+ * @param id - Technical identifier (e.g., 'admin' or 1).
72
72
  * @returns A promise resolving to the role or null if not found.
73
73
  */
74
- abstract findByName(name: string): Promise<IdentityRole | null>;
74
+ abstract findById(id: string | number): Promise<IdentityRole | null>;
75
75
  /**
76
76
  * Resolves the default role for newly connected accounts.
77
77
  *
@@ -87,7 +87,7 @@ export declare abstract class RoleStore {
87
87
  /**
88
88
  * Removes a role from the system.
89
89
  *
90
- * @param name - Technical name of the role to delete.
90
+ * @param id - Technical identifier of the role to delete.
91
91
  */
92
- abstract delete(name: string): Promise<void>;
92
+ abstract delete(id: string | number): Promise<void>;
93
93
  }
@@ -12,7 +12,6 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
12
12
  };
13
13
  import { injectable, inject } from "tsyringe";
14
14
  import { Server } from "@open-core/framework";
15
- import { v4 as uuidv4 } from "uuid";
16
15
  import { IDENTITY_OPTIONS } from "../../tokens";
17
16
  import { IdentityStore } from "../../contracts";
18
17
  import bcrypt from "bcryptjs";
@@ -68,8 +67,9 @@ let CredentialsAuthProvider = class CredentialsAuthProvider extends Server.AuthP
68
67
  if (this.isBanned(account)) {
69
68
  return { success: false, error: account.banReason ?? "Account is banned" };
70
69
  }
71
- player.linkAccount(account.linkedId);
72
- return { success: true, accountID: account.linkedId };
70
+ const accountIdStr = String(account.id);
71
+ player.linkAccount(accountIdStr);
72
+ return { success: true, accountID: accountIdStr };
73
73
  }
74
74
  /**
75
75
  * Registers a new account with a username and password.
@@ -95,11 +95,11 @@ let CredentialsAuthProvider = class CredentialsAuthProvider extends Server.AuthP
95
95
  username,
96
96
  passwordHash,
97
97
  identifier: primaryIdentifier,
98
- linkedId: uuidv4(),
99
- roleName: this.options.principal.defaultRole || "user",
98
+ roleId: this.options.principal.defaultRole || "user",
100
99
  });
101
- player.linkAccount(account.linkedId);
102
- return { success: true, accountID: account.linkedId, isNewAccount: true };
100
+ const accountIdStr = String(account.id);
101
+ player.linkAccount(accountIdStr);
102
+ return { success: true, accountID: accountIdStr, isNewAccount: true };
103
103
  }
104
104
  /**
105
105
  * Validates if the player's current linked account session is still active.
@@ -115,7 +115,7 @@ let CredentialsAuthProvider = class CredentialsAuthProvider extends Server.AuthP
115
115
  if (!account || this.isBanned(account)) {
116
116
  return { success: false, error: "Session invalid or account banned" };
117
117
  }
118
- return { success: true, accountID: account.linkedId };
118
+ return { success: true, accountID: String(account.id) };
119
119
  }
120
120
  /**
121
121
  * Performs logout logic for the player.
@@ -12,7 +12,6 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
12
12
  };
13
13
  import { injectable, inject } from "tsyringe";
14
14
  import { Server } from "@open-core/framework";
15
- import { v4 as uuidv4 } from "uuid";
16
15
  import { IDENTITY_OPTIONS } from "../../tokens";
17
16
  import { IdentityStore } from "../../contracts";
18
17
  /**
@@ -85,7 +84,7 @@ let LocalAuthProvider = class LocalAuthProvider extends Server.AuthProviderContr
85
84
  if (account.isBanned && (!account.banExpiresAt || account.banExpiresAt > new Date())) {
86
85
  return { success: false, error: account.banReason ?? "Account is banned" };
87
86
  }
88
- return { success: true, accountID: account.linkedId };
87
+ return { success: true, accountID: String(account.id) };
89
88
  }
90
89
  /**
91
90
  * Clears the authentication state for a player.
@@ -115,8 +114,7 @@ let LocalAuthProvider = class LocalAuthProvider extends Server.AuthProviderContr
115
114
  }
116
115
  account = await this.store.create({
117
116
  identifier: identifierValue,
118
- linkedId: uuidv4(),
119
- roleName: "user",
117
+ roleId: "user",
120
118
  });
121
119
  isNew = true;
122
120
  }
@@ -131,8 +129,9 @@ let LocalAuthProvider = class LocalAuthProvider extends Server.AuthProviderContr
131
129
  };
132
130
  }
133
131
  }
134
- player.linkAccount(account.linkedId);
135
- return { success: true, accountID: account.linkedId, isNewAccount: isNew };
132
+ const accountIdStr = String(account.id);
133
+ player.linkAccount(accountIdStr);
134
+ return { success: true, accountID: accountIdStr, isNewAccount: isNew };
136
135
  }
137
136
  /**
138
137
  * Internal implementation for API-based authentication strategy.
@@ -102,29 +102,37 @@ let IdentityPrincipalProvider = class IdentityPrincipalProvider extends Server.P
102
102
  if (!account)
103
103
  return null;
104
104
  let role;
105
- if (this.options.principal.mode === "roles") {
106
- role = this.options.principal.roles?.[account.roleName];
107
- }
108
- else if (this.roleStore) {
109
- const dbRole = await this.roleStore.findByName(account.roleName);
110
- if (dbRole)
111
- role = dbRole;
105
+ const roleId = account.roleId;
106
+ if (roleId !== undefined && roleId !== null && roleId !== "") {
107
+ if (this.options.principal.mode === "roles") {
108
+ role = this.options.principal.roles?.[roleId];
109
+ }
110
+ else if (this.roleStore) {
111
+ const dbRole = await this.roleStore.findById(roleId);
112
+ if (dbRole)
113
+ role = dbRole;
114
+ }
112
115
  }
113
116
  if (!role) {
114
- const defaultName = this.options.principal.defaultRole || "user";
115
- role = this.options.principal.roles?.[defaultName];
117
+ const defaultRoleId = this.options.principal.defaultRole;
118
+ if (defaultRoleId !== undefined && defaultRoleId !== null && defaultRoleId !== "") {
119
+ role = this.options.principal.roles?.[defaultRoleId];
120
+ if (!role && this.roleStore && this.options.principal.mode === "db") {
121
+ role = await this.roleStore.getDefaultRole();
122
+ }
123
+ }
116
124
  }
117
125
  if (!role)
118
126
  return null;
119
127
  const effectivePermissions = this.mergePermissions(role.permissions, account.customPermissions);
120
128
  return {
121
- id: account.linkedId,
122
- name: role.displayName || role.name,
129
+ id: linkedId,
130
+ name: role.displayName || String(role.id),
123
131
  rank: role.rank,
124
132
  permissions: effectivePermissions,
125
133
  meta: {
126
134
  accountId: account.id,
127
- roleName: role.name,
135
+ roleId: role.id,
128
136
  },
129
137
  };
130
138
  }
@@ -30,10 +30,10 @@ export declare class AccountService {
30
30
  /**
31
31
  * Assigns a security role to an account.
32
32
  *
33
- * @param accountId - The linked ID of the account.
34
- * @param roleName - Technical name of the role to assign.
33
+ * @param accountId - The unique ID of the account.
34
+ * @param roleId - Technical identifier of the role to assign.
35
35
  */
36
- assignRole(accountId: string, roleName: string): Promise<void>;
36
+ assignRole(accountId: string | number, roleId: string | number): Promise<void>;
37
37
  /**
38
38
  * Grants a custom permission override to an account.
39
39
  *
@@ -48,11 +48,11 @@ let AccountService = class AccountService {
48
48
  /**
49
49
  * Assigns a security role to an account.
50
50
  *
51
- * @param accountId - The linked ID of the account.
52
- * @param roleName - Technical name of the role to assign.
51
+ * @param accountId - The unique ID of the account.
52
+ * @param roleId - Technical identifier of the role to assign.
53
53
  */
54
- async assignRole(accountId, roleName) {
55
- await this.store.update(accountId, { roleName });
54
+ async assignRole(accountId, roleId) {
55
+ await this.store.update(accountId, { roleId });
56
56
  }
57
57
  /**
58
58
  * Grants a custom permission override to an account.
@@ -30,23 +30,23 @@ export declare class RoleService {
30
30
  /**
31
31
  * Updates an existing role's rank or permissions.
32
32
  *
33
- * @param name - The unique technical name of the role to update.
33
+ * @param id - The unique technical identifier of the role to update.
34
34
  * @param data - Partial object containing the fields to modify.
35
35
  * @returns A promise that resolves when the update is complete.
36
36
  */
37
- update(name: string, data: Partial<Omit<IdentityRole, "name">>): Promise<void>;
37
+ update(id: string | number, data: Partial<Omit<IdentityRole, "id">>): Promise<void>;
38
38
  /**
39
39
  * Permanently removes a role definition from the system.
40
40
  *
41
- * @param name - The technical name of the role to delete.
41
+ * @param id - The technical identifier of the role to delete.
42
42
  * @returns A promise that resolves when the role is deleted.
43
43
  */
44
- delete(name: string): Promise<void>;
44
+ delete(id: string | number): Promise<void>;
45
45
  /**
46
46
  * Retrieves the full list of permissions granted to a specific role.
47
47
  *
48
- * @param name - The technical name of the role.
48
+ * @param id - The technical identifier of the role.
49
49
  * @returns A promise resolving to an array of permission strings.
50
50
  */
51
- getPermissions(name: string): Promise<string[]>;
51
+ getPermissions(id: string | number): Promise<string[]>;
52
52
  }
@@ -46,12 +46,12 @@ let RoleService = class RoleService {
46
46
  /**
47
47
  * Updates an existing role's rank or permissions.
48
48
  *
49
- * @param name - The unique technical name of the role to update.
49
+ * @param id - The unique technical identifier of the role to update.
50
50
  * @param data - Partial object containing the fields to modify.
51
51
  * @returns A promise that resolves when the update is complete.
52
52
  */
53
- async update(name, data) {
54
- const existing = await this.store.findByName(name);
53
+ async update(id, data) {
54
+ const existing = await this.store.findById(id);
55
55
  if (!existing)
56
56
  return;
57
57
  await this.store.save({
@@ -62,20 +62,20 @@ let RoleService = class RoleService {
62
62
  /**
63
63
  * Permanently removes a role definition from the system.
64
64
  *
65
- * @param name - The technical name of the role to delete.
65
+ * @param id - The technical identifier of the role to delete.
66
66
  * @returns A promise that resolves when the role is deleted.
67
67
  */
68
- async delete(name) {
69
- await this.store.delete(name);
68
+ async delete(id) {
69
+ await this.store.delete(id);
70
70
  }
71
71
  /**
72
72
  * Retrieves the full list of permissions granted to a specific role.
73
73
  *
74
- * @param name - The technical name of the role.
74
+ * @param id - The technical identifier of the role.
75
75
  * @returns A promise resolving to an array of permission strings.
76
76
  */
77
- async getPermissions(name) {
78
- const role = await this.store.findByName(name);
77
+ async getPermissions(id) {
78
+ const role = await this.store.findById(id);
79
79
  return role?.permissions || [];
80
80
  }
81
81
  };
package/dist/types.d.ts CHANGED
@@ -31,9 +31,9 @@ export type PrincipalMode = "roles" | "db" | "api";
31
31
  */
32
32
  export interface IdentityRole {
33
33
  /**
34
- * Technical identifier for the role (e.g., 'admin', 'moderator', 'user').
34
+ * Technical identifier for the role (e.g., 'admin', 1, 'uuid').
35
35
  */
36
- name: string;
36
+ id: string | number;
37
37
  /**
38
38
  * Hierarchical weight.
39
39
  *
@@ -93,12 +93,12 @@ export interface IdentityOptions {
93
93
  *
94
94
  * Required when mode is 'roles'.
95
95
  */
96
- roles?: Record<string, IdentityRole>;
96
+ roles?: Record<string | number, IdentityRole>;
97
97
  /**
98
- * The name of the role assigned to newly created accounts.
98
+ * The ID of the role assigned to newly created accounts.
99
99
  * @defaultValue 'user'
100
100
  */
101
- defaultRole?: string;
101
+ defaultRole?: string | number;
102
102
  /**
103
103
  * Time-to-live in milliseconds for cached principal data.
104
104
  *
@@ -121,21 +121,15 @@ export interface IdentityAccount {
121
121
  /**
122
122
  * Internal unique database/store ID.
123
123
  */
124
- id: string;
125
- /**
126
- * External stable ID used by the framework (linkedID).
127
- *
128
- * Usually a UUID or an external system ID.
129
- */
130
- linkedId: string;
124
+ id: string | number;
131
125
  /**
132
126
  * Primary connection identifier (e.g., 'license:123...').
133
127
  */
134
128
  identifier: string;
135
129
  /**
136
- * Current technical role name assigned to this account.
130
+ * Current technical role ID assigned to this account.
137
131
  */
138
- roleName: string;
132
+ roleId?: string | number;
139
133
  /**
140
134
  * Optional technical username for credentials-based authentication.
141
135
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-core/identity",
3
- "version": "1.2.2",
3
+ "version": "1.2.4",
4
4
  "description": "Enterprise-grade identity, authentication, and authorization plugin for OpenCore Framework",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -14,6 +14,7 @@
14
14
  },
15
15
  "scripts": {
16
16
  "build": "tsc -p tsconfig.json",
17
+ "prepack": "npm run build",
17
18
  "clean": "rimraf dist",
18
19
  "lint": "eslint . --ext .ts",
19
20
  "lint:fix": "eslint . --ext .ts --fix",