@arikajs/authorization 0.1.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.

Potentially problematic release.


This version of @arikajs/authorization might be problematic. Click here for more details.

package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 ArikaJs
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 ADDED
@@ -0,0 +1,232 @@
1
+ ## Arika Authorization
2
+
3
+ `@arikajs/authorization` provides authorization (access control) for the ArikaJS framework.
4
+
5
+ It answers one critical question: **“Is the authenticated user allowed to perform this action?”**
6
+
7
+ This package provides a powerful authorization system with Gates and Policies, designed specifically for a modular, TypeScript-first Node.js framework.
8
+
9
+ ---
10
+
11
+ ### Status
12
+
13
+ - **Stage**: Experimental / v0.x
14
+ - **Scope**:
15
+ - Gate-based authorization
16
+ - Policy-based authorization
17
+ - Authorization middleware
18
+ - Centralized permission logic
19
+ - **Design**:
20
+ - Framework-agnostic (usable outside HTTP layer)
21
+ - Decoupled from transport layer
22
+ - Type-safe APIs
23
+
24
+ ---
25
+
26
+ ## ✨ Purpose
27
+
28
+ Authorization is not authentication.
29
+
30
+ - **Authentication** → Who is the user? (Handled by `@arikajs/auth`)
31
+ - **Authorization** → What can the user do? (Handled by `@arikajs/authorization`)
32
+
33
+ This package works on top of `@arikajs/auth` but remains fully decoupled from it.
34
+
35
+ ---
36
+
37
+ ## 🚀 Features
38
+
39
+ - **Gate-based authorization**: Simple closure-based checks.
40
+ - **Policy-based authorization**: Organize logic per model/resource.
41
+ - **Middleware integration**: Protect routes easily.
42
+ - **Controller & service-level checks**: Call authorization logic from anywhere.
43
+ - **Type-safe APIs**: Built for TypeScript.
44
+ - **Framework-agnostic**: Can be used in CLI, WebSocket, or HTTP contexts.
45
+
46
+ ---
47
+
48
+ ## 📦 Installation
49
+
50
+ ```bash
51
+ npm install @arikajs/authorization
52
+ # or
53
+ yarn add @arikajs/authorization
54
+ # or
55
+ pnpm add @arikajs/authorization
56
+ ```
57
+
58
+ ---
59
+
60
+ ## 🧠 Core Concepts
61
+
62
+ ### 1️⃣ Gates
63
+
64
+ Gates are simple ability checks defined globally.
65
+
66
+ ```ts
67
+ import { Gate } from '@arikajs/authorization';
68
+
69
+ Gate.define('edit-post', (user, post) => {
70
+ return user.id === post.userId;
71
+ });
72
+ ```
73
+
74
+ **Usage:**
75
+
76
+ ```ts
77
+ if (Gate.allows('edit-post', post)) {
78
+ // ...
79
+ }
80
+
81
+ if (Gate.denies('edit-post', post)) {
82
+ // ...
83
+ }
84
+ ```
85
+
86
+ ### 2️⃣ Policies
87
+
88
+ Policies organize authorization logic around a specific model or resource.
89
+
90
+ ```ts
91
+ class PostPolicy {
92
+ view(user, post) {
93
+ return true;
94
+ }
95
+
96
+ update(user, post) {
97
+ return user.id === post.userId;
98
+ }
99
+ }
100
+ ```
101
+
102
+ **Register Policy:**
103
+
104
+ ```ts
105
+ Gate.policy(Post, PostPolicy);
106
+ ```
107
+
108
+ **Usage:**
109
+
110
+ ```ts
111
+ // Automatically resolves to PostPolicy.update
112
+ Gate.allows('update', post);
113
+ ```
114
+
115
+ ### 3️⃣ Authorization Manager
116
+
117
+ The central engine that evaluates permissions.
118
+
119
+ ```ts
120
+ const authz = new AuthorizationManager(user);
121
+
122
+ authz.can('edit-post', post);
123
+ authz.cannot('delete-post', post);
124
+ ```
125
+
126
+ ---
127
+
128
+ ## 🧩 Middleware Support
129
+
130
+ Protect routes using authorization middleware:
131
+
132
+ ```ts
133
+ Route.get('/posts/:id/edit', controller)
134
+ .middleware('can:edit-post');
135
+ ```
136
+
137
+ WITH arguments (e.g. Policies):
138
+
139
+ ```ts
140
+ .middleware('can:update,post');
141
+ ```
142
+
143
+ Middleware automatically:
144
+ 1. Resolves the authenticated user.
145
+ 2. Executes the authorization check.
146
+ 3. Throws `403 Forbidden` on failure.
147
+
148
+ ---
149
+
150
+ ## 🧑💻 Controller Usage
151
+
152
+ ```ts
153
+ class PostController {
154
+ update(request) {
155
+ Gate.authorize('update', request.post);
156
+
157
+ // Authorized logic proceeds here...
158
+ }
159
+ }
160
+ ```
161
+
162
+ If unauthorized, it throws an `AuthorizationException`, which is automatically handled by the HTTP layer.
163
+
164
+ ---
165
+
166
+ ## 🏗 Architecture
167
+
168
+ ```
169
+ authorization/
170
+ ├── src/
171
+ │ ├── Gate.ts ← Define & evaluate abilities
172
+ │ ├── AuthorizationManager.ts ← Core authorization engine
173
+ │ ├── PolicyResolver.ts ← Maps models to policies
174
+ │ ├── Middleware/
175
+ │ │ └── Authorize.ts ← Route-level protection
176
+ │ ├── Exceptions/
177
+ │ │ └── AuthorizationException.ts
178
+ │ ├── Contracts/
179
+ │ │ └── Policy.ts
180
+ │ └── index.ts
181
+ ├── package.json
182
+ ├── tsconfig.json
183
+ ├── README.md
184
+ └── LICENSE
185
+ ```
186
+
187
+ ---
188
+
189
+ ## 🔌 Integration with ArikaJS
190
+
191
+ | Package | Responsibility |
192
+ | :--- | :--- |
193
+ | `@arikajs/auth` | User authentication |
194
+ | `@arikajs/authorization` | Access control |
195
+ | `@arikajs/router` | Route matching |
196
+ | `@arikajs/http` | Request & response handling |
197
+
198
+ ---
199
+
200
+ ## 🧪 Error Handling
201
+
202
+ Unauthorized access throws:
203
+ - `AuthorizationException` (403)
204
+
205
+ Handled automatically by:
206
+ - HTTP kernel
207
+ - Middleware pipeline
208
+
209
+ ---
210
+
211
+ ## 📌 Design Philosophy
212
+
213
+ - **Explicit over implicit**: Authorization rules should be clear.
214
+ - **Centralized rules**: Keep logic in Gates or Policies, not controllers.
215
+ - **Readable permission names**: Use descriptive names like `edit-post`.
216
+ - **Decoupled from transport layer**: Logic works for HTTP, CLI, etc.
217
+
218
+ > “Authentication identifies the user. Authorization empowers or restricts them.”
219
+
220
+ ---
221
+
222
+ ## 🔄 Versioning & Stability
223
+
224
+ - Current version: **v0.x** (Experimental)
225
+ - API may change until **v1.0**
226
+ - Will follow semantic versioning after stabilization
227
+
228
+ ---
229
+
230
+ ## 📜 License
231
+
232
+ `@arikajs/authorization` is open-sourced software licensed under the **MIT License**.
@@ -0,0 +1,17 @@
1
+ export declare class AuthorizationManager {
2
+ private user;
3
+ constructor(user: any);
4
+ /**
5
+ * Determine if the user can perform the given ability.
6
+ */
7
+ can(ability: string, ...args: any[]): Promise<boolean>;
8
+ /**
9
+ * Determine if the user cannot perform the given ability.
10
+ */
11
+ cannot(ability: string, ...args: any[]): Promise<boolean>;
12
+ /**
13
+ * Authorize or throw exception.
14
+ */
15
+ authorize(ability: string, ...args: any[]): Promise<void>;
16
+ }
17
+ //# sourceMappingURL=AuthorizationManager.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AuthorizationManager.d.ts","sourceRoot":"","sources":["../src/AuthorizationManager.ts"],"names":[],"mappings":"AAAA,qBAAa,oBAAoB;IAC7B,OAAO,CAAC,IAAI,CAAM;gBAEN,IAAI,EAAE,GAAG;IAIrB;;OAEG;IACU,GAAG,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC,OAAO,CAAC;IAKnE;;OAEG;IACU,MAAM,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC,OAAO,CAAC;IAItE;;OAEG;IACU,SAAS,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;CAIzE"}
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AuthorizationManager = void 0;
4
+ class AuthorizationManager {
5
+ constructor(user) {
6
+ this.user = user;
7
+ }
8
+ /**
9
+ * Determine if the user can perform the given ability.
10
+ */
11
+ async can(ability, ...args) {
12
+ const { Gate } = require('./Gate');
13
+ return await Gate.forUser(this.user).allows(ability, ...args);
14
+ }
15
+ /**
16
+ * Determine if the user cannot perform the given ability.
17
+ */
18
+ async cannot(ability, ...args) {
19
+ return !(await this.can(ability, ...args));
20
+ }
21
+ /**
22
+ * Authorize or throw exception.
23
+ */
24
+ async authorize(ability, ...args) {
25
+ const { Gate } = require('./Gate');
26
+ await Gate.forUser(this.user).authorize(ability, ...args);
27
+ }
28
+ }
29
+ exports.AuthorizationManager = AuthorizationManager;
30
+ //# sourceMappingURL=AuthorizationManager.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AuthorizationManager.js","sourceRoot":"","sources":["../src/AuthorizationManager.ts"],"names":[],"mappings":";;;AAAA,MAAa,oBAAoB;IAG7B,YAAY,IAAS;QACjB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACrB,CAAC;IAED;;OAEG;IACI,KAAK,CAAC,GAAG,CAAC,OAAe,EAAE,GAAG,IAAW;QAC5C,MAAM,EAAE,IAAI,EAAE,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC;QACnC,OAAO,MAAM,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,OAAO,EAAE,GAAG,IAAI,CAAC,CAAC;IAClE,CAAC;IAED;;OAEG;IACI,KAAK,CAAC,MAAM,CAAC,OAAe,EAAE,GAAG,IAAW;QAC/C,OAAO,CAAC,CAAC,MAAM,IAAI,CAAC,GAAG,CAAC,OAAO,EAAE,GAAG,IAAI,CAAC,CAAC,CAAC;IAC/C,CAAC;IAED;;OAEG;IACI,KAAK,CAAC,SAAS,CAAC,OAAe,EAAE,GAAG,IAAW;QAClD,MAAM,EAAE,IAAI,EAAE,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC;QACnC,MAAM,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,SAAS,CAAC,OAAO,EAAE,GAAG,IAAI,CAAC,CAAC;IAC9D,CAAC;CACJ;AA7BD,oDA6BC"}
@@ -0,0 +1,4 @@
1
+ export interface Policy {
2
+ [method: string]: ((user: any, ...args: any[]) => boolean | Promise<boolean>) | any;
3
+ }
4
+ //# sourceMappingURL=Policy.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Policy.d.ts","sourceRoot":"","sources":["../../src/Contracts/Policy.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,MAAM;IACnB,CAAC,MAAM,EAAE,MAAM,GAAG,CAAC,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC,GAAG,GAAG,CAAC;CACvF"}
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=Policy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Policy.js","sourceRoot":"","sources":["../../src/Contracts/Policy.ts"],"names":[],"mappings":""}
@@ -0,0 +1,5 @@
1
+ export declare class AuthorizationException extends Error {
2
+ statusCode: number;
3
+ constructor(message?: string);
4
+ }
5
+ //# sourceMappingURL=AuthorizationException.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AuthorizationException.d.ts","sourceRoot":"","sources":["../../src/Exceptions/AuthorizationException.ts"],"names":[],"mappings":"AAAA,qBAAa,sBAAuB,SAAQ,KAAK;IACtC,UAAU,EAAE,MAAM,CAAO;gBAEpB,OAAO,GAAE,MAAuC;CAI/D"}
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AuthorizationException = void 0;
4
+ class AuthorizationException extends Error {
5
+ constructor(message = 'This action is unauthorized.') {
6
+ super(message);
7
+ this.statusCode = 403;
8
+ this.name = 'AuthorizationException';
9
+ }
10
+ }
11
+ exports.AuthorizationException = AuthorizationException;
12
+ //# sourceMappingURL=AuthorizationException.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AuthorizationException.js","sourceRoot":"","sources":["../../src/Exceptions/AuthorizationException.ts"],"names":[],"mappings":";;;AAAA,MAAa,sBAAuB,SAAQ,KAAK;IAG7C,YAAY,UAAkB,8BAA8B;QACxD,KAAK,CAAC,OAAO,CAAC,CAAC;QAHZ,eAAU,GAAW,GAAG,CAAC;QAI5B,IAAI,CAAC,IAAI,GAAG,wBAAwB,CAAC;IACzC,CAAC;CACJ;AAPD,wDAOC"}
package/dist/Gate.d.ts ADDED
@@ -0,0 +1,40 @@
1
+ type GateCallback = (user: any, ...args: any[]) => boolean | Promise<boolean>;
2
+ export declare class Gate {
3
+ private static abilities;
4
+ private static policyResolver;
5
+ private static currentUser;
6
+ /**
7
+ * Define a new ability.
8
+ */
9
+ static define(ability: string, callback: GateCallback): void;
10
+ /**
11
+ * Register a policy for a model.
12
+ */
13
+ static policy(model: any, policy: any): void;
14
+ /**
15
+ * Set the current user for authorization checks.
16
+ */
17
+ static forUser(user: any): typeof Gate;
18
+ /**
19
+ * Determine if the user is authorized to perform an ability.
20
+ */
21
+ static allows(ability: string, ...args: any[]): Promise<boolean>;
22
+ /**
23
+ * Determine if the user is NOT authorized.
24
+ */
25
+ static denies(ability: string, ...args: any[]): Promise<boolean>;
26
+ /**
27
+ * Authorize or throw exception.
28
+ */
29
+ static authorize(ability: string, ...args: any[]): Promise<void>;
30
+ /**
31
+ * Check authorization (alias for allows).
32
+ */
33
+ static check(ability: string, ...args: any[]): Promise<boolean>;
34
+ /**
35
+ * Reset all gates and policies (useful for testing).
36
+ */
37
+ static reset(): void;
38
+ }
39
+ export {};
40
+ //# sourceMappingURL=Gate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Gate.d.ts","sourceRoot":"","sources":["../src/Gate.ts"],"names":[],"mappings":"AAGA,KAAK,YAAY,GAAG,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;AAE9E,qBAAa,IAAI;IACb,OAAO,CAAC,MAAM,CAAC,SAAS,CAAwC;IAChE,OAAO,CAAC,MAAM,CAAC,cAAc,CAAwC;IACrE,OAAO,CAAC,MAAM,CAAC,WAAW,CAAa;IAEvC;;OAEG;WACW,MAAM,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,YAAY,GAAG,IAAI;IAInE;;OAEG;WACW,MAAM,CAAC,KAAK,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,GAAG,IAAI;IAInD;;OAEG;WACW,OAAO,CAAC,IAAI,EAAE,GAAG,GAAG,OAAO,IAAI;IAK7C;;OAEG;WACiB,MAAM,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC,OAAO,CAAC;IAwB7E;;OAEG;WACiB,MAAM,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC,OAAO,CAAC;IAI7E;;OAEG;WACiB,SAAS,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAM7E;;OAEG;WACiB,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC,OAAO,CAAC;IAI5E;;OAEG;WACW,KAAK,IAAI,IAAI;CAK9B"}
package/dist/Gate.js ADDED
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Gate = void 0;
4
+ const PolicyResolver_1 = require("./PolicyResolver");
5
+ const AuthorizationException_1 = require("./Exceptions/AuthorizationException");
6
+ class Gate {
7
+ /**
8
+ * Define a new ability.
9
+ */
10
+ static define(ability, callback) {
11
+ this.abilities.set(ability, callback);
12
+ }
13
+ /**
14
+ * Register a policy for a model.
15
+ */
16
+ static policy(model, policy) {
17
+ this.policyResolver.register(model, policy);
18
+ }
19
+ /**
20
+ * Set the current user for authorization checks.
21
+ */
22
+ static forUser(user) {
23
+ this.currentUser = user;
24
+ return this;
25
+ }
26
+ /**
27
+ * Determine if the user is authorized to perform an ability.
28
+ */
29
+ static async allows(ability, ...args) {
30
+ // 1. Check if it's a direct gate definition
31
+ if (this.abilities.has(ability)) {
32
+ const callback = this.abilities.get(ability);
33
+ return await callback(this.currentUser, ...args);
34
+ }
35
+ // 2. Check if it's a policy method
36
+ if (args.length > 0) {
37
+ const resource = args[0];
38
+ const policy = this.policyResolver.resolvePolicy(resource);
39
+ if (policy) {
40
+ const method = this.policyResolver.getPolicyMethod(policy, ability);
41
+ if (method) {
42
+ return await method(this.currentUser, ...args);
43
+ }
44
+ }
45
+ }
46
+ // 3. Default deny
47
+ return false;
48
+ }
49
+ /**
50
+ * Determine if the user is NOT authorized.
51
+ */
52
+ static async denies(ability, ...args) {
53
+ return !(await this.allows(ability, ...args));
54
+ }
55
+ /**
56
+ * Authorize or throw exception.
57
+ */
58
+ static async authorize(ability, ...args) {
59
+ if (!(await this.allows(ability, ...args))) {
60
+ throw new AuthorizationException_1.AuthorizationException();
61
+ }
62
+ }
63
+ /**
64
+ * Check authorization (alias for allows).
65
+ */
66
+ static async check(ability, ...args) {
67
+ return await this.allows(ability, ...args);
68
+ }
69
+ /**
70
+ * Reset all gates and policies (useful for testing).
71
+ */
72
+ static reset() {
73
+ this.abilities.clear();
74
+ this.policyResolver = new PolicyResolver_1.PolicyResolver();
75
+ this.currentUser = null;
76
+ }
77
+ }
78
+ exports.Gate = Gate;
79
+ Gate.abilities = new Map();
80
+ Gate.policyResolver = new PolicyResolver_1.PolicyResolver();
81
+ Gate.currentUser = null;
82
+ //# sourceMappingURL=Gate.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Gate.js","sourceRoot":"","sources":["../src/Gate.ts"],"names":[],"mappings":";;;AAAA,qDAAkD;AAClD,gFAA6E;AAI7E,MAAa,IAAI;IAKb;;OAEG;IACI,MAAM,CAAC,MAAM,CAAC,OAAe,EAAE,QAAsB;QACxD,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;IAC1C,CAAC;IAED;;OAEG;IACI,MAAM,CAAC,MAAM,CAAC,KAAU,EAAE,MAAW;QACxC,IAAI,CAAC,cAAc,CAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;IAChD,CAAC;IAED;;OAEG;IACI,MAAM,CAAC,OAAO,CAAC,IAAS;QAC3B,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC;QACxB,OAAO,IAAI,CAAC;IAChB,CAAC;IAED;;OAEG;IACI,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,OAAe,EAAE,GAAG,IAAW;QACtD,4CAA4C;QAC5C,IAAI,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,CAAC;YAC9B,MAAM,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,OAAO,CAAE,CAAC;YAC9C,OAAO,MAAM,QAAQ,CAAC,IAAI,CAAC,WAAW,EAAE,GAAG,IAAI,CAAC,CAAC;QACrD,CAAC;QAED,mCAAmC;QACnC,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YAClB,MAAM,QAAQ,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC;YACzB,MAAM,MAAM,GAAG,IAAI,CAAC,cAAc,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;YAE3D,IAAI,MAAM,EAAE,CAAC;gBACT,MAAM,MAAM,GAAG,IAAI,CAAC,cAAc,CAAC,eAAe,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;gBACpE,IAAI,MAAM,EAAE,CAAC;oBACT,OAAO,MAAM,MAAM,CAAC,IAAI,CAAC,WAAW,EAAE,GAAG,IAAI,CAAC,CAAC;gBACnD,CAAC;YACL,CAAC;QACL,CAAC;QAED,kBAAkB;QAClB,OAAO,KAAK,CAAC;IACjB,CAAC;IAED;;OAEG;IACI,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,OAAe,EAAE,GAAG,IAAW;QACtD,OAAO,CAAC,CAAC,MAAM,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,GAAG,IAAI,CAAC,CAAC,CAAC;IAClD,CAAC;IAED;;OAEG;IACI,MAAM,CAAC,KAAK,CAAC,SAAS,CAAC,OAAe,EAAE,GAAG,IAAW;QACzD,IAAI,CAAC,CAAC,MAAM,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,GAAG,IAAI,CAAC,CAAC,EAAE,CAAC;YACzC,MAAM,IAAI,+CAAsB,EAAE,CAAC;QACvC,CAAC;IACL,CAAC;IAED;;OAEG;IACI,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,OAAe,EAAE,GAAG,IAAW;QACrD,OAAO,MAAM,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,GAAG,IAAI,CAAC,CAAC;IAC/C,CAAC;IAED;;OAEG;IACI,MAAM,CAAC,KAAK;QACf,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,CAAC;QACvB,IAAI,CAAC,cAAc,GAAG,IAAI,+BAAc,EAAE,CAAC;QAC3C,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC;IAC5B,CAAC;;AApFL,oBAqFC;AApFkB,cAAS,GAA8B,IAAI,GAAG,EAAE,CAAC;AACjD,mBAAc,GAAmB,IAAI,+BAAc,EAAE,CAAC;AACtD,gBAAW,GAAQ,IAAI,CAAC"}
@@ -0,0 +1,12 @@
1
+ export declare class Authorize {
2
+ /**
3
+ * Handle authorization middleware.
4
+ *
5
+ * @param request - The request object (should have user attached)
6
+ * @param next - The next middleware function
7
+ * @param ability - The ability to check (e.g., 'edit-post' or 'update')
8
+ * @param resourceKey - Optional resource key from request (e.g., 'post')
9
+ */
10
+ handle(request: any, next: () => Promise<any>, ability: string, resourceKey?: string): Promise<any>;
11
+ }
12
+ //# sourceMappingURL=Authorize.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Authorize.d.ts","sourceRoot":"","sources":["../../src/Middleware/Authorize.ts"],"names":[],"mappings":"AAGA,qBAAa,SAAS;IAClB;;;;;;;OAOG;IACU,MAAM,CACf,OAAO,EAAE,GAAG,EACZ,IAAI,EAAE,MAAM,OAAO,CAAC,GAAG,CAAC,EACxB,OAAO,EAAE,MAAM,EACf,WAAW,CAAC,EAAE,MAAM,GACrB,OAAO,CAAC,GAAG,CAAC;CAoBlB"}
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Authorize = void 0;
4
+ const Gate_1 = require("../Gate");
5
+ const AuthorizationException_1 = require("../Exceptions/AuthorizationException");
6
+ class Authorize {
7
+ /**
8
+ * Handle authorization middleware.
9
+ *
10
+ * @param request - The request object (should have user attached)
11
+ * @param next - The next middleware function
12
+ * @param ability - The ability to check (e.g., 'edit-post' or 'update')
13
+ * @param resourceKey - Optional resource key from request (e.g., 'post')
14
+ */
15
+ async handle(request, next, ability, resourceKey) {
16
+ const user = request.user;
17
+ if (!user) {
18
+ throw new AuthorizationException_1.AuthorizationException('User not authenticated.');
19
+ }
20
+ Gate_1.Gate.forUser(user);
21
+ // If resourceKey is provided, get resource from request
22
+ const resource = resourceKey ? request[resourceKey] : null;
23
+ if (resource) {
24
+ await Gate_1.Gate.authorize(ability, resource);
25
+ }
26
+ else {
27
+ await Gate_1.Gate.authorize(ability);
28
+ }
29
+ return next();
30
+ }
31
+ }
32
+ exports.Authorize = Authorize;
33
+ //# sourceMappingURL=Authorize.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Authorize.js","sourceRoot":"","sources":["../../src/Middleware/Authorize.ts"],"names":[],"mappings":";;;AAAA,kCAA+B;AAC/B,iFAA8E;AAE9E,MAAa,SAAS;IAClB;;;;;;;OAOG;IACI,KAAK,CAAC,MAAM,CACf,OAAY,EACZ,IAAwB,EACxB,OAAe,EACf,WAAoB;QAEpB,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;QAE1B,IAAI,CAAC,IAAI,EAAE,CAAC;YACR,MAAM,IAAI,+CAAsB,CAAC,yBAAyB,CAAC,CAAC;QAChE,CAAC;QAED,WAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QAEnB,wDAAwD;QACxD,MAAM,QAAQ,GAAG,WAAW,CAAC,CAAC,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;QAE3D,IAAI,QAAQ,EAAE,CAAC;YACX,MAAM,WAAI,CAAC,SAAS,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;QAC5C,CAAC;aAAM,CAAC;YACJ,MAAM,WAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;QAClC,CAAC;QAED,OAAO,IAAI,EAAE,CAAC;IAClB,CAAC;CACJ;AAlCD,8BAkCC"}
@@ -0,0 +1,16 @@
1
+ export declare class PolicyResolver {
2
+ private policies;
3
+ /**
4
+ * Register a policy for a given model/class.
5
+ */
6
+ register(model: any, policy: any): void;
7
+ /**
8
+ * Resolve the policy for a given resource.
9
+ */
10
+ resolvePolicy(resource: any): any | null;
11
+ /**
12
+ * Get policy method for ability.
13
+ */
14
+ getPolicyMethod(policy: any, ability: string): Function | null;
15
+ }
16
+ //# sourceMappingURL=PolicyResolver.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PolicyResolver.d.ts","sourceRoot":"","sources":["../src/PolicyResolver.ts"],"names":[],"mappings":"AAEA,qBAAa,cAAc;IACvB,OAAO,CAAC,QAAQ,CAA4B;IAE5C;;OAEG;IACI,QAAQ,CAAC,KAAK,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,GAAG,IAAI;IAI9C;;OAEG;IACI,aAAa,CAAC,QAAQ,EAAE,GAAG,GAAG,GAAG,GAAG,IAAI;IAiB/C;;OAEG;IACI,eAAe,CAAC,MAAM,EAAE,GAAG,EAAE,OAAO,EAAE,MAAM,GAAG,QAAQ,GAAG,IAAI;CAWxE"}
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PolicyResolver = void 0;
4
+ class PolicyResolver {
5
+ constructor() {
6
+ this.policies = new Map();
7
+ }
8
+ /**
9
+ * Register a policy for a given model/class.
10
+ */
11
+ register(model, policy) {
12
+ this.policies.set(model, policy);
13
+ }
14
+ /**
15
+ * Resolve the policy for a given resource.
16
+ */
17
+ resolvePolicy(resource) {
18
+ if (!resource)
19
+ return null;
20
+ // Check if resource has a constructor
21
+ const constructor = resource.constructor;
22
+ if (constructor && this.policies.has(constructor)) {
23
+ return this.policies.get(constructor);
24
+ }
25
+ // Direct lookup
26
+ if (this.policies.has(resource)) {
27
+ return this.policies.get(resource);
28
+ }
29
+ return null;
30
+ }
31
+ /**
32
+ * Get policy method for ability.
33
+ */
34
+ getPolicyMethod(policy, ability) {
35
+ if (!policy)
36
+ return null;
37
+ const instance = typeof policy === 'function' ? new policy() : policy;
38
+ if (typeof instance[ability] === 'function') {
39
+ return instance[ability].bind(instance);
40
+ }
41
+ return null;
42
+ }
43
+ }
44
+ exports.PolicyResolver = PolicyResolver;
45
+ //# sourceMappingURL=PolicyResolver.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PolicyResolver.js","sourceRoot":"","sources":["../src/PolicyResolver.ts"],"names":[],"mappings":";;;AAEA,MAAa,cAAc;IAA3B;QACY,aAAQ,GAAkB,IAAI,GAAG,EAAE,CAAC;IA2ChD,CAAC;IAzCG;;OAEG;IACI,QAAQ,CAAC,KAAU,EAAE,MAAW;QACnC,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;IACrC,CAAC;IAED;;OAEG;IACI,aAAa,CAAC,QAAa;QAC9B,IAAI,CAAC,QAAQ;YAAE,OAAO,IAAI,CAAC;QAE3B,sCAAsC;QACtC,MAAM,WAAW,GAAG,QAAQ,CAAC,WAAW,CAAC;QACzC,IAAI,WAAW,IAAI,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,WAAW,CAAC,EAAE,CAAC;YAChD,OAAO,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC;QAC1C,CAAC;QAED,gBAAgB;QAChB,IAAI,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE,CAAC;YAC9B,OAAO,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QACvC,CAAC;QAED,OAAO,IAAI,CAAC;IAChB,CAAC;IAED;;OAEG;IACI,eAAe,CAAC,MAAW,EAAE,OAAe;QAC/C,IAAI,CAAC,MAAM;YAAE,OAAO,IAAI,CAAC;QAEzB,MAAM,QAAQ,GAAG,OAAO,MAAM,KAAK,UAAU,CAAC,CAAC,CAAC,IAAI,MAAM,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC;QAEtE,IAAI,OAAO,QAAQ,CAAC,OAAO,CAAC,KAAK,UAAU,EAAE,CAAC;YAC1C,OAAO,QAAQ,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;QAC5C,CAAC;QAED,OAAO,IAAI,CAAC;IAChB,CAAC;CACJ;AA5CD,wCA4CC"}
@@ -0,0 +1,7 @@
1
+ export * from './Gate';
2
+ export * from './AuthorizationManager';
3
+ export * from './PolicyResolver';
4
+ export * from './Contracts/Policy';
5
+ export * from './Exceptions/AuthorizationException';
6
+ export * from './Middleware/Authorize';
7
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,QAAQ,CAAC;AACvB,cAAc,wBAAwB,CAAC;AACvC,cAAc,kBAAkB,CAAC;AACjC,cAAc,oBAAoB,CAAC;AACnC,cAAc,qCAAqC,CAAC;AACpD,cAAc,wBAAwB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./Gate"), exports);
18
+ __exportStar(require("./AuthorizationManager"), exports);
19
+ __exportStar(require("./PolicyResolver"), exports);
20
+ __exportStar(require("./Contracts/Policy"), exports);
21
+ __exportStar(require("./Exceptions/AuthorizationException"), exports);
22
+ __exportStar(require("./Middleware/Authorize"), exports);
23
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,yCAAuB;AACvB,yDAAuC;AACvC,mDAAiC;AACjC,qDAAmC;AACnC,sEAAoD;AACpD,yDAAuC"}
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@arikajs/authorization",
3
+ "version": "0.1.0",
4
+ "description": "Authorization (access control) for the ArikaJS framework.",
5
+ "license": "MIT",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "scripts": {
9
+ "build": "tsc -p tsconfig.json",
10
+ "build:tests": "tsc -p tsconfig.test.json",
11
+ "clean": "rm -rf dist",
12
+ "prepare": "echo skip",
13
+ "test": "npm run build && npm run build:tests && node scripts/fix-test-imports.js && node --test 'dist/tests/**/*.test.js'",
14
+ "test:watch": "npm run build && npm run build:tests && node --test --watch 'dist/tests/**/*.test.js'"
15
+ },
16
+ "files": [
17
+ "dist"
18
+ ],
19
+ "keywords": [
20
+ "arika",
21
+ "arika-js",
22
+ "framework",
23
+ "authorization",
24
+ "access-control",
25
+ "policy",
26
+ "gate"
27
+ ],
28
+ "engines": {
29
+ "node": ">=20.0.0"
30
+ },
31
+ "repository": {
32
+ "type": "git",
33
+ "url": "git+https://github.com/arikajs/authorization.git"
34
+ },
35
+ "bugs": {
36
+ "url": "https://github.com/arikajs/authorization/issues"
37
+ },
38
+ "homepage": "https://github.com/arikajs/authorization#readme",
39
+ "devDependencies": {
40
+ "@types/node": "^20.11.24",
41
+ "typescript": "^5.3.3"
42
+ },
43
+ "author": "Prakash Tank"
44
+ }