@wild-framework/auth 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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Wild Framework contributors
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,28 @@
1
+ # @wild-framework/auth
2
+
3
+ Authentication guards and authorization policies for [Wild Framework](https://github.com/your-org/wild-framework).
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ pnpm add @wild-framework/auth
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```ts
14
+ import { AuthManager, Policy } from "@wild-framework/auth";
15
+
16
+ auth.setUserResolver(async (req) => verifyToken(req.headers.authorization));
17
+ router.get("/dashboard", auth.authenticate(), action(Dashboard, "index"));
18
+
19
+ class PostPolicy extends Policy<Post> {
20
+ update(user: User, post: Post) { return user.id === post.authorId; }
21
+ }
22
+
23
+ if (!auth.authorize(PostPolicy, "update", post, user)) throw new ForbiddenException();
24
+ ```
25
+
26
+ ## License
27
+
28
+ MIT
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Thrown when authentication is required but no valid credentials are provided.
3
+ */
4
+ export declare class UnauthorizedException extends Error {
5
+ readonly statusCode = 401;
6
+ constructor(message?: string);
7
+ }
8
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,qBAAa,qBAAsB,SAAQ,KAAK;IAC5C,SAAgB,UAAU,OAAO;gBAErB,OAAO,SAAiB;CAIvC"}
package/dist/errors.js ADDED
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Thrown when authentication is required but no valid credentials are provided.
3
+ */
4
+ export class UnauthorizedException extends Error {
5
+ statusCode = 401;
6
+ constructor(message = "Unauthorized") {
7
+ super(message);
8
+ this.name = "UnauthorizedException";
9
+ }
10
+ }
11
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,OAAO,qBAAsB,SAAQ,KAAK;IAC5B,UAAU,GAAG,GAAG,CAAC;IAEjC,YAAY,OAAO,GAAG,cAAc;QAChC,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,uBAAuB,CAAC;IACxC,CAAC;CACJ"}
@@ -0,0 +1,6 @@
1
+ export { AuthManager } from "./manager.js";
2
+ export type { UserResolver } from "./manager.js";
3
+ export { Policy } from "./manager.js";
4
+ export { AuthProvider } from "./provider.js";
5
+ export { UnauthorizedException } from "./errors.js";
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAC3C,YAAY,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AACjD,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AACtC,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ export { AuthManager } from "./manager.js";
2
+ export { Policy } from "./manager.js";
3
+ export { AuthProvider } from "./provider.js";
4
+ export { UnauthorizedException } from "./errors.js";
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAE3C,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AACtC,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC"}
@@ -0,0 +1,70 @@
1
+ /**
2
+ * Minimal request interface — compatible with Express but no dependency.
3
+ */
4
+ interface AuthRequest {
5
+ headers: Record<string, string | string[] | undefined>;
6
+ [key: string]: unknown;
7
+ }
8
+ /**
9
+ * Minimal response interface.
10
+ */
11
+ interface AuthResponse {
12
+ }
13
+ /**
14
+ * Express-compatible next function.
15
+ */
16
+ type NextFunction = (error?: unknown) => void;
17
+ /**
18
+ * Function that resolves a user from an incoming request.
19
+ * Return `null` if the request is unauthenticated.
20
+ */
21
+ export type UserResolver<TUser = unknown> = (req: AuthRequest) => Promise<TUser | null> | TUser | null;
22
+ /**
23
+ * Base class for authorization policies.
24
+ *
25
+ * Subclasses define methods like `view(user, resource)`, `update(user, resource)`
26
+ * that return `true` if the action is allowed.
27
+ */
28
+ export declare abstract class Policy<TResource = unknown> {
29
+ }
30
+ /**
31
+ * Manages authentication and authorization.
32
+ *
33
+ * Holds a user resolver (set during boot) and provides middleware
34
+ * and policy-based authorization.
35
+ */
36
+ export declare class AuthManager<TUser = unknown> {
37
+ private resolver;
38
+ /**
39
+ * Set the function that resolves a user from a request.
40
+ * Called during provider boot.
41
+ */
42
+ setUserResolver(resolver: UserResolver<TUser>): void;
43
+ /**
44
+ * Resolve the current user from a request.
45
+ * Returns `null` if not authenticated or no resolver is configured.
46
+ */
47
+ user(req: AuthRequest): Promise<TUser | null>;
48
+ /**
49
+ * Middleware that requires authentication.
50
+ *
51
+ * Resolves the user and attaches it to `req`. Throws
52
+ * `UnauthorizedException` if no user is resolved.
53
+ *
54
+ * The resolved user is stored on `(req as any).user`.
55
+ */
56
+ authenticate(): (req: AuthRequest, _res: AuthResponse, next: NextFunction) => void;
57
+ /**
58
+ * Check if the current user is authorized to perform an action on a resource.
59
+ *
60
+ * @param policyClass — a Policy subclass
61
+ * @param action — the method name to call on the policy
62
+ * @param resource — the resource being acted upon
63
+ * @param user — the current user (resolved from request)
64
+ *
65
+ * @returns `true` if the policy method returns `true`
66
+ */
67
+ authorize<TPolicy extends Policy<TResource>, TResource>(policyClass: new () => TPolicy, action: string & keyof TPolicy, resource: TResource, user: TUser): boolean;
68
+ }
69
+ export {};
70
+ //# sourceMappingURL=manager.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"manager.d.ts","sourceRoot":"","sources":["../src/manager.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,UAAU,WAAW;IACjB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,SAAS,CAAC,CAAC;IACvD,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CAC1B;AAED;;GAEG;AACH,UAAU,YAAY;CAErB;AAED;;GAEG;AACH,KAAK,YAAY,GAAG,CAAC,KAAK,CAAC,EAAE,OAAO,KAAK,IAAI,CAAC;AAE9C;;;GAGG;AACH,MAAM,MAAM,YAAY,CAAC,KAAK,GAAG,OAAO,IAAI,CAAC,GAAG,EAAE,WAAW,KAAK,OAAO,CAAC,KAAK,GAAG,IAAI,CAAC,GAAG,KAAK,GAAG,IAAI,CAAC;AAEvG;;;;;GAKG;AACH,8BAAsB,MAAM,CAAC,SAAS,GAAG,OAAO;CAG/C;AAED;;;;;GAKG;AACH,qBAAa,WAAW,CAAC,KAAK,GAAG,OAAO;IACpC,OAAO,CAAC,QAAQ,CAAoC;IAMpD;;;OAGG;IACH,eAAe,CAAC,QAAQ,EAAE,YAAY,CAAC,KAAK,CAAC,GAAG,IAAI;IAQpD;;;OAGG;IACG,IAAI,CAAC,GAAG,EAAE,WAAW,GAAG,OAAO,CAAC,KAAK,GAAG,IAAI,CAAC;IASnD;;;;;;;OAOG;IACH,YAAY,IAAI,CAAC,GAAG,EAAE,WAAW,EAAE,IAAI,EAAE,YAAY,EAAE,IAAI,EAAE,YAAY,KAAK,IAAI;IAmBlF;;;;;;;;;OASG;IACH,SAAS,CAAC,OAAO,SAAS,MAAM,CAAC,SAAS,CAAC,EAAE,SAAS,EAClD,WAAW,EAAE,UAAU,OAAO,EAC9B,MAAM,EAAE,MAAM,GAAG,MAAM,OAAO,EAC9B,QAAQ,EAAE,SAAS,EACnB,IAAI,EAAE,KAAK,GACZ,OAAO;CAUb"}
@@ -0,0 +1,88 @@
1
+ import { UnauthorizedException } from "./errors.js";
2
+ /**
3
+ * Base class for authorization policies.
4
+ *
5
+ * Subclasses define methods like `view(user, resource)`, `update(user, resource)`
6
+ * that return `true` if the action is allowed.
7
+ */
8
+ export class Policy {
9
+ }
10
+ /**
11
+ * Manages authentication and authorization.
12
+ *
13
+ * Holds a user resolver (set during boot) and provides middleware
14
+ * and policy-based authorization.
15
+ */
16
+ export class AuthManager {
17
+ resolver = null;
18
+ // -------------------------------------------------------------------
19
+ // Configuration
20
+ // -------------------------------------------------------------------
21
+ /**
22
+ * Set the function that resolves a user from a request.
23
+ * Called during provider boot.
24
+ */
25
+ setUserResolver(resolver) {
26
+ this.resolver = resolver;
27
+ }
28
+ // -------------------------------------------------------------------
29
+ // User resolution
30
+ // -------------------------------------------------------------------
31
+ /**
32
+ * Resolve the current user from a request.
33
+ * Returns `null` if not authenticated or no resolver is configured.
34
+ */
35
+ async user(req) {
36
+ if (!this.resolver)
37
+ return null;
38
+ return this.resolver(req);
39
+ }
40
+ // -------------------------------------------------------------------
41
+ // Middleware
42
+ // -------------------------------------------------------------------
43
+ /**
44
+ * Middleware that requires authentication.
45
+ *
46
+ * Resolves the user and attaches it to `req`. Throws
47
+ * `UnauthorizedException` if no user is resolved.
48
+ *
49
+ * The resolved user is stored on `(req as any).user`.
50
+ */
51
+ authenticate() {
52
+ return async (req, _res, next) => {
53
+ try {
54
+ const user = await this.user(req);
55
+ if (!user) {
56
+ throw new UnauthorizedException();
57
+ }
58
+ req.user = user;
59
+ next();
60
+ }
61
+ catch (error) {
62
+ next(error);
63
+ }
64
+ };
65
+ }
66
+ // -------------------------------------------------------------------
67
+ // Authorization
68
+ // -------------------------------------------------------------------
69
+ /**
70
+ * Check if the current user is authorized to perform an action on a resource.
71
+ *
72
+ * @param policyClass — a Policy subclass
73
+ * @param action — the method name to call on the policy
74
+ * @param resource — the resource being acted upon
75
+ * @param user — the current user (resolved from request)
76
+ *
77
+ * @returns `true` if the policy method returns `true`
78
+ */
79
+ authorize(policyClass, action, resource, user) {
80
+ const policy = new policyClass();
81
+ const method = policy[action];
82
+ if (typeof method !== "function") {
83
+ throw new Error(`Policy method "${String(action)}" not found on "${policyClass.name}".`);
84
+ }
85
+ return method.call(policy, user, resource) === true;
86
+ }
87
+ }
88
+ //# sourceMappingURL=manager.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"manager.js","sourceRoot":"","sources":["../src/manager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AA4BpD;;;;;GAKG;AACH,MAAM,OAAgB,MAAM;CAG3B;AAED;;;;;GAK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AAC,kBAAkB,MAAM,CAAC,MAAM,CAAC,mBAAmB,WAAW,CAAC,IAAI,IAAI,CAAC,CAAC;QAC7F,CAAC;QAED,OAAQ,MAAmB,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,EAAE,QAAQ,CAAC,KAAK,IAAI,CAAC;IACtE,CAAC;CACJ"}
@@ -0,0 +1,11 @@
1
+ import type { Provider, Application } from "@wild-framework/core";
2
+ /**
3
+ * Registers the auth system with the application.
4
+ *
5
+ * Binds `AuthManager` into the container. The user resolver must be
6
+ * configured by another provider during `boot()`.
7
+ */
8
+ export declare class AuthProvider implements Provider {
9
+ register(app: Application): void;
10
+ }
11
+ //# sourceMappingURL=provider.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAIlE;;;;;GAKG;AACH,qBAAa,YAAa,YAAW,QAAQ;IACzC,QAAQ,CAAC,GAAG,EAAE,WAAW,GAAG,IAAI;CAKnC"}
@@ -0,0 +1,16 @@
1
+ import { Container } from "@wild-framework/container";
2
+ import { AuthManager } from "./manager.js";
3
+ /**
4
+ * Registers the auth system with the application.
5
+ *
6
+ * Binds `AuthManager` into the container. The user resolver must be
7
+ * configured by another provider during `boot()`.
8
+ */
9
+ export class AuthProvider {
10
+ register(app) {
11
+ const container = app.resolve(Container);
12
+ const auth = new AuthManager();
13
+ container.singleton(AuthManager, () => auth);
14
+ }
15
+ }
16
+ //# sourceMappingURL=provider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"provider.js","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AACtD,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAE3C;;;;;GAKG;AACH,MAAM,OAAO,YAAY;IACrB,QAAQ,CAAC,GAAgB;QACrB,MAAM,SAAS,GAAG,GAAG,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;QACzC,MAAM,IAAI,GAAG,IAAI,WAAW,EAAE,CAAC;QAC/B,SAAS,CAAC,SAAS,CAAC,WAAW,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,CAAC;IACjD,CAAC;CACJ"}
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@wild-framework/auth",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "main": "./dist/index.js",
6
+ "types": "./dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "import": "./dist/index.js"
11
+ }
12
+ },
13
+ "files": [
14
+ "dist"
15
+ ],
16
+ "publishConfig": {
17
+ "access": "public"
18
+ },
19
+ "license": "MIT",
20
+ "author": "Sphah",
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "git+https://github.com/KhandaSoftware/wild-framework.git"
24
+ },
25
+ "description": "Authentication guards and authorization policies for Wild Framework.",
26
+ "keywords": [
27
+ "wild",
28
+ "framework",
29
+ "auth",
30
+ "authorization",
31
+ "policies"
32
+ ],
33
+ "dependencies": {
34
+ "@wild-framework/core": "0.1.0",
35
+ "@wild-framework/container": "0.1.0"
36
+ },
37
+ "scripts": {
38
+ "build": "tsc",
39
+ "clean": "rm -rf dist",
40
+ "test": "vitest run"
41
+ }
42
+ }