@vercube/auth 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +104 -0
- package/dist/index.mjs +47 -15
- package/package.json +6 -12
- package/dist/Decorators/Auth.d.ts +0 -7
- package/dist/Decorators/User.d.ts +0 -32
- package/dist/Middleware/AuthMiddleware.d.ts +0 -24
- package/dist/Services/AuthProvider.d.ts +0 -23
- package/dist/Types/AuthTypes.d.ts +0 -15
- package/dist/index.cjs +0 -161
- package/dist/index.d.ts +0 -5
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { BaseMiddleware, MiddlewareOptions } from "@vercube/core";
|
|
2
|
+
|
|
3
|
+
//#region src/Services/AuthProvider.d.ts
|
|
4
|
+
|
|
5
|
+
declare abstract class AuthProvider<U = unknown> {
|
|
6
|
+
/**
|
|
7
|
+
* Validate authentication
|
|
8
|
+
* @param {Request} request - The request object
|
|
9
|
+
* @param {AuthTypes.MiddlewareOptions} params - Additional parameters
|
|
10
|
+
* @returns An error string or Promise of error string, null or Promise of null if authentication is successful
|
|
11
|
+
*/
|
|
12
|
+
abstract validate(request: Request, params?: AuthTypes.MiddlewareOptions): Promise<string | null> | string | null;
|
|
13
|
+
/**
|
|
14
|
+
* Get current user
|
|
15
|
+
* @param {Request} request - The request object
|
|
16
|
+
* @returns A promise of the current user or null if no user is authenticated
|
|
17
|
+
*/
|
|
18
|
+
abstract getCurrentUser(request: Request): Promise<U | null> | U | null;
|
|
19
|
+
}
|
|
20
|
+
//#endregion
|
|
21
|
+
//#region src/Types/AuthTypes.d.ts
|
|
22
|
+
declare namespace AuthTypes {
|
|
23
|
+
/**
|
|
24
|
+
* Middleware options for auth
|
|
25
|
+
*/
|
|
26
|
+
interface MiddlewareOptions {
|
|
27
|
+
/**
|
|
28
|
+
* Roles to authorize
|
|
29
|
+
* @default []
|
|
30
|
+
*/
|
|
31
|
+
roles?: string[];
|
|
32
|
+
/**
|
|
33
|
+
* Override provider to use for authorization
|
|
34
|
+
* Default one is set inside of the IOC container
|
|
35
|
+
*/
|
|
36
|
+
provider?: typeof AuthProvider;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
//#endregion
|
|
40
|
+
//#region src/Decorators/Auth.d.ts
|
|
41
|
+
/**
|
|
42
|
+
* Authentication decorator that adds middleware to protect routes or controllers
|
|
43
|
+
* @param options Optional options for the authentication middleware
|
|
44
|
+
* @returns A decorator function that adds authentication middleware to the target
|
|
45
|
+
*/
|
|
46
|
+
declare function Auth(options?: AuthTypes.MiddlewareOptions): Function;
|
|
47
|
+
//#endregion
|
|
48
|
+
//#region src/Decorators/User.d.ts
|
|
49
|
+
/**
|
|
50
|
+
* Options for the User decorator
|
|
51
|
+
* @interface UserDecoratorOptions
|
|
52
|
+
*/
|
|
53
|
+
interface UserDecoratorOptions {
|
|
54
|
+
/**
|
|
55
|
+
* Optional custom auth provider to use for retrieving the current user
|
|
56
|
+
* If not provided, the default AuthProvider will be used
|
|
57
|
+
*/
|
|
58
|
+
provider?: typeof AuthProvider;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Creates a decorator that injects the current user into controller methods
|
|
62
|
+
*
|
|
63
|
+
* @param {UserDecoratorOptions} [params] - Optional configuration for the decorator
|
|
64
|
+
* @returns {Function} A decorator function that can be applied to controller method parameters
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* // Basic usage
|
|
68
|
+
* async someMethod(@User() user: User) {
|
|
69
|
+
* // user is automatically injected
|
|
70
|
+
* }
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* // With custom provider
|
|
74
|
+
* async someMethod(@User() user: User) {
|
|
75
|
+
* // user is retrieved using CustomAuthProvider
|
|
76
|
+
* }
|
|
77
|
+
*/
|
|
78
|
+
declare function User(params?: UserDecoratorOptions): Function;
|
|
79
|
+
//#endregion
|
|
80
|
+
//#region src/Middleware/AuthMiddleware.d.ts
|
|
81
|
+
/**
|
|
82
|
+
* Middleware for auth
|
|
83
|
+
* @implements {BaseMiddleware}
|
|
84
|
+
* @description authorizes incoming request
|
|
85
|
+
* @example
|
|
86
|
+
* const middleware = new AuthMiddleware();
|
|
87
|
+
* await middleware.use(event);
|
|
88
|
+
*/
|
|
89
|
+
declare class AuthMiddleware implements BaseMiddleware<AuthTypes.MiddlewareOptions> {
|
|
90
|
+
private gContainer;
|
|
91
|
+
private gLogger;
|
|
92
|
+
private gAuthProvider;
|
|
93
|
+
/**
|
|
94
|
+
* Middleware function that processes the HTTP event.
|
|
95
|
+
*
|
|
96
|
+
* @param {Request} request - The HTTP request to be processed
|
|
97
|
+
* @param {Response} response - The HTTP response to be processed
|
|
98
|
+
* @param {MiddlewareOptions} args - Additional arguments for the middleware
|
|
99
|
+
* @returns {Promise<void>} - A promise that resolves when the processing is complete.
|
|
100
|
+
*/
|
|
101
|
+
onRequest(request: Request, response: Response, args: MiddlewareOptions<AuthTypes.MiddlewareOptions>): Promise<void>;
|
|
102
|
+
}
|
|
103
|
+
//#endregion
|
|
104
|
+
export { Auth, AuthMiddleware, AuthProvider, AuthTypes, User };
|
package/dist/index.mjs
CHANGED
|
@@ -1,21 +1,52 @@
|
|
|
1
|
+
import "node:module";
|
|
1
2
|
import { UnauthorizedError, initializeMetadata, initializeMetadataMethod } from "@vercube/core";
|
|
2
3
|
import { BaseDecorator, Container, Inject, InjectOptional, createDecorator } from "@vercube/di";
|
|
3
4
|
import { Logger } from "@vercube/logger";
|
|
4
5
|
|
|
5
|
-
//#region
|
|
6
|
+
//#region rolldown:runtime
|
|
7
|
+
var __create = Object.create;
|
|
8
|
+
var __defProp = Object.defineProperty;
|
|
9
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
10
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
11
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
12
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
13
|
+
var __commonJS = (cb, mod) => function() {
|
|
14
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
15
|
+
};
|
|
16
|
+
var __copyProps = (to, from, except, desc) => {
|
|
17
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
18
|
+
key = keys[i];
|
|
19
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
20
|
+
get: ((k) => from[k]).bind(null, key),
|
|
21
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
return to;
|
|
25
|
+
};
|
|
26
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
27
|
+
value: mod,
|
|
28
|
+
enumerable: true
|
|
29
|
+
}) : target, mod));
|
|
30
|
+
|
|
31
|
+
//#endregion
|
|
32
|
+
//#region src/Services/AuthProvider.ts
|
|
6
33
|
var AuthProvider = class {};
|
|
7
34
|
|
|
8
35
|
//#endregion
|
|
9
|
-
//#region node_modules/.pnpm/@oxc-project+runtime@0.
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
36
|
+
//#region ../../node_modules/.pnpm/@oxc-project+runtime@0.72.1/node_modules/@oxc-project/runtime/src/helpers/decorate.js
|
|
37
|
+
var require_decorate = __commonJS({ "../../node_modules/.pnpm/@oxc-project+runtime@0.72.1/node_modules/@oxc-project/runtime/src/helpers/decorate.js"(exports, module) {
|
|
38
|
+
function __decorate(decorators, target, key, desc) {
|
|
39
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
40
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
41
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
42
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
43
|
+
}
|
|
44
|
+
module.exports = __decorate, module.exports.__esModule = true, module.exports["default"] = module.exports;
|
|
45
|
+
} });
|
|
16
46
|
|
|
17
47
|
//#endregion
|
|
18
|
-
//#region
|
|
48
|
+
//#region src/Middleware/AuthMiddleware.ts
|
|
49
|
+
var import_decorate$1 = __toESM(require_decorate());
|
|
19
50
|
/**
|
|
20
51
|
* Middleware for auth
|
|
21
52
|
* @implements {BaseMiddleware}
|
|
@@ -47,12 +78,12 @@ var AuthMiddleware = class {
|
|
|
47
78
|
if (authenticationError) throw new UnauthorizedError(authenticationError);
|
|
48
79
|
}
|
|
49
80
|
};
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
81
|
+
(0, import_decorate$1.default)([Inject(Container)], AuthMiddleware.prototype, "gContainer", void 0);
|
|
82
|
+
(0, import_decorate$1.default)([InjectOptional(Logger)], AuthMiddleware.prototype, "gLogger", void 0);
|
|
83
|
+
(0, import_decorate$1.default)([InjectOptional(AuthProvider)], AuthMiddleware.prototype, "gAuthProvider", void 0);
|
|
53
84
|
|
|
54
85
|
//#endregion
|
|
55
|
-
//#region
|
|
86
|
+
//#region src/Decorators/Auth.ts
|
|
56
87
|
/**
|
|
57
88
|
* Authentication decorator that adds middleware to protect routes or controllers
|
|
58
89
|
* @param options Optional options for the authentication middleware
|
|
@@ -72,7 +103,8 @@ function Auth(options) {
|
|
|
72
103
|
}
|
|
73
104
|
|
|
74
105
|
//#endregion
|
|
75
|
-
//#region
|
|
106
|
+
//#region src/Decorators/User.ts
|
|
107
|
+
var import_decorate = __toESM(require_decorate());
|
|
76
108
|
/**
|
|
77
109
|
* Decorator class for injecting the current user into controller methods
|
|
78
110
|
* Extends BaseDecorator to provide user injection functionality
|
|
@@ -101,7 +133,7 @@ var UserDecorator = class extends BaseDecorator {
|
|
|
101
133
|
});
|
|
102
134
|
}
|
|
103
135
|
};
|
|
104
|
-
|
|
136
|
+
(0, import_decorate.default)([Inject(Container)], UserDecorator.prototype, "gContainer", void 0);
|
|
105
137
|
/**
|
|
106
138
|
* Creates a decorator that injects the current user into controller methods
|
|
107
139
|
*
|
package/package.json
CHANGED
|
@@ -1,29 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vercube/auth",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"description": "Auth module for Vercube framework",
|
|
5
5
|
"repository": "@vercube/auth",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"sideEffects": false,
|
|
8
8
|
"type": "module",
|
|
9
9
|
"exports": {
|
|
10
|
-
".":
|
|
11
|
-
"types": "./dist/index.d.ts",
|
|
12
|
-
"import": "./dist/index.mjs",
|
|
13
|
-
"require": "./dist/index.cjs"
|
|
14
|
-
}
|
|
10
|
+
".": "./dist/index.mjs"
|
|
15
11
|
},
|
|
16
|
-
"
|
|
17
|
-
"module": "./dist/index.mjs",
|
|
18
|
-
"types": "./dist/index.d.ts",
|
|
12
|
+
"types": "./dist/index.d.mts",
|
|
19
13
|
"files": [
|
|
20
14
|
"dist",
|
|
21
15
|
"README.md"
|
|
22
16
|
],
|
|
23
17
|
"dependencies": {
|
|
24
|
-
"@vercube/core": "0.0.
|
|
25
|
-
"@vercube/
|
|
26
|
-
"@vercube/
|
|
18
|
+
"@vercube/core": "0.0.4",
|
|
19
|
+
"@vercube/logger": "0.0.4",
|
|
20
|
+
"@vercube/di": "0.0.4"
|
|
27
21
|
},
|
|
28
22
|
"publishConfig": {
|
|
29
23
|
"access": "public"
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import type { AuthTypes } from "../Types/AuthTypes.js";
|
|
2
|
-
/**
|
|
3
|
-
* Authentication decorator that adds middleware to protect routes or controllers
|
|
4
|
-
* @param options Optional options for the authentication middleware
|
|
5
|
-
* @returns A decorator function that adds authentication middleware to the target
|
|
6
|
-
*/
|
|
7
|
-
export declare function Auth(options?: AuthTypes.MiddlewareOptions): Function;
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { AuthProvider } from "../Services/AuthProvider.js";
|
|
2
|
-
/**
|
|
3
|
-
* Options for the User decorator
|
|
4
|
-
* @interface UserDecoratorOptions
|
|
5
|
-
*/
|
|
6
|
-
interface UserDecoratorOptions {
|
|
7
|
-
/**
|
|
8
|
-
* Optional custom auth provider to use for retrieving the current user
|
|
9
|
-
* If not provided, the default AuthProvider will be used
|
|
10
|
-
*/
|
|
11
|
-
provider?: typeof AuthProvider;
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* Creates a decorator that injects the current user into controller methods
|
|
15
|
-
*
|
|
16
|
-
* @param {UserDecoratorOptions} [params] - Optional configuration for the decorator
|
|
17
|
-
* @returns {Function} A decorator function that can be applied to controller method parameters
|
|
18
|
-
*
|
|
19
|
-
* @example
|
|
20
|
-
* // Basic usage
|
|
21
|
-
* async someMethod(@User() user: User) {
|
|
22
|
-
* // user is automatically injected
|
|
23
|
-
* }
|
|
24
|
-
*
|
|
25
|
-
* @example
|
|
26
|
-
* // With custom provider
|
|
27
|
-
* async someMethod(@User() user: User) {
|
|
28
|
-
* // user is retrieved using CustomAuthProvider
|
|
29
|
-
* }
|
|
30
|
-
*/
|
|
31
|
-
export declare function User(params?: UserDecoratorOptions): Function;
|
|
32
|
-
export {};
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { type BaseMiddleware, type MiddlewareOptions } from "@vercube/core";
|
|
2
|
-
import type { AuthTypes } from "../Types/AuthTypes.js";
|
|
3
|
-
/**
|
|
4
|
-
* Middleware for auth
|
|
5
|
-
* @implements {BaseMiddleware}
|
|
6
|
-
* @description authorizes incoming request
|
|
7
|
-
* @example
|
|
8
|
-
* const middleware = new AuthMiddleware();
|
|
9
|
-
* await middleware.use(event);
|
|
10
|
-
*/
|
|
11
|
-
export declare class AuthMiddleware implements BaseMiddleware<AuthTypes.MiddlewareOptions> {
|
|
12
|
-
private gContainer;
|
|
13
|
-
private gLogger;
|
|
14
|
-
private gAuthProvider;
|
|
15
|
-
/**
|
|
16
|
-
* Middleware function that processes the HTTP event.
|
|
17
|
-
*
|
|
18
|
-
* @param {Request} request - The HTTP request to be processed
|
|
19
|
-
* @param {Response} response - The HTTP response to be processed
|
|
20
|
-
* @param {MiddlewareOptions} args - Additional arguments for the middleware
|
|
21
|
-
* @returns {Promise<void>} - A promise that resolves when the processing is complete.
|
|
22
|
-
*/
|
|
23
|
-
onRequest(request: Request, response: Response, args: MiddlewareOptions<AuthTypes.MiddlewareOptions>): Promise<void>;
|
|
24
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Abstract class representing an authorization provider
|
|
3
|
-
* Provides a common interface for different authorization implementations
|
|
4
|
-
*
|
|
5
|
-
* @abstract
|
|
6
|
-
* @class AuthorizationProvider
|
|
7
|
-
*/
|
|
8
|
-
import { AuthTypes } from "../Types/AuthTypes.js";
|
|
9
|
-
export declare abstract class AuthProvider<U = unknown> {
|
|
10
|
-
/**
|
|
11
|
-
* Validate authentication
|
|
12
|
-
* @param {Request} request - The request object
|
|
13
|
-
* @param {AuthTypes.MiddlewareOptions} params - Additional parameters
|
|
14
|
-
* @returns An error string or Promise of error string, null or Promise of null if authentication is successful
|
|
15
|
-
*/
|
|
16
|
-
abstract validate(request: Request, params?: AuthTypes.MiddlewareOptions): Promise<string | null> | string | null;
|
|
17
|
-
/**
|
|
18
|
-
* Get current user
|
|
19
|
-
* @param {Request} request - The request object
|
|
20
|
-
* @returns A promise of the current user or null if no user is authenticated
|
|
21
|
-
*/
|
|
22
|
-
abstract getCurrentUser(request: Request): Promise<U | null> | U | null;
|
|
23
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { AuthProvider } from "../Services/AuthProvider.js";
|
|
2
|
-
export declare namespace AuthTypes {
|
|
3
|
-
interface MiddlewareOptions {
|
|
4
|
-
/**
|
|
5
|
-
* Roles to authorize
|
|
6
|
-
* @default []
|
|
7
|
-
*/
|
|
8
|
-
roles?: string[];
|
|
9
|
-
/**
|
|
10
|
-
* Override provider to use for authorization
|
|
11
|
-
* Default one is set inside of the IOC container
|
|
12
|
-
*/
|
|
13
|
-
provider?: typeof AuthProvider;
|
|
14
|
-
}
|
|
15
|
-
}
|
package/dist/index.cjs
DELETED
|
@@ -1,161 +0,0 @@
|
|
|
1
|
-
//#region rolldown:runtime
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __commonJS = (cb, mod) => function() {
|
|
9
|
-
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
10
|
-
};
|
|
11
|
-
var __copyProps = (to, from, except, desc) => {
|
|
12
|
-
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
13
|
-
key = keys[i];
|
|
14
|
-
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
15
|
-
get: ((k) => from[k]).bind(null, key),
|
|
16
|
-
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
17
|
-
});
|
|
18
|
-
}
|
|
19
|
-
return to;
|
|
20
|
-
};
|
|
21
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
22
|
-
value: mod,
|
|
23
|
-
enumerable: true
|
|
24
|
-
}) : target, mod));
|
|
25
|
-
|
|
26
|
-
//#endregion
|
|
27
|
-
const __vercube_core = __toESM(require("@vercube/core"));
|
|
28
|
-
const __vercube_di = __toESM(require("@vercube/di"));
|
|
29
|
-
const __vercube_logger = __toESM(require("@vercube/logger"));
|
|
30
|
-
|
|
31
|
-
//#region packages/auth/src/Services/AuthProvider.ts
|
|
32
|
-
var AuthProvider = class {};
|
|
33
|
-
|
|
34
|
-
//#endregion
|
|
35
|
-
//#region node_modules/.pnpm/@oxc-project+runtime@0.67.0/node_modules/@oxc-project/runtime/src/helpers/decorate.js
|
|
36
|
-
var require_decorate = __commonJS({ "node_modules/.pnpm/@oxc-project+runtime@0.67.0/node_modules/@oxc-project/runtime/src/helpers/decorate.js"(exports, module) {
|
|
37
|
-
function __decorate(decorators, target, key, desc) {
|
|
38
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
39
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
40
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
41
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
42
|
-
}
|
|
43
|
-
module.exports = __decorate, module.exports.__esModule = true, module.exports["default"] = module.exports;
|
|
44
|
-
} });
|
|
45
|
-
var import_decorate = __toESM(require_decorate(), 1);
|
|
46
|
-
|
|
47
|
-
//#endregion
|
|
48
|
-
//#region packages/auth/src/Middleware/AuthMiddleware.ts
|
|
49
|
-
/**
|
|
50
|
-
* Middleware for auth
|
|
51
|
-
* @implements {BaseMiddleware}
|
|
52
|
-
* @description authorizes incoming request
|
|
53
|
-
* @example
|
|
54
|
-
* const middleware = new AuthMiddleware();
|
|
55
|
-
* await middleware.use(event);
|
|
56
|
-
*/
|
|
57
|
-
var AuthMiddleware = class {
|
|
58
|
-
gContainer;
|
|
59
|
-
gLogger;
|
|
60
|
-
gAuthProvider;
|
|
61
|
-
/**
|
|
62
|
-
* Middleware function that processes the HTTP event.
|
|
63
|
-
*
|
|
64
|
-
* @param {Request} request - The HTTP request to be processed
|
|
65
|
-
* @param {Response} response - The HTTP response to be processed
|
|
66
|
-
* @param {MiddlewareOptions} args - Additional arguments for the middleware
|
|
67
|
-
* @returns {Promise<void>} - A promise that resolves when the processing is complete.
|
|
68
|
-
*/
|
|
69
|
-
async onRequest(request, response, args) {
|
|
70
|
-
let provider = this.gAuthProvider;
|
|
71
|
-
if (args?.middlewareArgs?.provider) provider = this.gContainer.getOptional(args.middlewareArgs.provider);
|
|
72
|
-
if (!provider) {
|
|
73
|
-
this.gLogger?.warn("AuthMiddleware::AuthProvider is not registered");
|
|
74
|
-
return;
|
|
75
|
-
}
|
|
76
|
-
const authenticationError = await provider.validate(request, args.middlewareArgs);
|
|
77
|
-
if (authenticationError) throw new __vercube_core.UnauthorizedError(authenticationError);
|
|
78
|
-
}
|
|
79
|
-
};
|
|
80
|
-
(0, import_decorate.default)([(0, __vercube_di.Inject)(__vercube_di.Container)], AuthMiddleware.prototype, "gContainer", void 0);
|
|
81
|
-
(0, import_decorate.default)([(0, __vercube_di.InjectOptional)(__vercube_logger.Logger)], AuthMiddleware.prototype, "gLogger", void 0);
|
|
82
|
-
(0, import_decorate.default)([(0, __vercube_di.InjectOptional)(AuthProvider)], AuthMiddleware.prototype, "gAuthProvider", void 0);
|
|
83
|
-
|
|
84
|
-
//#endregion
|
|
85
|
-
//#region packages/auth/src/Decorators/Auth.ts
|
|
86
|
-
/**
|
|
87
|
-
* Authentication decorator that adds middleware to protect routes or controllers
|
|
88
|
-
* @param options Optional options for the authentication middleware
|
|
89
|
-
* @returns A decorator function that adds authentication middleware to the target
|
|
90
|
-
*/
|
|
91
|
-
function Auth(options) {
|
|
92
|
-
return function internalDecorator(target, propertyName) {
|
|
93
|
-
const meta = (0, __vercube_core.initializeMetadata)(propertyName ? target : target.prototype);
|
|
94
|
-
if (propertyName) (0, __vercube_core.initializeMetadataMethod)(target, propertyName);
|
|
95
|
-
meta.__middlewares.push({
|
|
96
|
-
target: propertyName ?? "__global__",
|
|
97
|
-
priority: -999,
|
|
98
|
-
middleware: AuthMiddleware,
|
|
99
|
-
args: { ...options }
|
|
100
|
-
});
|
|
101
|
-
};
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
//#endregion
|
|
105
|
-
//#region packages/auth/src/Decorators/User.ts
|
|
106
|
-
/**
|
|
107
|
-
* Decorator class for injecting the current user into controller methods
|
|
108
|
-
* Extends BaseDecorator to provide user injection functionality
|
|
109
|
-
*
|
|
110
|
-
* @class UserDecorator
|
|
111
|
-
* @extends {BaseDecorator<UserDecoratorOptions>}
|
|
112
|
-
*/
|
|
113
|
-
var UserDecorator = class extends __vercube_di.BaseDecorator {
|
|
114
|
-
gContainer;
|
|
115
|
-
/**
|
|
116
|
-
* Called when the decorator is created.
|
|
117
|
-
* Initializes metadata for the property and adds parameter information
|
|
118
|
-
* to handle user injection in controller methods.
|
|
119
|
-
*/
|
|
120
|
-
created() {
|
|
121
|
-
(0, __vercube_core.initializeMetadata)(this.prototype);
|
|
122
|
-
const method = (0, __vercube_core.initializeMetadataMethod)(this.prototype, this.propertyName);
|
|
123
|
-
method.args.push({
|
|
124
|
-
idx: this.propertyIndex,
|
|
125
|
-
type: "custom",
|
|
126
|
-
resolver: async (event) => {
|
|
127
|
-
const provider = this.options?.provider ? this.gContainer.getOptional(this.options.provider) : this.gContainer.getOptional(AuthProvider);
|
|
128
|
-
if (!provider) return null;
|
|
129
|
-
return provider.getCurrentUser(event.request);
|
|
130
|
-
}
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
};
|
|
134
|
-
(0, import_decorate.default)([(0, __vercube_di.Inject)(__vercube_di.Container)], UserDecorator.prototype, "gContainer", void 0);
|
|
135
|
-
/**
|
|
136
|
-
* Creates a decorator that injects the current user into controller methods
|
|
137
|
-
*
|
|
138
|
-
* @param {UserDecoratorOptions} [params] - Optional configuration for the decorator
|
|
139
|
-
* @returns {Function} A decorator function that can be applied to controller method parameters
|
|
140
|
-
*
|
|
141
|
-
* @example
|
|
142
|
-
* // Basic usage
|
|
143
|
-
* async someMethod(@User() user: User) {
|
|
144
|
-
* // user is automatically injected
|
|
145
|
-
* }
|
|
146
|
-
*
|
|
147
|
-
* @example
|
|
148
|
-
* // With custom provider
|
|
149
|
-
* async someMethod(@User() user: User) {
|
|
150
|
-
* // user is retrieved using CustomAuthProvider
|
|
151
|
-
* }
|
|
152
|
-
*/
|
|
153
|
-
function User(params) {
|
|
154
|
-
return (0, __vercube_di.createDecorator)(UserDecorator, params);
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
//#endregion
|
|
158
|
-
exports.Auth = Auth
|
|
159
|
-
exports.AuthMiddleware = AuthMiddleware
|
|
160
|
-
exports.AuthProvider = AuthProvider
|
|
161
|
-
exports.User = User
|