@vercube/auth 0.0.1-beta.0 → 0.0.1-beta.3
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 +29 -0
- package/dist/Decorators/{Authenticate.d.ts → Auth.d.ts} +2 -2
- package/dist/Decorators/User.d.ts +32 -0
- package/dist/Middleware/{AuthenticationMiddleware.d.ts → AuthMiddleware.d.ts} +7 -8
- package/dist/Services/AuthProvider.d.ts +23 -0
- package/dist/Types/AuthTypes.d.ts +15 -0
- package/dist/index.cjs +38 -63
- package/dist/index.d.ts +5 -6
- package/dist/index.mjs +37 -60
- package/package.json +6 -5
- package/dist/Decorators/Authorize.d.ts +0 -8
- package/dist/Middleware/AuthorizationMiddleware.d.ts +0 -28
- package/dist/Services/AuthenticationProvider.d.ts +0 -15
- package/dist/Services/AuthorizationProvider.d.ts +0 -16
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025-present - Vercube
|
|
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,29 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
<a href="https://vercube.dev/"><img src="../../.github/assets/logo.png" alt="Vite logo" width="200"></a>
|
|
3
|
+
<br>
|
|
4
|
+
<br>
|
|
5
|
+
|
|
6
|
+
# Vercube
|
|
7
|
+
|
|
8
|
+
Next generation HTTP framework
|
|
9
|
+
|
|
10
|
+
<a href="https://www.npmjs.com/package/@vercube/cli">
|
|
11
|
+
<img src="https://img.shields.io/npm/v/%40vercube%2Fcore?style=for-the-badge&logo=npm&color=%23767eff" alt="npm"/>
|
|
12
|
+
</a>
|
|
13
|
+
<a href="https://www.npmjs.com/package/@vercube/cli">
|
|
14
|
+
<img src="https://img.shields.io/npm/dm/%40vercube%2Fcore?style=for-the-badge&logo=npm&color=%23767eff" alt="npm"/>
|
|
15
|
+
</a>
|
|
16
|
+
<a href="https://github.com/vercube/vercube/blob/main/LICENSE" target="_blank">
|
|
17
|
+
<img src="https://img.shields.io/npm/l/%40vercube%2Fcore?style=for-the-badge&color=%23767eff" alt="License"/>
|
|
18
|
+
</a>
|
|
19
|
+
<br/>
|
|
20
|
+
<br/>
|
|
21
|
+
</div>
|
|
22
|
+
|
|
23
|
+
An ultra-efficient JavaScript server framework that runs anywhere - Node.js, Bun, or Deno - with unmatched flexibility and complete configurability for developers who refuse to sacrifice speed or control.
|
|
24
|
+
|
|
25
|
+
## Auth module
|
|
26
|
+
The Auth module provides a flexible and extensible authentication and authorization system for Vercube applications. It offers a unified interface for different authentication implementations through a dependency injection-based architecture.
|
|
27
|
+
|
|
28
|
+
## <a name="documentation">📖 Documentation</a>
|
|
29
|
+
Comprehensive documentation is available at [vercube.dev](https://vercube.dev). There you'll find detailed module descriptions, project information, guides, and everything else you need to know about Vercube.
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { AuthTypes } from "../Types/AuthTypes.js";
|
|
2
2
|
/**
|
|
3
3
|
* Authentication decorator that adds middleware to protect routes or controllers
|
|
4
4
|
* @param options Optional options for the authentication middleware
|
|
5
5
|
* @returns A decorator function that adds authentication middleware to the target
|
|
6
6
|
*/
|
|
7
|
-
export declare function
|
|
7
|
+
export declare function Auth(options?: AuthTypes.MiddlewareOptions): Function;
|
|
@@ -0,0 +1,32 @@
|
|
|
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,18 +1,17 @@
|
|
|
1
1
|
import { type BaseMiddleware, type MiddlewareOptions } from "@vercube/core";
|
|
2
|
-
import {
|
|
2
|
+
import type { AuthTypes } from "../Types/AuthTypes.js";
|
|
3
3
|
/**
|
|
4
|
-
* Middleware for
|
|
5
|
-
* @class AuthenticationMiddleware
|
|
4
|
+
* Middleware for auth
|
|
6
5
|
* @implements {BaseMiddleware}
|
|
7
|
-
* @description
|
|
6
|
+
* @description authorizes incoming request
|
|
8
7
|
* @example
|
|
9
|
-
* const middleware = new
|
|
8
|
+
* const middleware = new AuthMiddleware();
|
|
10
9
|
* await middleware.use(event);
|
|
11
10
|
*/
|
|
12
|
-
export declare class
|
|
11
|
+
export declare class AuthMiddleware implements BaseMiddleware<AuthTypes.MiddlewareOptions> {
|
|
13
12
|
private gContainer;
|
|
14
|
-
private gAuthenticationProvider;
|
|
15
13
|
private gLogger;
|
|
14
|
+
private gAuthProvider;
|
|
16
15
|
/**
|
|
17
16
|
* Middleware function that processes the HTTP event.
|
|
18
17
|
*
|
|
@@ -21,5 +20,5 @@ export declare class AuthenticationMiddleware implements BaseMiddleware<Authenti
|
|
|
21
20
|
* @param {MiddlewareOptions} args - Additional arguments for the middleware
|
|
22
21
|
* @returns {Promise<void>} - A promise that resolves when the processing is complete.
|
|
23
22
|
*/
|
|
24
|
-
onRequest(request: Request, response: Response, args: MiddlewareOptions<
|
|
23
|
+
onRequest(request: Request, response: Response, args: MiddlewareOptions<AuthTypes.MiddlewareOptions>): Promise<void>;
|
|
25
24
|
}
|
|
@@ -0,0 +1,23 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
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
CHANGED
|
@@ -28,8 +28,8 @@ const __vercube_core = __toESM(require("@vercube/core"));
|
|
|
28
28
|
const __vercube_di = __toESM(require("@vercube/di"));
|
|
29
29
|
const __vercube_logger = __toESM(require("@vercube/logger"));
|
|
30
30
|
|
|
31
|
-
//#region packages/auth/src/Services/
|
|
32
|
-
var
|
|
31
|
+
//#region packages/auth/src/Services/AuthProvider.ts
|
|
32
|
+
var AuthProvider = class {};
|
|
33
33
|
|
|
34
34
|
//#endregion
|
|
35
35
|
//#region node_modules/.pnpm/@oxc-project+runtime@0.63.0/node_modules/@oxc-project/runtime/src/helpers/decorate.js
|
|
@@ -45,11 +45,11 @@ var require_decorate = __commonJS({ "node_modules/.pnpm/@oxc-project+runtime@0.6
|
|
|
45
45
|
var import_decorate = __toESM(require_decorate(), 1);
|
|
46
46
|
|
|
47
47
|
//#endregion
|
|
48
|
-
//#region packages/auth/src/Middleware/
|
|
49
|
-
var
|
|
48
|
+
//#region packages/auth/src/Middleware/AuthMiddleware.ts
|
|
49
|
+
var AuthMiddleware = class {
|
|
50
50
|
gContainer;
|
|
51
|
-
gAuthenticationProvider;
|
|
52
51
|
gLogger;
|
|
52
|
+
gAuthProvider;
|
|
53
53
|
/**
|
|
54
54
|
* Middleware function that processes the HTTP event.
|
|
55
55
|
*
|
|
@@ -59,90 +59,65 @@ var AuthenticationMiddleware = class {
|
|
|
59
59
|
* @returns {Promise<void>} - A promise that resolves when the processing is complete.
|
|
60
60
|
*/
|
|
61
61
|
async onRequest(request, response, args) {
|
|
62
|
-
let provider = this.
|
|
62
|
+
let provider = this.gAuthProvider;
|
|
63
63
|
if (args?.middlewareArgs?.provider) provider = this.gContainer.getOptional(args.middlewareArgs.provider);
|
|
64
64
|
if (!provider) {
|
|
65
|
-
this.gLogger?.warn("
|
|
65
|
+
this.gLogger?.warn("AuthMiddleware::AuthProvider is not registered");
|
|
66
66
|
return;
|
|
67
67
|
}
|
|
68
|
-
const authenticationError = await provider.
|
|
68
|
+
const authenticationError = await provider.validate(request, args.middlewareArgs);
|
|
69
69
|
if (authenticationError) throw new __vercube_core.UnauthorizedError(authenticationError);
|
|
70
70
|
}
|
|
71
71
|
};
|
|
72
|
-
(0, import_decorate.default)([(0, __vercube_di.Inject)(__vercube_di.Container)],
|
|
73
|
-
(0, import_decorate.default)([(0, __vercube_di.InjectOptional)(
|
|
74
|
-
(0, import_decorate.default)([(0, __vercube_di.InjectOptional)(
|
|
72
|
+
(0, import_decorate.default)([(0, __vercube_di.Inject)(__vercube_di.Container)], AuthMiddleware.prototype, "gContainer", void 0);
|
|
73
|
+
(0, import_decorate.default)([(0, __vercube_di.InjectOptional)(__vercube_logger.Logger)], AuthMiddleware.prototype, "gLogger", void 0);
|
|
74
|
+
(0, import_decorate.default)([(0, __vercube_di.InjectOptional)(AuthProvider)], AuthMiddleware.prototype, "gAuthProvider", void 0);
|
|
75
75
|
|
|
76
76
|
//#endregion
|
|
77
|
-
//#region packages/auth/src/Decorators/
|
|
78
|
-
function
|
|
77
|
+
//#region packages/auth/src/Decorators/Auth.ts
|
|
78
|
+
function Auth(options) {
|
|
79
79
|
return function internalDecorator(target, propertyName) {
|
|
80
80
|
const meta = (0, __vercube_core.initializeMetadata)(propertyName ? target : target.prototype);
|
|
81
81
|
if (propertyName) (0, __vercube_core.initializeMetadataMethod)(target, propertyName);
|
|
82
82
|
meta.__middlewares.push({
|
|
83
83
|
target: propertyName ?? "__global__",
|
|
84
84
|
priority: -999,
|
|
85
|
-
middleware:
|
|
85
|
+
middleware: AuthMiddleware,
|
|
86
86
|
args: { ...options }
|
|
87
87
|
});
|
|
88
88
|
};
|
|
89
89
|
}
|
|
90
90
|
|
|
91
91
|
//#endregion
|
|
92
|
-
//#region packages/auth/src/
|
|
93
|
-
var
|
|
94
|
-
|
|
95
|
-
//#endregion
|
|
96
|
-
//#region packages/auth/src/Middleware/AuthorizationMiddleware.ts
|
|
97
|
-
var AuthorizationMiddleware = class {
|
|
92
|
+
//#region packages/auth/src/Decorators/User.ts
|
|
93
|
+
var UserDecorator = class extends __vercube_di.BaseDecorator {
|
|
98
94
|
gContainer;
|
|
99
|
-
gAuthorizationProvider;
|
|
100
|
-
gLogger;
|
|
101
95
|
/**
|
|
102
|
-
*
|
|
103
|
-
*
|
|
104
|
-
*
|
|
105
|
-
* @param {Response} response - The HTTP response event.
|
|
106
|
-
* @param {MiddlewareOptions} args - Additional arguments for the middleware
|
|
107
|
-
* @returns {Promise<void>} - A promise that resolves when the processing is complete.
|
|
96
|
+
* Called when the decorator is created.
|
|
97
|
+
* Initializes metadata for the property and adds parameter information
|
|
98
|
+
* to handle user injection in controller methods.
|
|
108
99
|
*/
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
this.
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
};
|
|
120
|
-
(0, import_decorate.default)([(0, __vercube_di.Inject)(__vercube_di.Container)], AuthorizationMiddleware.prototype, "gContainer", void 0);
|
|
121
|
-
(0, import_decorate.default)([(0, __vercube_di.InjectOptional)(AuthorizationProvider)], AuthorizationMiddleware.prototype, "gAuthorizationProvider", void 0);
|
|
122
|
-
(0, import_decorate.default)([(0, __vercube_di.InjectOptional)(__vercube_logger.Logger)], AuthorizationMiddleware.prototype, "gLogger", void 0);
|
|
123
|
-
|
|
124
|
-
//#endregion
|
|
125
|
-
//#region packages/auth/src/Decorators/Authorize.ts
|
|
126
|
-
function Authorize(params, options) {
|
|
127
|
-
return function internalDecorator(target, propertyName) {
|
|
128
|
-
const meta = (0, __vercube_core.initializeMetadata)(propertyName ? target : target.prototype);
|
|
129
|
-
if (propertyName) (0, __vercube_core.initializeMetadataMethod)(target, propertyName);
|
|
130
|
-
meta.__middlewares.push({
|
|
131
|
-
target: propertyName ?? "__global__",
|
|
132
|
-
priority: -998,
|
|
133
|
-
middleware: AuthorizationMiddleware,
|
|
134
|
-
args: {
|
|
135
|
-
params,
|
|
136
|
-
options
|
|
100
|
+
created() {
|
|
101
|
+
(0, __vercube_core.initializeMetadata)(this.prototype);
|
|
102
|
+
const method = (0, __vercube_core.initializeMetadataMethod)(this.prototype, this.propertyName);
|
|
103
|
+
method.args.push({
|
|
104
|
+
idx: this.propertyIndex,
|
|
105
|
+
type: "custom",
|
|
106
|
+
resolver: async (event) => {
|
|
107
|
+
const provider = this.options?.provider ? this.gContainer.getOptional(this.options.provider) : this.gContainer.getOptional(AuthProvider);
|
|
108
|
+
if (!provider) return null;
|
|
109
|
+
return provider.getCurrentUser(event.request);
|
|
137
110
|
}
|
|
138
111
|
});
|
|
139
|
-
}
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
(0, import_decorate.default)([(0, __vercube_di.Inject)(__vercube_di.Container)], UserDecorator.prototype, "gContainer", void 0);
|
|
115
|
+
function User(params) {
|
|
116
|
+
return (0, __vercube_di.createDecorator)(UserDecorator, params);
|
|
140
117
|
}
|
|
141
118
|
|
|
142
119
|
//#endregion
|
|
143
|
-
exports.
|
|
144
|
-
exports.
|
|
145
|
-
exports.
|
|
146
|
-
exports.
|
|
147
|
-
exports.AuthorizationProvider = AuthorizationProvider
|
|
148
|
-
exports.Authorize = Authorize
|
|
120
|
+
exports.Auth = Auth
|
|
121
|
+
exports.AuthMiddleware = AuthMiddleware
|
|
122
|
+
exports.AuthProvider = AuthProvider
|
|
123
|
+
exports.User = User
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
export * from "./Decorators/
|
|
2
|
-
export * from "./Decorators/
|
|
3
|
-
export * from "./Middleware/
|
|
4
|
-
export * from "./
|
|
5
|
-
export * from "./
|
|
6
|
-
export * from "./Services/AuthorizationProvider.js";
|
|
1
|
+
export * from "./Decorators/Auth.js";
|
|
2
|
+
export * from "./Decorators/User.js";
|
|
3
|
+
export * from "./Middleware/AuthMiddleware.js";
|
|
4
|
+
export * from "./Services/AuthProvider.js";
|
|
5
|
+
export * from "./Types/AuthTypes.js";
|
package/dist/index.mjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { Container, Inject, InjectOptional } from "@vercube/di";
|
|
1
|
+
import { UnauthorizedError, initializeMetadata, initializeMetadataMethod } from "@vercube/core";
|
|
2
|
+
import { BaseDecorator, Container, Inject, InjectOptional, createDecorator } from "@vercube/di";
|
|
3
3
|
import { Logger } from "@vercube/logger";
|
|
4
4
|
|
|
5
|
-
//#region packages/auth/src/Services/
|
|
6
|
-
var
|
|
5
|
+
//#region packages/auth/src/Services/AuthProvider.ts
|
|
6
|
+
var AuthProvider = class {};
|
|
7
7
|
|
|
8
8
|
//#endregion
|
|
9
9
|
//#region node_modules/.pnpm/@oxc-project+runtime@0.63.0/node_modules/@oxc-project/runtime/src/helpers/esm/decorate.js
|
|
@@ -15,11 +15,11 @@ function __decorate(decorators, target, key, desc) {
|
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
//#endregion
|
|
18
|
-
//#region packages/auth/src/Middleware/
|
|
19
|
-
var
|
|
18
|
+
//#region packages/auth/src/Middleware/AuthMiddleware.ts
|
|
19
|
+
var AuthMiddleware = class {
|
|
20
20
|
gContainer;
|
|
21
|
-
gAuthenticationProvider;
|
|
22
21
|
gLogger;
|
|
22
|
+
gAuthProvider;
|
|
23
23
|
/**
|
|
24
24
|
* Middleware function that processes the HTTP event.
|
|
25
25
|
*
|
|
@@ -29,85 +29,62 @@ var AuthenticationMiddleware = class {
|
|
|
29
29
|
* @returns {Promise<void>} - A promise that resolves when the processing is complete.
|
|
30
30
|
*/
|
|
31
31
|
async onRequest(request, response, args) {
|
|
32
|
-
let provider = this.
|
|
32
|
+
let provider = this.gAuthProvider;
|
|
33
33
|
if (args?.middlewareArgs?.provider) provider = this.gContainer.getOptional(args.middlewareArgs.provider);
|
|
34
34
|
if (!provider) {
|
|
35
|
-
this.gLogger?.warn("
|
|
35
|
+
this.gLogger?.warn("AuthMiddleware::AuthProvider is not registered");
|
|
36
36
|
return;
|
|
37
37
|
}
|
|
38
|
-
const authenticationError = await provider.
|
|
38
|
+
const authenticationError = await provider.validate(request, args.middlewareArgs);
|
|
39
39
|
if (authenticationError) throw new UnauthorizedError(authenticationError);
|
|
40
40
|
}
|
|
41
41
|
};
|
|
42
|
-
__decorate([Inject(Container)],
|
|
43
|
-
__decorate([InjectOptional(
|
|
44
|
-
__decorate([InjectOptional(
|
|
42
|
+
__decorate([Inject(Container)], AuthMiddleware.prototype, "gContainer", void 0);
|
|
43
|
+
__decorate([InjectOptional(Logger)], AuthMiddleware.prototype, "gLogger", void 0);
|
|
44
|
+
__decorate([InjectOptional(AuthProvider)], AuthMiddleware.prototype, "gAuthProvider", void 0);
|
|
45
45
|
|
|
46
46
|
//#endregion
|
|
47
|
-
//#region packages/auth/src/Decorators/
|
|
48
|
-
function
|
|
47
|
+
//#region packages/auth/src/Decorators/Auth.ts
|
|
48
|
+
function Auth(options) {
|
|
49
49
|
return function internalDecorator(target, propertyName) {
|
|
50
50
|
const meta = initializeMetadata(propertyName ? target : target.prototype);
|
|
51
51
|
if (propertyName) initializeMetadataMethod(target, propertyName);
|
|
52
52
|
meta.__middlewares.push({
|
|
53
53
|
target: propertyName ?? "__global__",
|
|
54
54
|
priority: -999,
|
|
55
|
-
middleware:
|
|
55
|
+
middleware: AuthMiddleware,
|
|
56
56
|
args: { ...options }
|
|
57
57
|
});
|
|
58
58
|
};
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
//#endregion
|
|
62
|
-
//#region packages/auth/src/
|
|
63
|
-
var
|
|
64
|
-
|
|
65
|
-
//#endregion
|
|
66
|
-
//#region packages/auth/src/Middleware/AuthorizationMiddleware.ts
|
|
67
|
-
var AuthorizationMiddleware = class {
|
|
62
|
+
//#region packages/auth/src/Decorators/User.ts
|
|
63
|
+
var UserDecorator = class extends BaseDecorator {
|
|
68
64
|
gContainer;
|
|
69
|
-
gAuthorizationProvider;
|
|
70
|
-
gLogger;
|
|
71
65
|
/**
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
* @param {Response} response - The HTTP response event.
|
|
76
|
-
* @param {MiddlewareOptions} args - Additional arguments for the middleware
|
|
77
|
-
* @returns {Promise<void>} - A promise that resolves when the processing is complete.
|
|
66
|
+
* Called when the decorator is created.
|
|
67
|
+
* Initializes metadata for the property and adds parameter information
|
|
68
|
+
* to handle user injection in controller methods.
|
|
78
69
|
*/
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
this.
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
};
|
|
90
|
-
__decorate([Inject(Container)], AuthorizationMiddleware.prototype, "gContainer", void 0);
|
|
91
|
-
__decorate([InjectOptional(AuthorizationProvider)], AuthorizationMiddleware.prototype, "gAuthorizationProvider", void 0);
|
|
92
|
-
__decorate([InjectOptional(Logger)], AuthorizationMiddleware.prototype, "gLogger", void 0);
|
|
93
|
-
|
|
94
|
-
//#endregion
|
|
95
|
-
//#region packages/auth/src/Decorators/Authorize.ts
|
|
96
|
-
function Authorize(params, options) {
|
|
97
|
-
return function internalDecorator(target, propertyName) {
|
|
98
|
-
const meta = initializeMetadata(propertyName ? target : target.prototype);
|
|
99
|
-
if (propertyName) initializeMetadataMethod(target, propertyName);
|
|
100
|
-
meta.__middlewares.push({
|
|
101
|
-
target: propertyName ?? "__global__",
|
|
102
|
-
priority: -998,
|
|
103
|
-
middleware: AuthorizationMiddleware,
|
|
104
|
-
args: {
|
|
105
|
-
params,
|
|
106
|
-
options
|
|
70
|
+
created() {
|
|
71
|
+
initializeMetadata(this.prototype);
|
|
72
|
+
const method = initializeMetadataMethod(this.prototype, this.propertyName);
|
|
73
|
+
method.args.push({
|
|
74
|
+
idx: this.propertyIndex,
|
|
75
|
+
type: "custom",
|
|
76
|
+
resolver: async (event) => {
|
|
77
|
+
const provider = this.options?.provider ? this.gContainer.getOptional(this.options.provider) : this.gContainer.getOptional(AuthProvider);
|
|
78
|
+
if (!provider) return null;
|
|
79
|
+
return provider.getCurrentUser(event.request);
|
|
107
80
|
}
|
|
108
81
|
});
|
|
109
|
-
}
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
__decorate([Inject(Container)], UserDecorator.prototype, "gContainer", void 0);
|
|
85
|
+
function User(params) {
|
|
86
|
+
return createDecorator(UserDecorator, params);
|
|
110
87
|
}
|
|
111
88
|
|
|
112
89
|
//#endregion
|
|
113
|
-
export {
|
|
90
|
+
export { Auth, AuthMiddleware, AuthProvider, User };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vercube/auth",
|
|
3
|
-
"version": "0.0.1-beta.
|
|
3
|
+
"version": "0.0.1-beta.3",
|
|
4
4
|
"description": "Auth module for Vercube framework",
|
|
5
5
|
"repository": "@vercube/auth",
|
|
6
6
|
"license": "MIT",
|
|
@@ -17,12 +17,13 @@
|
|
|
17
17
|
"module": "./dist/index.mjs",
|
|
18
18
|
"types": "./dist/index.d.ts",
|
|
19
19
|
"files": [
|
|
20
|
-
"dist"
|
|
20
|
+
"dist",
|
|
21
|
+
"README.md"
|
|
21
22
|
],
|
|
22
23
|
"dependencies": {
|
|
23
|
-
"@vercube/
|
|
24
|
-
"@vercube/
|
|
25
|
-
"@vercube/
|
|
24
|
+
"@vercube/di": "0.0.1-beta.3",
|
|
25
|
+
"@vercube/logger": "0.0.1-beta.3",
|
|
26
|
+
"@vercube/core": "0.0.1-beta.3"
|
|
26
27
|
},
|
|
27
28
|
"publishConfig": {
|
|
28
29
|
"access": "public"
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { AuthorizationTypes } from "../Types/AuthorizationTypes.js";
|
|
2
|
-
/**
|
|
3
|
-
* Authorization decorator that adds middleware to protect routes or controllers
|
|
4
|
-
* @param params Parameters for the authorization middleware
|
|
5
|
-
* @param options Optional options for the authorization middleware
|
|
6
|
-
* @returns A decorator function that adds authorization middleware to the target
|
|
7
|
-
*/
|
|
8
|
-
export declare function Authorize<T>(params: T, options?: AuthorizationTypes.MiddlewareOptions<T>): Function;
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { type BaseMiddleware, type MiddlewareOptions } from "@vercube/core";
|
|
2
|
-
import { AuthorizationTypes } from "../Types/AuthorizationTypes.js";
|
|
3
|
-
/**
|
|
4
|
-
* Middleware for authorization
|
|
5
|
-
* @class AuthorizationMiddleware
|
|
6
|
-
* @implements {BaseMiddleware}
|
|
7
|
-
* @description Authorizes incoming request
|
|
8
|
-
* @example
|
|
9
|
-
* const middleware = new AuthorizationMiddleware<UserType>();
|
|
10
|
-
* await middleware.use(event, args);
|
|
11
|
-
*/
|
|
12
|
-
export declare class AuthorizationMiddleware<T> implements BaseMiddleware {
|
|
13
|
-
private gContainer;
|
|
14
|
-
private gAuthorizationProvider;
|
|
15
|
-
private gLogger;
|
|
16
|
-
/**
|
|
17
|
-
* Middleware function that processes the HTTP event.
|
|
18
|
-
*
|
|
19
|
-
* @param {Request} request - The HTTP request event.
|
|
20
|
-
* @param {Response} response - The HTTP response event.
|
|
21
|
-
* @param {MiddlewareOptions} args - Additional arguments for the middleware
|
|
22
|
-
* @returns {Promise<void>} - A promise that resolves when the processing is complete.
|
|
23
|
-
*/
|
|
24
|
-
onRequest(request: Request, response: Response, args: MiddlewareOptions<{
|
|
25
|
-
options: AuthorizationTypes.MiddlewareOptions
|
|
26
|
-
params: T
|
|
27
|
-
}>): Promise<void>;
|
|
28
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Abstract class representing an authentication provider
|
|
3
|
-
* Provides a common interface for different authentication implementations
|
|
4
|
-
*
|
|
5
|
-
* @abstract
|
|
6
|
-
* @class AuthenticationProvider
|
|
7
|
-
*/
|
|
8
|
-
export declare abstract class AuthenticationProvider {
|
|
9
|
-
/**
|
|
10
|
-
* Authenticates based on the HTTP event
|
|
11
|
-
* @param request - The HTTP event containing the request
|
|
12
|
-
* @returns An error string or Promise of error string, null or Promise of null if authentication is successful
|
|
13
|
-
*/
|
|
14
|
-
abstract authenticate(request: Request): Promise<string | null> | string | null;
|
|
15
|
-
}
|
|
@@ -1,16 +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
|
-
export declare abstract class AuthorizationProvider<T> {
|
|
9
|
-
/**
|
|
10
|
-
* Authorizes based on given params
|
|
11
|
-
* @param {Request} request - The request object
|
|
12
|
-
* @param {T} params - Additional parameters
|
|
13
|
-
* @returns An error string or Promise of error string, null or Promise of null if authentication is successful
|
|
14
|
-
*/
|
|
15
|
-
abstract authorize(request: Request, params: T): Promise<string | null> | string | null;
|
|
16
|
-
}
|