factor-based-permissions 0.0.1 → 0.0.2
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/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +90 -0
- package/dist/index.js.map +1 -0
- package/package.json +20 -4
- package/index.js +0 -1
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare class FactorBasedPermissions<TFactor extends number, TPermission extends number> {
|
|
2
|
+
private _factorsLookup;
|
|
3
|
+
private _permissionsLookup;
|
|
4
|
+
private _permissionCheckLookup;
|
|
5
|
+
constructor(serialized?: string | null | undefined);
|
|
6
|
+
private _parseFactors;
|
|
7
|
+
private _parsePermissions;
|
|
8
|
+
checkPermission(permission: TPermission): boolean | null;
|
|
9
|
+
getSatisfiedFactors(permission: TPermission): TFactor[];
|
|
10
|
+
getMissingFactors(permission: TPermission): TFactor[];
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAmBA,qBAAa,sBAAsB,CAAC,OAAO,SAAS,MAAM,EAAE,WAAW,SAAS,MAAM;IACpF,OAAO,CAAC,cAAc,CAA+B;IACrD,OAAO,CAAC,kBAAkB,CAAqC;IAC/D,OAAO,CAAC,sBAAsB,CAA0C;gBAErD,UAAU,CAAC,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS;IAQzD,OAAO,CAAC,aAAa;IAWrB,OAAO,CAAC,iBAAiB;IAsBlB,eAAe,CAAC,UAAU,EAAE,WAAW;IAyBvC,mBAAmB,CAAC,UAAU,EAAE,WAAW;IAW3C,iBAAiB,CAAC,UAAU,EAAE,WAAW;CAUjD"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
const SATISFIED_FACTORS_PREFIX = "!";
|
|
2
|
+
const PERMISSIONS_PREFIX = "#";
|
|
3
|
+
const ITEMS_DELIM = ",";
|
|
4
|
+
const GROUP_DELIM = "&";
|
|
5
|
+
const REQUIRED_FACTORS_DELIM = "+";
|
|
6
|
+
const groupsRegex = buildGroupsRegex();
|
|
7
|
+
function buildGroupsRegex() {
|
|
8
|
+
const groupPrefixes = SATISFIED_FACTORS_PREFIX + PERMISSIONS_PREFIX;
|
|
9
|
+
const satisfiedFactorsGroup = `(?:${SATISFIED_FACTORS_PREFIX}([^${groupPrefixes}]*))?`;
|
|
10
|
+
const permissionGroupsGroup = `(?:${PERMISSIONS_PREFIX}([^${groupPrefixes}]*))?`;
|
|
11
|
+
return new RegExp(satisfiedFactorsGroup + permissionGroupsGroup);
|
|
12
|
+
}
|
|
13
|
+
function parseNumeric(value) {
|
|
14
|
+
return parseInt(value, 32);
|
|
15
|
+
}
|
|
16
|
+
export class FactorBasedPermissions {
|
|
17
|
+
_factorsLookup = new Map();
|
|
18
|
+
_permissionsLookup = new Map();
|
|
19
|
+
_permissionCheckLookup = new Map();
|
|
20
|
+
constructor(serialized) {
|
|
21
|
+
if (typeof serialized === "string") {
|
|
22
|
+
const matches = groupsRegex.exec(serialized);
|
|
23
|
+
this._parseFactors(matches?.[1] ?? null);
|
|
24
|
+
this._parsePermissions(matches?.[2] ?? null);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
_parseFactors(satisfiedFactorsGroup) {
|
|
28
|
+
if (!satisfiedFactorsGroup) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
for (const factor of satisfiedFactorsGroup.split(ITEMS_DELIM)) {
|
|
32
|
+
const factorNumeric = parseNumeric(factor);
|
|
33
|
+
this._factorsLookup.set(factorNumeric, true);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
_parsePermissions(permissionGroupsGroup) {
|
|
37
|
+
if (!permissionGroupsGroup) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
for (const permissionGroup of permissionGroupsGroup.split(GROUP_DELIM)) {
|
|
41
|
+
const [permissionsRaw, requiredFactorsRaw] = permissionGroup.split(REQUIRED_FACTORS_DELIM);
|
|
42
|
+
const permissions = permissionsRaw?.split(ITEMS_DELIM)?.map((parseNumeric)) ?? [];
|
|
43
|
+
const requiredFactors = requiredFactorsRaw?.split(ITEMS_DELIM)?.map((parseNumeric)) ?? [];
|
|
44
|
+
for (const permission of permissions) {
|
|
45
|
+
this._permissionsLookup.set(permission, requiredFactors);
|
|
46
|
+
}
|
|
47
|
+
for (const requiredFactor of requiredFactors) {
|
|
48
|
+
if (!this._factorsLookup.has(requiredFactor)) {
|
|
49
|
+
this._factorsLookup.set(requiredFactor, false);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
checkPermission(permission) {
|
|
55
|
+
const cachedResult = this._permissionCheckLookup.get(permission);
|
|
56
|
+
if (cachedResult !== undefined) {
|
|
57
|
+
return cachedResult;
|
|
58
|
+
}
|
|
59
|
+
const requiredFactors = this._permissionsLookup.get(permission);
|
|
60
|
+
if (!requiredFactors) {
|
|
61
|
+
this._permissionCheckLookup.set(permission, null);
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
for (const requiredFactor of requiredFactors) {
|
|
65
|
+
if (!this._factorsLookup.get(requiredFactor)) {
|
|
66
|
+
this._permissionCheckLookup.set(permission, false);
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
this._permissionCheckLookup.set(permission, true);
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
getSatisfiedFactors(permission) {
|
|
74
|
+
const cachedResult = this._permissionCheckLookup.get(permission);
|
|
75
|
+
const requiredFactors = this._permissionsLookup.get(permission) ?? [];
|
|
76
|
+
if (cachedResult === true) {
|
|
77
|
+
return requiredFactors;
|
|
78
|
+
}
|
|
79
|
+
return requiredFactors.filter((factor) => this._factorsLookup.get(factor));
|
|
80
|
+
}
|
|
81
|
+
getMissingFactors(permission) {
|
|
82
|
+
const cachedResult = this._permissionCheckLookup.get(permission);
|
|
83
|
+
if (cachedResult === true) {
|
|
84
|
+
return [];
|
|
85
|
+
}
|
|
86
|
+
const requiredFactors = this._permissionsLookup.get(permission) ?? [];
|
|
87
|
+
return requiredFactors.filter((factor) => !this._factorsLookup.get(factor));
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,MAAM,wBAAwB,GAAG,GAAG,CAAC;AACrC,MAAM,kBAAkB,GAAG,GAAG,CAAC;AAC/B,MAAM,WAAW,GAAG,GAAG,CAAC;AACxB,MAAM,WAAW,GAAG,GAAG,CAAC;AACxB,MAAM,sBAAsB,GAAG,GAAG,CAAC;AAEnC,MAAM,WAAW,GAAG,gBAAgB,EAAE,CAAC;AAEvC,SAAS,gBAAgB;IACvB,MAAM,aAAa,GAAG,wBAAwB,GAAG,kBAAkB,CAAC;IACpE,MAAM,qBAAqB,GAAG,MAAM,wBAAwB,MAAM,aAAa,OAAO,CAAC;IACvF,MAAM,qBAAqB,GAAG,MAAM,kBAAkB,MAAM,aAAa,OAAO,CAAC;IACjF,OAAO,IAAI,MAAM,CAAC,qBAAqB,GAAG,qBAAqB,CAAC,CAAC;AACnE,CAAC;AAED,SAAS,YAAY,CAAmB,KAAa;IACnD,OAAO,QAAQ,CAAC,KAAK,EAAE,EAAE,CAAM,CAAC;AAClC,CAAC;AAED,MAAM,OAAO,sBAAsB;IACzB,cAAc,GAAG,IAAI,GAAG,EAAoB,CAAC;IAC7C,kBAAkB,GAAG,IAAI,GAAG,EAA0B,CAAC;IACvD,sBAAsB,GAAG,IAAI,GAAG,EAA+B,CAAC;IAExE,YAAmB,UAAsC;QACvD,IAAI,OAAO,UAAU,KAAK,QAAQ,EAAE,CA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tE,IAAI,YAAY,KAAK,IAAI,EAAE,CAAC;YAC1B,OAAO,eAAe,CAAC;QACzB,CAAC;QAED,OAAO,eAAe,CAAC,MAAM,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC;IAC7E,CAAC;IAEM,iBAAiB,CAAC,UAAuB;QAC9C,MAAM,YAAY,GAAG,IAAI,CAAC,sBAAsB,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;QAEjE,IAAI,YAAY,KAAK,IAAI,EAAE,CAAC;YAC1B,OAAO,EAAE,CAAC;QACZ,CAAC;QAED,MAAM,eAAe,GAAG,IAAI,CAAC,kBAAkB,CAAC,GAAG,CAAC,UAAU,CAAC,IAAI,EAAE,CAAC;QACtE,OAAO,eAAe,CAAC,MAAM,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC;IAC9E,CAAC;CACF"}
|
package/package.json
CHANGED
|
@@ -1,9 +1,25 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "factor-based-permissions",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
4
|
"description": "",
|
|
5
5
|
"license": "MIT",
|
|
6
|
-
"author": "",
|
|
7
|
-
"type": "
|
|
8
|
-
"
|
|
6
|
+
"author": "0x2E757",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"main": "dist/index.js",
|
|
12
|
+
"types": "dist/index.d.ts",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"import": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "tsc"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"typescript": "^5.9.3"
|
|
24
|
+
}
|
|
9
25
|
}
|
package/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export default function noop() { }
|