@omnitron-dev/access 0.0.0-stage → 0.3.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/LICENSE +21 -0
- package/README.md +84 -2
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/permissions.d.ts +77 -0
- package/dist/permissions.d.ts.map +1 -0
- package/dist/permissions.js +115 -0
- package/dist/platform-roles.d.ts +52 -0
- package/dist/platform-roles.d.ts.map +1 -0
- package/dist/platform-roles.js +53 -0
- package/dist/role-hierarchy.d.ts +32 -0
- package/dist/role-hierarchy.d.ts.map +1 -0
- package/dist/role-hierarchy.js +84 -0
- package/dist/session-policy.d.ts +26 -0
- package/dist/session-policy.d.ts.map +1 -0
- package/dist/session-policy.js +45 -0
- package/package.json +59 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 LuxQuant
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,85 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @omnitron-dev/access
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Role hierarchies and permission policies shared by browsers, servers and workers.
|
|
4
|
+
No runtime dependencies, Node built-ins, decorators, network clients or global state.
|
|
5
|
+
|
|
6
|
+
```ts
|
|
7
|
+
import { createRoleHierarchy } from '@omnitron-dev/access/roles';
|
|
8
|
+
import { createPermissionChecker } from '@omnitron-dev/access/permissions';
|
|
9
|
+
|
|
10
|
+
export const roles = createRoleHierarchy({
|
|
11
|
+
reader: { level: 0 },
|
|
12
|
+
author: { level: 1, inherits: ['reader'] },
|
|
13
|
+
reviewer: { level: 2, inherits: ['reader'] },
|
|
14
|
+
owner: { level: 3, inherits: ['author', 'reviewer'], assignable: false },
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
roles.expandRoles('reviewer'); // ['reader', 'reviewer']; rank does not grant author
|
|
18
|
+
roles.canModerateRole(['owner'], 'author'); // true; peers cannot moderate each other
|
|
19
|
+
|
|
20
|
+
const allows = createPermissionChecker(['documents.read', 'media.*'], 'explicit');
|
|
21
|
+
allows('media.upload'); // true
|
|
22
|
+
allows('documents.read.all'); // false
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Choose the surface
|
|
26
|
+
|
|
27
|
+
| Import | Responsibility |
|
|
28
|
+
| --- | --- |
|
|
29
|
+
| `@omnitron-dev/access` | Pure role and permission primitives |
|
|
30
|
+
| `@omnitron-dev/access/roles` | Application-defined, immutable role hierarchy |
|
|
31
|
+
| `@omnitron-dev/access/permissions` | Matching, compiled checks, registry helpers and types |
|
|
32
|
+
| `@omnitron-dev/access/platform` | Optional five-role platform preset |
|
|
33
|
+
| `@omnitron-dev/access/session-policy` | Pure MFA, absolute session age and timezone-aware access windows |
|
|
34
|
+
|
|
35
|
+
The platform preset provides `user`, `moderator`, `security`, `admin` and
|
|
36
|
+
`superadmin`. Security and moderation have separate inheritance. Unknown roles
|
|
37
|
+
have rank `-1` and cannot be visibility, moderation or assignment targets.
|
|
38
|
+
Expansion preserves independent claims without granting them rank. The retired
|
|
39
|
+
`boss` claim expands to `user` for compatibility; service identity is established
|
|
40
|
+
by the authentication adapter, never by this hierarchy.
|
|
41
|
+
|
|
42
|
+
## Matching and enforcement
|
|
43
|
+
|
|
44
|
+
`hierarchical` (the default) lets a concrete parent grant its descendants:
|
|
45
|
+
`documents` grants `documents.read`. `explicit` requires an exact key, `*`, or
|
|
46
|
+
a suffix wildcard such as `documents.*`. Colons are literal characters.
|
|
47
|
+
Select the mode deliberately at each application boundary. Titan's Netron
|
|
48
|
+
helpers use explicit mode; the compatibility `titan-auth/access` entry uses
|
|
49
|
+
hierarchical mode by default. Neither migration broadens existing authority.
|
|
50
|
+
|
|
51
|
+
`createPermissionChecker` snapshots the supplied grants once. It checks exact
|
|
52
|
+
keys with a Set and walks dot boundaries for prefixes; it never scans every
|
|
53
|
+
grant on each check. Recreate it when the principal or grants change.
|
|
54
|
+
`hasAllPermissions`, `hasAnyPermission` and `resolvePermissions` compile once per
|
|
55
|
+
call. `resolvePermissions` returns recognised effective keys in registry order;
|
|
56
|
+
`expandWildcards` retains concrete grants verbatim for compatibility.
|
|
57
|
+
|
|
58
|
+
Registry scope, risk and labels are application metadata. Validate grants against
|
|
59
|
+
the applicable registry before storing them. Enforce authentication, tenant
|
|
60
|
+
ownership, resource state and grant delegation on the server. A browser check
|
|
61
|
+
only controls affordances. An assignment list is not an authorization gate:
|
|
62
|
+
check the target's current rank, then validate the requested role and ownership.
|
|
63
|
+
|
|
64
|
+
## Optional integration
|
|
65
|
+
|
|
66
|
+
JWT/session verification lives in `@omnitron-dev/titan-auth`; pass your hierarchy's
|
|
67
|
+
`expandRoleSet` as the shared-session preset's `roleExpander`. Token issuers and
|
|
68
|
+
verifiers must share the same definition. Opaque sessions can use the pure policy
|
|
69
|
+
without JWT or Titan Auth. Storage, Redis and database adapters are independent.
|
|
70
|
+
|
|
71
|
+
The package ships ESM and declarations, marks `sideEffects: false`, and exposes
|
|
72
|
+
narrow entry points. Importing permission helpers does not instantiate a role
|
|
73
|
+
preset. Application-owned adapters and catalogs remain outside the package.
|
|
74
|
+
|
|
75
|
+
`evaluateSessionPolicy(policy, { hasMfa, createdAt, now? })` evaluates current
|
|
76
|
+
session facts against optional `requireMfa`, `maxSessionDuration` (minutes), and
|
|
77
|
+
`timeWindows` (`days`, `startHour`, `endHour`, IANA `timezone`). Time windows may
|
|
78
|
+
cross midnight; their days name the starting day. End hours are exclusive, with
|
|
79
|
+
24 representing midnight. Unknown or malformed controls fail closed.
|
|
80
|
+
Persist proof that MFA was completed for the session; enrollment is not proof.
|
|
81
|
+
Evaluate on each protected request and renewal, using the original session
|
|
82
|
+
creation time. This pure helper owns neither clocks, session storage nor tokens.
|
|
83
|
+
|
|
84
|
+
See the [access policy guide](https://omnitron.dev/docs/auth/access-policy) for
|
|
85
|
+
configuration, migration and enforcement examples.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAC;AACpC,cAAc,kBAAkB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Application-defined permission keys, scope metadata and matching rules.
|
|
3
|
+
* Keys use dot-separated names chosen by the application, for example
|
|
4
|
+
* `workspace.documents.edit`. A full wildcard grants every key; a suffix
|
|
5
|
+
* wildcard grants descendants only; a concrete key also grants descendants.
|
|
6
|
+
* Matching does not infer tenant ownership, role membership or scope. Callers
|
|
7
|
+
* select the applicable registry and granted keys at the enforcement boundary.
|
|
8
|
+
*/
|
|
9
|
+
/** Scope under which a permission is enforced. */
|
|
10
|
+
export type PermissionScope = string;
|
|
11
|
+
/** Severity bucket — used by the admin UI to highlight risky perms. */
|
|
12
|
+
export type PermissionRisk = 'low' | 'medium' | 'high' | 'critical';
|
|
13
|
+
/**
|
|
14
|
+
* One row in an application-owned registry. Scope is descriptive metadata;
|
|
15
|
+
* the type parameter preserves a project's vocabulary across its catalog.
|
|
16
|
+
*/
|
|
17
|
+
export interface PermissionDefinition<Scope extends PermissionScope = PermissionScope> {
|
|
18
|
+
/** Canonical dot-separated identifier — see grammar above. */
|
|
19
|
+
key: string;
|
|
20
|
+
/** Where the permission is enforced. */
|
|
21
|
+
scope: Scope;
|
|
22
|
+
/** Top-level grouping for UI rendering. */
|
|
23
|
+
domain: string;
|
|
24
|
+
/** Sub-grouping within a domain (e.g. 'Users', 'Roles'). */
|
|
25
|
+
category: string;
|
|
26
|
+
/** Short human-readable label. */
|
|
27
|
+
label: string;
|
|
28
|
+
/** Longer description shown on hover / detail. */
|
|
29
|
+
description: string;
|
|
30
|
+
/** Risk bucket — informational, never affects matching. */
|
|
31
|
+
risk: PermissionRisk;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Hierarchical grants include descendants (`documents` grants `documents.read`).
|
|
35
|
+
* Explicit grants require an exact key or a suffix wildcard. Explicit mode also
|
|
36
|
+
* preserves the transport convention that `documents.*` matches `documents.`.
|
|
37
|
+
*/
|
|
38
|
+
export type PermissionMatchMode = 'hierarchical' | 'explicit';
|
|
39
|
+
/** Compare one grant without allocating an index. Colons have no special meaning. */
|
|
40
|
+
export declare function permissionGrants(granted: string, required: string, mode?: PermissionMatchMode): boolean;
|
|
41
|
+
/** True if any grant authorises a single key. For repeated checks, compile once. */
|
|
42
|
+
export declare function hasPermission(granted: readonly string[], required: string, mode?: PermissionMatchMode): boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Snapshot a grant set for repeated checks. Exact lookup is constant time; prefix
|
|
45
|
+
* lookups depend on the required key's dot boundaries, not the number of grants.
|
|
46
|
+
* No regex, global cache, token parsing or mutable caller state is retained.
|
|
47
|
+
* Rebuild the checker when the authenticated principal's grants change.
|
|
48
|
+
*/
|
|
49
|
+
export declare function createPermissionChecker(granted: readonly string[], mode?: PermissionMatchMode): (required: string) => boolean;
|
|
50
|
+
/** Empty requirements are satisfied; compile only once for the whole decision. */
|
|
51
|
+
export declare function hasAllPermissions(granted: readonly string[], required: readonly string[], mode?: PermissionMatchMode): boolean;
|
|
52
|
+
/** An empty requirement list cannot provide a matching alternative. */
|
|
53
|
+
export declare function hasAnyPermission(granted: readonly string[], required: readonly string[], mode?: PermissionMatchMode): boolean;
|
|
54
|
+
/**
|
|
55
|
+
* Is a grant recognised by the selected registry? The full wildcard is
|
|
56
|
+
* accepted; a suffix wildcard needs a registered descendant. Concrete keys
|
|
57
|
+
* must exist in the registry. Call this before persisting role grants.
|
|
58
|
+
*
|
|
59
|
+
* Pass the registry restricted to the right scope to enforce that
|
|
60
|
+
* a platform role can't be granted an org-only key or vice versa.
|
|
61
|
+
*/
|
|
62
|
+
export declare function isValidPermissionKey(key: string, registry: ReadonlyArray<PermissionDefinition>): boolean;
|
|
63
|
+
/**
|
|
64
|
+
* Replace wildcard entries with registered keys, retaining concrete grants
|
|
65
|
+
* verbatim. For the complete effective set, including hierarchical descendants,
|
|
66
|
+
* use resolvePermissions. Enforcement uses permissionGrants directly.
|
|
67
|
+
*/
|
|
68
|
+
export declare function expandWildcards(granted: ReadonlyArray<string>, registry: ReadonlyArray<PermissionDefinition>): string[];
|
|
69
|
+
/**
|
|
70
|
+
* Convenience factory used by the per-domain catalog files
|
|
71
|
+
* (`permissions.ts`, `org-permissions.ts`) so each entry stays a
|
|
72
|
+
* single-line declaration. Risk defaults to 'low'.
|
|
73
|
+
*/
|
|
74
|
+
export declare function definePermission<const Scope extends PermissionScope>(key: string, scope: Scope, domain: string, category: string, label: string, description: string, risk?: PermissionRisk): PermissionDefinition<Scope>;
|
|
75
|
+
/** Registered keys authorised by the grants, in registry order without duplicates. */
|
|
76
|
+
export declare function resolvePermissions(granted: readonly string[], registry: ReadonlyArray<Pick<PermissionDefinition, 'key'>>, mode?: PermissionMatchMode): string[];
|
|
77
|
+
//# sourceMappingURL=permissions.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"permissions.d.ts","sourceRoot":"","sources":["../src/permissions.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAMH,kDAAkD;AAClD,MAAM,MAAM,eAAe,GAAG,MAAM,CAAC;AAErC,uEAAuE;AACvE,MAAM,MAAM,cAAc,GAAG,KAAK,GAAG,QAAQ,GAAG,MAAM,GAAG,UAAU,CAAC;AAEpE;;;GAGG;AACH,MAAM,WAAW,oBAAoB,CAAC,KAAK,SAAS,eAAe,GAAG,eAAe;IACnF,8DAA8D;IAC9D,GAAG,EAAE,MAAM,CAAC;IACZ,wCAAwC;IACxC,KAAK,EAAE,KAAK,CAAC;IACb,2CAA2C;IAC3C,MAAM,EAAE,MAAM,CAAC;IACf,4DAA4D;IAC5D,QAAQ,EAAE,MAAM,CAAC;IACjB,kCAAkC;IAClC,KAAK,EAAE,MAAM,CAAC;IACd,kDAAkD;IAClD,WAAW,EAAE,MAAM,CAAC;IACpB,2DAA2D;IAC3D,IAAI,EAAE,cAAc,CAAC;CACtB;AAMD;;;;GAIG;AACH,MAAM,MAAM,mBAAmB,GAAG,cAAc,GAAG,UAAU,CAAC;AAE9D,qFAAqF;AACrF,wBAAgB,gBAAgB,CAC9B,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,IAAI,GAAE,mBAAoC,GAC5E,OAAO,CAOT;AAED,oFAAoF;AACpF,wBAAgB,aAAa,CAC3B,OAAO,EAAE,SAAS,MAAM,EAAE,EAAE,QAAQ,EAAE,MAAM,EAAE,IAAI,GAAE,mBAAoC,GACvF,OAAO,CAET;AAED;;;;;GAKG;AACH,wBAAgB,uBAAuB,CACrC,OAAO,EAAE,SAAS,MAAM,EAAE,EAAE,IAAI,GAAE,mBAAoC,GACrE,CAAC,QAAQ,EAAE,MAAM,KAAK,OAAO,CAY/B;AAED,kFAAkF;AAClF,wBAAgB,iBAAiB,CAC/B,OAAO,EAAE,SAAS,MAAM,EAAE,EAAE,QAAQ,EAAE,SAAS,MAAM,EAAE,EAAE,IAAI,GAAE,mBAAoC,GAClG,OAAO,CAET;AAED,uEAAuE;AACvE,wBAAgB,gBAAgB,CAC9B,OAAO,EAAE,SAAS,MAAM,EAAE,EAAE,QAAQ,EAAE,SAAS,MAAM,EAAE,EAAE,IAAI,GAAE,mBAAoC,GAClG,OAAO,CAET;AAMD;;;;;;;GAOG;AACH,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,EAAE,aAAa,CAAC,oBAAoB,CAAC,GAAG,OAAO,CAQxG;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAC7B,OAAO,EAAE,aAAa,CAAC,MAAM,CAAC,EAC9B,QAAQ,EAAE,aAAa,CAAC,oBAAoB,CAAC,GAC5C,MAAM,EAAE,CAeV;AAMD;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,CAAC,KAAK,SAAS,eAAe,EAClE,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,KAAK,EACZ,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,MAAM,EAChB,KAAK,EAAE,MAAM,EACb,WAAW,EAAE,MAAM,EACnB,IAAI,GAAE,cAAsB,GAC3B,oBAAoB,CAAC,KAAK,CAAC,CAE7B;AAED,sFAAsF;AACtF,wBAAgB,kBAAkB,CAChC,OAAO,EAAE,SAAS,MAAM,EAAE,EAC1B,QAAQ,EAAE,aAAa,CAAC,IAAI,CAAC,oBAAoB,EAAE,KAAK,CAAC,CAAC,EAC1D,IAAI,GAAE,mBAAoC,GACzC,MAAM,EAAE,CAGV"}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Application-defined permission keys, scope metadata and matching rules.
|
|
3
|
+
* Keys use dot-separated names chosen by the application, for example
|
|
4
|
+
* `workspace.documents.edit`. A full wildcard grants every key; a suffix
|
|
5
|
+
* wildcard grants descendants only; a concrete key also grants descendants.
|
|
6
|
+
* Matching does not infer tenant ownership, role membership or scope. Callers
|
|
7
|
+
* select the applicable registry and granted keys at the enforcement boundary.
|
|
8
|
+
*/
|
|
9
|
+
/** Compare one grant without allocating an index. Colons have no special meaning. */
|
|
10
|
+
export function permissionGrants(granted, required, mode = 'hierarchical') {
|
|
11
|
+
if (granted === '*' || granted === required)
|
|
12
|
+
return true;
|
|
13
|
+
if (granted.endsWith('.*')) {
|
|
14
|
+
const prefix = granted.slice(0, -1);
|
|
15
|
+
return required.startsWith(prefix) && (mode === 'explicit' || required.length > prefix.length);
|
|
16
|
+
}
|
|
17
|
+
return mode === 'hierarchical' && required.startsWith(`${granted}.`);
|
|
18
|
+
}
|
|
19
|
+
/** True if any grant authorises a single key. For repeated checks, compile once. */
|
|
20
|
+
export function hasPermission(granted, required, mode = 'hierarchical') {
|
|
21
|
+
return granted.some(grant => permissionGrants(grant, required, mode));
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Snapshot a grant set for repeated checks. Exact lookup is constant time; prefix
|
|
25
|
+
* lookups depend on the required key's dot boundaries, not the number of grants.
|
|
26
|
+
* No regex, global cache, token parsing or mutable caller state is retained.
|
|
27
|
+
* Rebuild the checker when the authenticated principal's grants change.
|
|
28
|
+
*/
|
|
29
|
+
export function createPermissionChecker(granted, mode = 'hierarchical') {
|
|
30
|
+
const keys = new Set(granted);
|
|
31
|
+
if (keys.has('*'))
|
|
32
|
+
return () => true;
|
|
33
|
+
return required => {
|
|
34
|
+
if (keys.has(required))
|
|
35
|
+
return true;
|
|
36
|
+
for (let dot = required.indexOf('.'); dot !== -1; dot = required.indexOf('.', dot + 1)) {
|
|
37
|
+
const prefix = required.slice(0, dot);
|
|
38
|
+
if (mode === 'hierarchical' && !prefix.endsWith('.*') && keys.has(prefix))
|
|
39
|
+
return true;
|
|
40
|
+
if ((mode === 'explicit' || dot < required.length - 1) && keys.has(`${prefix}.*`))
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
return false;
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
/** Empty requirements are satisfied; compile only once for the whole decision. */
|
|
47
|
+
export function hasAllPermissions(granted, required, mode = 'hierarchical') {
|
|
48
|
+
return required.every(createPermissionChecker(granted, mode));
|
|
49
|
+
}
|
|
50
|
+
/** An empty requirement list cannot provide a matching alternative. */
|
|
51
|
+
export function hasAnyPermission(granted, required, mode = 'hierarchical') {
|
|
52
|
+
return required.some(createPermissionChecker(granted, mode));
|
|
53
|
+
}
|
|
54
|
+
// ---------------------------------------------------------------------------
|
|
55
|
+
// VALIDATION
|
|
56
|
+
// ---------------------------------------------------------------------------
|
|
57
|
+
/**
|
|
58
|
+
* Is a grant recognised by the selected registry? The full wildcard is
|
|
59
|
+
* accepted; a suffix wildcard needs a registered descendant. Concrete keys
|
|
60
|
+
* must exist in the registry. Call this before persisting role grants.
|
|
61
|
+
*
|
|
62
|
+
* Pass the registry restricted to the right scope to enforce that
|
|
63
|
+
* a platform role can't be granted an org-only key or vice versa.
|
|
64
|
+
*/
|
|
65
|
+
export function isValidPermissionKey(key, registry) {
|
|
66
|
+
if (key === '*')
|
|
67
|
+
return true;
|
|
68
|
+
if (key.endsWith('.*')) {
|
|
69
|
+
// Wildcard is valid iff at least one concrete key lives under it.
|
|
70
|
+
const prefix = key.slice(0, -1);
|
|
71
|
+
return registry.some((d) => d.key.startsWith(prefix));
|
|
72
|
+
}
|
|
73
|
+
return registry.some((d) => d.key === key);
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Replace wildcard entries with registered keys, retaining concrete grants
|
|
77
|
+
* verbatim. For the complete effective set, including hierarchical descendants,
|
|
78
|
+
* use resolvePermissions. Enforcement uses permissionGrants directly.
|
|
79
|
+
*/
|
|
80
|
+
export function expandWildcards(granted, registry) {
|
|
81
|
+
const out = new Set();
|
|
82
|
+
for (const g of granted) {
|
|
83
|
+
if (g === '*') {
|
|
84
|
+
for (const d of registry)
|
|
85
|
+
out.add(d.key);
|
|
86
|
+
}
|
|
87
|
+
else if (g.endsWith('.*')) {
|
|
88
|
+
const prefix = g.slice(0, -1);
|
|
89
|
+
for (const d of registry) {
|
|
90
|
+
if (d.key.startsWith(prefix))
|
|
91
|
+
out.add(d.key);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
out.add(g);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return Array.from(out);
|
|
99
|
+
}
|
|
100
|
+
// ---------------------------------------------------------------------------
|
|
101
|
+
// FACTORY
|
|
102
|
+
// ---------------------------------------------------------------------------
|
|
103
|
+
/**
|
|
104
|
+
* Convenience factory used by the per-domain catalog files
|
|
105
|
+
* (`permissions.ts`, `org-permissions.ts`) so each entry stays a
|
|
106
|
+
* single-line declaration. Risk defaults to 'low'.
|
|
107
|
+
*/
|
|
108
|
+
export function definePermission(key, scope, domain, category, label, description, risk = 'low') {
|
|
109
|
+
return { key, scope, domain, category, label, description, risk };
|
|
110
|
+
}
|
|
111
|
+
/** Registered keys authorised by the grants, in registry order without duplicates. */
|
|
112
|
+
export function resolvePermissions(granted, registry, mode = 'hierarchical') {
|
|
113
|
+
const allows = createPermissionChecker(granted, mode);
|
|
114
|
+
return [...new Set(registry.filter(({ key }) => allows(key)).map(({ key }) => key))];
|
|
115
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Optional platform preset. Applications with another role vocabulary use
|
|
3
|
+
* createRoleHierarchy; importing the factory does not configure global state.
|
|
4
|
+
* Security and moderation are separate capabilities, even though their ranks
|
|
5
|
+
* differ. Trusted service identities do not belong to this human hierarchy.
|
|
6
|
+
*/
|
|
7
|
+
declare const platformDefinitions: {
|
|
8
|
+
user: {
|
|
9
|
+
level: number;
|
|
10
|
+
};
|
|
11
|
+
moderator: {
|
|
12
|
+
level: number;
|
|
13
|
+
inherits: string[];
|
|
14
|
+
};
|
|
15
|
+
security: {
|
|
16
|
+
level: number;
|
|
17
|
+
inherits: string[];
|
|
18
|
+
};
|
|
19
|
+
admin: {
|
|
20
|
+
level: number;
|
|
21
|
+
inherits: string[];
|
|
22
|
+
};
|
|
23
|
+
superadmin: {
|
|
24
|
+
level: number;
|
|
25
|
+
inherits: string[];
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
export type PlatformRole = keyof typeof platformDefinitions;
|
|
29
|
+
export declare const PLATFORM_ROLES: readonly PlatformRole[];
|
|
30
|
+
export declare const ROLE_PRIORITY: Readonly<Record<string, number>>;
|
|
31
|
+
/** Unknown roles have no authority and return -1. */
|
|
32
|
+
export declare function rolePriority(role: string | null | undefined): number;
|
|
33
|
+
export declare function effectiveLevel(roles: readonly string[] | null | undefined): number;
|
|
34
|
+
/** Peers may be visible. Unknown targets are always refused. */
|
|
35
|
+
export declare function canSeeUserWithRole(actorRoles: readonly string[], targetRole: string): boolean;
|
|
36
|
+
/** Requires a strictly higher rank; callers separately enforce ownership. */
|
|
37
|
+
export declare function canModerateUserWithRole(actorRoles: readonly string[], targetRole: string): boolean;
|
|
38
|
+
export declare function visibleRolesFor(actorRoles: readonly string[], candidateRoles: readonly string[]): string[];
|
|
39
|
+
/**
|
|
40
|
+
* Assignment choices include peers. First check canModerateUserWithRole against
|
|
41
|
+
* the target's current role before changing it; this list alone is not a gate.
|
|
42
|
+
*/
|
|
43
|
+
export declare function assignableRolesFor(actorRoles: readonly string[], candidateRoles: readonly string[], currentTargetRole?: string | null): string[];
|
|
44
|
+
/**
|
|
45
|
+
* Expand the platform preset's claims. The retired `boss` claim remains a
|
|
46
|
+
* user-only compatibility alias; it has no independent rank or assignability.
|
|
47
|
+
* Other unknown claims, including service_role, pass through without rank.
|
|
48
|
+
*/
|
|
49
|
+
export declare function expandRoles(platformRole: string): string[];
|
|
50
|
+
export declare function expandRoleSet(roles: readonly string[]): string[];
|
|
51
|
+
export {};
|
|
52
|
+
//# sourceMappingURL=platform-roles.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"platform-roles.d.ts","sourceRoot":"","sources":["../src/platform-roles.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AACH,QAAA,MAAM,mBAAmB;IACvB,IAAI;QAAI,KAAK;;IACb,SAAS;QAAI,KAAK;QAAK,QAAQ;;IAC/B,QAAQ;QAAI,KAAK;QAAK,QAAQ;;IAC9B,KAAK;QAAI,KAAK;QAAK,QAAQ;;IAC3B,UAAU;QAAI,KAAK;QAAK,QAAQ;;CACjC,CAAC;AACF,MAAM,MAAM,YAAY,GAAG,MAAM,OAAO,mBAAmB,CAAC;AAC5D,eAAO,MAAM,cAAc,EAAE,SAAS,YAAY,EAAsE,CAAC;AAGzH,eAAO,MAAM,aAAa,kCAAmC,CAAC;AAE9D,qDAAqD;AACrD,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,MAAM,CAEpE;AAED,wBAAgB,cAAc,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,GAAG,IAAI,GAAG,SAAS,GAAG,MAAM,CAElF;AAED,gEAAgE;AAChE,wBAAgB,kBAAkB,CAAC,UAAU,EAAE,SAAS,MAAM,EAAE,EAAE,UAAU,EAAE,MAAM,GAAG,OAAO,CAE7F;AAED,6EAA6E;AAC7E,wBAAgB,uBAAuB,CAAC,UAAU,EAAE,SAAS,MAAM,EAAE,EAAE,UAAU,EAAE,MAAM,GAAG,OAAO,CAElG;AAED,wBAAgB,eAAe,CAAC,UAAU,EAAE,SAAS,MAAM,EAAE,EAAE,cAAc,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,EAAE,CAE1G;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CAChC,UAAU,EAAE,SAAS,MAAM,EAAE,EAAE,cAAc,EAAE,SAAS,MAAM,EAAE,EAAE,iBAAiB,CAAC,EAAE,MAAM,GAAG,IAAI,GAClG,MAAM,EAAE,CAEV;AAED;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,YAAY,EAAE,MAAM,GAAG,MAAM,EAAE,CAE1D;AAED,wBAAgB,aAAa,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,EAAE,CAEhE"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { createRoleHierarchy } from './role-hierarchy.js';
|
|
2
|
+
/**
|
|
3
|
+
* Optional platform preset. Applications with another role vocabulary use
|
|
4
|
+
* createRoleHierarchy; importing the factory does not configure global state.
|
|
5
|
+
* Security and moderation are separate capabilities, even though their ranks
|
|
6
|
+
* differ. Trusted service identities do not belong to this human hierarchy.
|
|
7
|
+
*/
|
|
8
|
+
const platformDefinitions = {
|
|
9
|
+
user: { level: 0 },
|
|
10
|
+
moderator: { level: 1, inherits: ['user'] },
|
|
11
|
+
security: { level: 2, inherits: ['user'] },
|
|
12
|
+
admin: { level: 3, inherits: ['moderator', 'security'] },
|
|
13
|
+
superadmin: { level: 4, inherits: ['admin'] },
|
|
14
|
+
};
|
|
15
|
+
export const PLATFORM_ROLES = Object.freeze(Object.keys(platformDefinitions));
|
|
16
|
+
const platformRoleHierarchy = /* @__PURE__ */ createRoleHierarchy(platformDefinitions);
|
|
17
|
+
export const ROLE_PRIORITY = platformRoleHierarchy.priorities;
|
|
18
|
+
/** Unknown roles have no authority and return -1. */
|
|
19
|
+
export function rolePriority(role) {
|
|
20
|
+
return platformRoleHierarchy.rolePriority(role);
|
|
21
|
+
}
|
|
22
|
+
export function effectiveLevel(roles) {
|
|
23
|
+
return platformRoleHierarchy.effectiveLevel(roles);
|
|
24
|
+
}
|
|
25
|
+
/** Peers may be visible. Unknown targets are always refused. */
|
|
26
|
+
export function canSeeUserWithRole(actorRoles, targetRole) {
|
|
27
|
+
return platformRoleHierarchy.canSeeRole(actorRoles, targetRole);
|
|
28
|
+
}
|
|
29
|
+
/** Requires a strictly higher rank; callers separately enforce ownership. */
|
|
30
|
+
export function canModerateUserWithRole(actorRoles, targetRole) {
|
|
31
|
+
return platformRoleHierarchy.canModerateRole(actorRoles, targetRole);
|
|
32
|
+
}
|
|
33
|
+
export function visibleRolesFor(actorRoles, candidateRoles) {
|
|
34
|
+
return platformRoleHierarchy.visibleRolesFor(actorRoles, candidateRoles);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Assignment choices include peers. First check canModerateUserWithRole against
|
|
38
|
+
* the target's current role before changing it; this list alone is not a gate.
|
|
39
|
+
*/
|
|
40
|
+
export function assignableRolesFor(actorRoles, candidateRoles, currentTargetRole) {
|
|
41
|
+
return platformRoleHierarchy.assignableRolesFor(actorRoles, candidateRoles, currentTargetRole);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Expand the platform preset's claims. The retired `boss` claim remains a
|
|
45
|
+
* user-only compatibility alias; it has no independent rank or assignability.
|
|
46
|
+
* Other unknown claims, including service_role, pass through without rank.
|
|
47
|
+
*/
|
|
48
|
+
export function expandRoles(platformRole) {
|
|
49
|
+
return platformRole === 'boss' ? ['user'] : platformRoleHierarchy.expandRoles(platformRole);
|
|
50
|
+
}
|
|
51
|
+
export function expandRoleSet(roles) {
|
|
52
|
+
return [...new Set(roles.flatMap(expandRoles))];
|
|
53
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/** A project's role rank and explicitly inherited capabilities. */
|
|
2
|
+
export interface RoleDefinition {
|
|
3
|
+
/** Non-negative integer used for visibility, moderation and assignment. */
|
|
4
|
+
readonly level: number;
|
|
5
|
+
/** Roles included in token expansion. Rank alone does not imply inheritance. */
|
|
6
|
+
readonly inherits?: readonly string[];
|
|
7
|
+
/** False keeps this role out of assignment choices. Defaults to true. */
|
|
8
|
+
readonly assignable?: boolean;
|
|
9
|
+
}
|
|
10
|
+
/** Browser-safe policy shared by token issuers, verifiers and user interfaces. */
|
|
11
|
+
export interface RoleHierarchy {
|
|
12
|
+
readonly priorities: Readonly<Record<string, number>>;
|
|
13
|
+
rolePriority(role: string | null | undefined): number;
|
|
14
|
+
effectiveLevel(roles: readonly string[] | null | undefined): number;
|
|
15
|
+
canSeeRole(actorRoles: readonly string[], targetRole: string): boolean;
|
|
16
|
+
canModerateRole(actorRoles: readonly string[], targetRole: string): boolean;
|
|
17
|
+
visibleRolesFor(actorRoles: readonly string[], candidates: readonly string[]): string[];
|
|
18
|
+
assignableRolesFor(actorRoles: readonly string[], candidates: readonly string[], currentRole?: string | null): string[];
|
|
19
|
+
expandRoles(role: string): string[];
|
|
20
|
+
expandRoleSet(roles: readonly string[]): string[];
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Create an isolated, immutable role policy. Unknown roles have no rank and
|
|
24
|
+
* cannot be viewed, moderated or assigned through these helpers. Expansion
|
|
25
|
+
* preserves unknown claims for applications that also use independent roles;
|
|
26
|
+
* preserving a claim never establishes its authority or service identity.
|
|
27
|
+
*
|
|
28
|
+
* Inheritance is explicit and must be acyclic. Use the same definition for
|
|
29
|
+
* issuance, verification and UI affordances; enforce decisions on the server.
|
|
30
|
+
*/
|
|
31
|
+
export declare function createRoleHierarchy(definitions: Readonly<Record<string, RoleDefinition>>): RoleHierarchy;
|
|
32
|
+
//# sourceMappingURL=role-hierarchy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"role-hierarchy.d.ts","sourceRoot":"","sources":["../src/role-hierarchy.ts"],"names":[],"mappings":"AAAA,mEAAmE;AACnE,MAAM,WAAW,cAAc;IAC7B,2EAA2E;IAC3E,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,gFAAgF;IAChF,QAAQ,CAAC,QAAQ,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IACtC,yEAAyE;IACzE,QAAQ,CAAC,UAAU,CAAC,EAAE,OAAO,CAAC;CAC/B;AAED,kFAAkF;AAClF,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,UAAU,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;IACtD,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,MAAM,CAAC;IACtD,cAAc,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,GAAG,IAAI,GAAG,SAAS,GAAG,MAAM,CAAC;IACpE,UAAU,CAAC,UAAU,EAAE,SAAS,MAAM,EAAE,EAAE,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC;IACvE,eAAe,CAAC,UAAU,EAAE,SAAS,MAAM,EAAE,EAAE,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC;IAC5E,eAAe,CAAC,UAAU,EAAE,SAAS,MAAM,EAAE,EAAE,UAAU,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,EAAE,CAAC;IACxF,kBAAkB,CAChB,UAAU,EAAE,SAAS,MAAM,EAAE,EAAE,UAAU,EAAE,SAAS,MAAM,EAAE,EAAE,WAAW,CAAC,EAAE,MAAM,GAAG,IAAI,GACxF,MAAM,EAAE,CAAC;IACZ,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IACpC,aAAa,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,EAAE,CAAC;CACnD;AAED;;;;;;;;GAQG;AACH,wBAAgB,mBAAmB,CAAC,WAAW,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC,GAAG,aAAa,CAwExG"}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Create an isolated, immutable role policy. Unknown roles have no rank and
|
|
3
|
+
* cannot be viewed, moderated or assigned through these helpers. Expansion
|
|
4
|
+
* preserves unknown claims for applications that also use independent roles;
|
|
5
|
+
* preserving a claim never establishes its authority or service identity.
|
|
6
|
+
*
|
|
7
|
+
* Inheritance is explicit and must be acyclic. Use the same definition for
|
|
8
|
+
* issuance, verification and UI affordances; enforce decisions on the server.
|
|
9
|
+
*/
|
|
10
|
+
export function createRoleHierarchy(definitions) {
|
|
11
|
+
const roles = new Map();
|
|
12
|
+
for (const [name, definition] of Object.entries(definitions)) {
|
|
13
|
+
if (!name || name.trim() !== name)
|
|
14
|
+
throw new Error('Role names must be non-empty and trimmed');
|
|
15
|
+
if (!definition || !Number.isSafeInteger(definition.level) || definition.level < 0) {
|
|
16
|
+
throw new Error(`Role "${name}" needs a non-negative safe integer level`);
|
|
17
|
+
}
|
|
18
|
+
if (definition.inherits !== undefined && !Array.isArray(definition.inherits)) {
|
|
19
|
+
throw new Error(`Role "${name}" inherits must be an array of role names`);
|
|
20
|
+
}
|
|
21
|
+
if (definition.assignable !== undefined && typeof definition.assignable !== 'boolean') {
|
|
22
|
+
throw new Error(`Role "${name}" assignable must be a boolean`);
|
|
23
|
+
}
|
|
24
|
+
roles.set(name, {
|
|
25
|
+
level: definition.level,
|
|
26
|
+
inherits: [...(definition.inherits ?? [])],
|
|
27
|
+
assignable: definition.assignable ?? true,
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
if (roles.size === 0)
|
|
31
|
+
throw new Error('A role hierarchy needs at least one role');
|
|
32
|
+
const expansions = new Map();
|
|
33
|
+
const visiting = new Set();
|
|
34
|
+
const expand = (name) => {
|
|
35
|
+
const cached = expansions.get(name);
|
|
36
|
+
if (cached)
|
|
37
|
+
return cached;
|
|
38
|
+
const definition = roles.get(name);
|
|
39
|
+
if (!definition)
|
|
40
|
+
throw new Error(`Unknown inherited role "${name}"`);
|
|
41
|
+
if (visiting.has(name))
|
|
42
|
+
throw new Error(`Role inheritance cycle at "${name}"`);
|
|
43
|
+
visiting.add(name);
|
|
44
|
+
const result = new Set();
|
|
45
|
+
for (const parent of definition.inherits) {
|
|
46
|
+
for (const role of expand(parent))
|
|
47
|
+
result.add(role);
|
|
48
|
+
}
|
|
49
|
+
result.add(name);
|
|
50
|
+
visiting.delete(name);
|
|
51
|
+
const ordered = Object.freeze([...result]);
|
|
52
|
+
expansions.set(name, ordered);
|
|
53
|
+
return ordered;
|
|
54
|
+
};
|
|
55
|
+
for (const name of roles.keys())
|
|
56
|
+
expand(name);
|
|
57
|
+
const rolePriority = (role) => role == null ? -1 : (roles.get(role)?.level ?? -1);
|
|
58
|
+
const effectiveLevel = (claims) => {
|
|
59
|
+
let level = -1;
|
|
60
|
+
for (const role of claims ?? [])
|
|
61
|
+
level = Math.max(level, rolePriority(role));
|
|
62
|
+
return level;
|
|
63
|
+
};
|
|
64
|
+
const canSeeRole = (actor, target) => {
|
|
65
|
+
const targetLevel = rolePriority(target);
|
|
66
|
+
return targetLevel >= 0 && effectiveLevel(actor) >= targetLevel;
|
|
67
|
+
};
|
|
68
|
+
const canModerateRole = (actor, target) => {
|
|
69
|
+
const targetLevel = rolePriority(target);
|
|
70
|
+
return targetLevel >= 0 && effectiveLevel(actor) > targetLevel;
|
|
71
|
+
};
|
|
72
|
+
const expandRoles = (role) => [...(expansions.get(role) ?? [role])];
|
|
73
|
+
return Object.freeze({
|
|
74
|
+
priorities: Object.freeze(Object.assign(Object.create(null), Object.fromEntries([...roles].map(([name, role]) => [name, role.level])))),
|
|
75
|
+
rolePriority,
|
|
76
|
+
effectiveLevel,
|
|
77
|
+
canSeeRole,
|
|
78
|
+
canModerateRole,
|
|
79
|
+
visibleRolesFor: (actor, candidates) => candidates.filter(role => canSeeRole(actor, role)),
|
|
80
|
+
assignableRolesFor: (actor, candidates, currentRole) => candidates.filter(role => role !== currentRole && roles.get(role)?.assignable === true && canSeeRole(actor, role)),
|
|
81
|
+
expandRoles,
|
|
82
|
+
expandRoleSet: (claims) => [...new Set(claims.flatMap(expandRoles))],
|
|
83
|
+
});
|
|
84
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/** Transport- and storage-independent constraints on an authenticated session. */
|
|
2
|
+
export interface SessionPolicy {
|
|
3
|
+
requireMfa?: boolean;
|
|
4
|
+
/** Absolute session age in minutes; renewing a token does not reset it. */
|
|
5
|
+
maxSessionDuration?: number;
|
|
6
|
+
timeWindows?: Array<{
|
|
7
|
+
days: number[];
|
|
8
|
+
startHour: number;
|
|
9
|
+
endHour: number;
|
|
10
|
+
timezone: string;
|
|
11
|
+
}>;
|
|
12
|
+
}
|
|
13
|
+
export interface SessionFacts {
|
|
14
|
+
hasMfa: boolean;
|
|
15
|
+
createdAt: number;
|
|
16
|
+
now?: number;
|
|
17
|
+
}
|
|
18
|
+
export type SessionPolicyDecision = {
|
|
19
|
+
allowed: true;
|
|
20
|
+
} | {
|
|
21
|
+
allowed: false;
|
|
22
|
+
reason: 'invalid_policy' | 'mfa_required' | 'session_expired' | 'outside_time_window';
|
|
23
|
+
};
|
|
24
|
+
/** Unknown/malformed controls fail closed. Hours use each window's IANA timezone. */
|
|
25
|
+
export declare function evaluateSessionPolicy(policy: SessionPolicy | null, facts: SessionFacts): SessionPolicyDecision;
|
|
26
|
+
//# sourceMappingURL=session-policy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session-policy.d.ts","sourceRoot":"","sources":["../src/session-policy.ts"],"names":[],"mappings":"AAAA,kFAAkF;AAClF,MAAM,WAAW,aAAa;IAC5B,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,2EAA2E;IAC3E,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B,WAAW,CAAC,EAAE,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,EAAE,CAAC;QAAC,SAAS,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;CAC/F;AACD,MAAM,WAAW,YAAY;IAC3B,MAAM,EAAE,OAAO,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;IAClB,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AACD,MAAM,MAAM,qBAAqB,GAAG;IAAE,OAAO,EAAE,IAAI,CAAA;CAAE,GAAG;IACtD,OAAO,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,gBAAgB,GAAG,cAAc,GAAG,iBAAiB,GAAG,qBAAqB,CAAC;CACvG,CAAC;AAEF,qFAAqF;AACrF,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,aAAa,GAAG,IAAI,EAAE,KAAK,EAAE,YAAY,GAAG,qBAAqB,CAqC9G"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/** Unknown/malformed controls fail closed. Hours use each window's IANA timezone. */
|
|
2
|
+
export function evaluateSessionPolicy(policy, facts) {
|
|
3
|
+
if (policy === null)
|
|
4
|
+
return { allowed: true };
|
|
5
|
+
const now = facts.now ?? Date.now();
|
|
6
|
+
if (!policy || typeof policy !== 'object' || Array.isArray(policy) || !Number.isFinite(now) ||
|
|
7
|
+
Object.keys(policy).some(key => !['requireMfa', 'maxSessionDuration', 'timeWindows'].includes(key)) ||
|
|
8
|
+
(policy.requireMfa !== undefined && typeof policy.requireMfa !== 'boolean') ||
|
|
9
|
+
(policy.maxSessionDuration !== undefined && (!Number.isSafeInteger(policy.maxSessionDuration) || policy.maxSessionDuration <= 0)))
|
|
10
|
+
return { allowed: false, reason: 'invalid_policy' };
|
|
11
|
+
if (policy.requireMfa && !facts.hasMfa)
|
|
12
|
+
return { allowed: false, reason: 'mfa_required' };
|
|
13
|
+
if (policy.maxSessionDuration !== undefined && (!Number.isFinite(facts.createdAt) || facts.createdAt > now ||
|
|
14
|
+
now - facts.createdAt >= policy.maxSessionDuration * 60_000))
|
|
15
|
+
return { allowed: false, reason: 'session_expired' };
|
|
16
|
+
if (policy.timeWindows !== undefined) {
|
|
17
|
+
if (!Array.isArray(policy.timeWindows) || policy.timeWindows.length > 64)
|
|
18
|
+
return { allowed: false, reason: 'invalid_policy' };
|
|
19
|
+
let matches = false;
|
|
20
|
+
for (const window of policy.timeWindows) {
|
|
21
|
+
if (!window || !Array.isArray(window.days) || window.days.length > 7 ||
|
|
22
|
+
window.days.some(day => !Number.isInteger(day) || day < 0 || day > 6) ||
|
|
23
|
+
!Number.isInteger(window.startHour) || window.startHour < 0 || window.startHour > 23 ||
|
|
24
|
+
!Number.isInteger(window.endHour) || window.endHour < 0 || window.endHour > 24 ||
|
|
25
|
+
window.startHour === window.endHour || typeof window.timezone !== 'string' || !window.timezone)
|
|
26
|
+
return { allowed: false, reason: 'invalid_policy' };
|
|
27
|
+
try {
|
|
28
|
+
const parts = new Intl.DateTimeFormat('en-US', { timeZone: window.timezone,
|
|
29
|
+
weekday: 'short', hour: 'numeric', hourCycle: 'h23' }).formatToParts(now);
|
|
30
|
+
const day = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'].indexOf(parts.find(p => p.type === 'weekday').value);
|
|
31
|
+
const hour = Number(parts.find(p => p.type === 'hour').value);
|
|
32
|
+
matches ||= window.startHour < window.endHour
|
|
33
|
+
? window.days.includes(day) && hour >= window.startHour && hour < window.endHour
|
|
34
|
+
: (window.days.includes(day) && hour >= window.startHour) ||
|
|
35
|
+
(window.days.includes((day + 6) % 7) && hour < window.endHour);
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
return { allowed: false, reason: 'invalid_policy' };
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
if (policy.timeWindows.length && !matches)
|
|
42
|
+
return { allowed: false, reason: 'outside_time_window' };
|
|
43
|
+
}
|
|
44
|
+
return { allowed: true };
|
|
45
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,62 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@omnitron-dev/access",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.3.2",
|
|
4
|
+
"description": "Dependency-free role hierarchies and permission policies for browsers and servers",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"main": "./dist/index.js",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"import": "./dist/index.js",
|
|
14
|
+
"default": "./dist/index.js"
|
|
15
|
+
},
|
|
16
|
+
"./roles": {
|
|
17
|
+
"types": "./dist/role-hierarchy.d.ts",
|
|
18
|
+
"import": "./dist/role-hierarchy.js",
|
|
19
|
+
"default": "./dist/role-hierarchy.js"
|
|
20
|
+
},
|
|
21
|
+
"./permissions": {
|
|
22
|
+
"types": "./dist/permissions.d.ts",
|
|
23
|
+
"import": "./dist/permissions.js",
|
|
24
|
+
"default": "./dist/permissions.js"
|
|
25
|
+
},
|
|
26
|
+
"./platform": {
|
|
27
|
+
"types": "./dist/platform-roles.d.ts",
|
|
28
|
+
"import": "./dist/platform-roles.js",
|
|
29
|
+
"default": "./dist/platform-roles.js"
|
|
30
|
+
},
|
|
31
|
+
"./session-policy": {
|
|
32
|
+
"types": "./dist/session-policy.d.ts",
|
|
33
|
+
"import": "./dist/session-policy.js",
|
|
34
|
+
"default": "./dist/session-policy.js"
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"files": [
|
|
38
|
+
"dist",
|
|
39
|
+
"README.md"
|
|
40
|
+
],
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"esbuild": "^0.28.2",
|
|
43
|
+
"typescript": "npm:@typescript/typescript6@^6.0.2",
|
|
44
|
+
"vitest": "^5.0.5",
|
|
45
|
+
"@typescript/native": "npm:typescript@^7.0.2"
|
|
46
|
+
},
|
|
47
|
+
"engines": {
|
|
48
|
+
"node": ">=18"
|
|
49
|
+
},
|
|
50
|
+
"repository": {
|
|
51
|
+
"type": "git",
|
|
52
|
+
"url": "git+https://github.com/omnitron-dev/omni.git",
|
|
53
|
+
"directory": "packages/access"
|
|
54
|
+
},
|
|
55
|
+
"gitHead": "ecf537907f8e7efa75a56012a96c84c451ced77b",
|
|
56
|
+
"scripts": {
|
|
57
|
+
"build": "../../scripts/build-package.sh -p ./tsconfig.json",
|
|
58
|
+
"typecheck": "tsc --noEmit",
|
|
59
|
+
"test": "vitest run",
|
|
60
|
+
"check:bundle": "node scripts/check-bundle.mjs"
|
|
61
|
+
}
|
|
6
62
|
}
|