@vercube/auth 1.2.1 → 1.3.1
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 +8 -9
- package/dist/index.mjs +45 -14
- package/package.json +5 -4
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { BaseMiddleware, MiddlewareOptions } from "@vercube/core";
|
|
2
2
|
//#region src/Services/AuthProvider.d.ts
|
|
3
|
-
declare abstract class AuthProvider<U = unknown> {
|
|
3
|
+
export declare abstract class AuthProvider<U = unknown> {
|
|
4
4
|
/**
|
|
5
5
|
* Validate authentication
|
|
6
6
|
* @param {Request} request - The request object
|
|
@@ -17,7 +17,7 @@ declare abstract class AuthProvider<U = unknown> {
|
|
|
17
17
|
}
|
|
18
18
|
//#endregion
|
|
19
19
|
//#region src/Types/AuthTypes.d.ts
|
|
20
|
-
declare namespace AuthTypes {
|
|
20
|
+
export declare namespace AuthTypes {
|
|
21
21
|
/**
|
|
22
22
|
* Middleware options for auth
|
|
23
23
|
*/
|
|
@@ -41,7 +41,7 @@ declare namespace AuthTypes {
|
|
|
41
41
|
* @param options Optional options for the authentication middleware
|
|
42
42
|
* @returns A decorator function that adds authentication middleware to the target
|
|
43
43
|
*/
|
|
44
|
-
declare function Auth(options?: AuthTypes.MiddlewareOptions): Function;
|
|
44
|
+
export declare function Auth(options?: AuthTypes.MiddlewareOptions): Function;
|
|
45
45
|
//#endregion
|
|
46
46
|
//#region src/Decorators/User.d.ts
|
|
47
47
|
/**
|
|
@@ -73,7 +73,7 @@ interface UserDecoratorOptions {
|
|
|
73
73
|
* // user is retrieved using CustomAuthProvider
|
|
74
74
|
* }
|
|
75
75
|
*/
|
|
76
|
-
declare function User(params?: UserDecoratorOptions): Function;
|
|
76
|
+
export declare function User(params?: UserDecoratorOptions): Function;
|
|
77
77
|
//#endregion
|
|
78
78
|
//#region src/Middleware/AuthMiddleware.d.ts
|
|
79
79
|
/**
|
|
@@ -84,10 +84,8 @@ declare function User(params?: UserDecoratorOptions): Function;
|
|
|
84
84
|
* const middleware = new AuthMiddleware();
|
|
85
85
|
* await middleware.use(event);
|
|
86
86
|
*/
|
|
87
|
-
declare class AuthMiddleware implements BaseMiddleware<AuthTypes.MiddlewareOptions> {
|
|
87
|
+
export declare class AuthMiddleware implements BaseMiddleware<AuthTypes.MiddlewareOptions> {
|
|
88
88
|
private gContainer;
|
|
89
|
-
private gLogger;
|
|
90
|
-
private gAuthProvider;
|
|
91
89
|
/**
|
|
92
90
|
* Middleware function that processes the HTTP event.
|
|
93
91
|
*
|
|
@@ -95,8 +93,9 @@ declare class AuthMiddleware implements BaseMiddleware<AuthTypes.MiddlewareOptio
|
|
|
95
93
|
* @param {Response} response - The HTTP response to be processed
|
|
96
94
|
* @param {MiddlewareOptions} args - Additional arguments for the middleware
|
|
97
95
|
* @returns {Promise<void>} - A promise that resolves when the processing is complete.
|
|
96
|
+
* @throws {InternalServerError} - If the auth provider is not registered in the container
|
|
97
|
+
* @throws {UnauthorizedError} - If authentication fails
|
|
98
98
|
*/
|
|
99
99
|
onRequest(request: Request, response: Response, args: MiddlewareOptions<AuthTypes.MiddlewareOptions>): Promise<void>;
|
|
100
100
|
}
|
|
101
|
-
//#endregion
|
|
102
|
-
export { Auth, AuthMiddleware, AuthProvider, AuthTypes, User };
|
|
101
|
+
//#endregion
|
package/dist/index.mjs
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { UnauthorizedError, addMetadataMiddleware, initializeMetadata, initializeMetadataMethod, setMetadataArg } from "@vercube/core";
|
|
2
|
-
import { BaseDecorator, Container, Inject,
|
|
3
|
-
import {
|
|
1
|
+
import { InternalServerError, UnauthorizedError, addMetadataMiddleware, initializeMetadata, initializeMetadataMethod, setMetadataArg } from "@vercube/core";
|
|
2
|
+
import { BaseDecorator, Container, Inject, createDecorator } from "@vercube/di";
|
|
3
|
+
import { ValueType, createInstrument } from "@vercube/telemetry/instrument";
|
|
4
4
|
//#region src/Services/AuthProvider.ts
|
|
5
5
|
var AuthProvider = class {};
|
|
6
6
|
//#endregion
|
|
7
|
-
//#region \0@oxc-project+runtime@0.
|
|
7
|
+
//#region \0@oxc-project+runtime@0.150.0/helpers/esm/decorate.js
|
|
8
8
|
function __decorate(decorators, target, key, desc) {
|
|
9
9
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
10
10
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
@@ -14,6 +14,34 @@ function __decorate(decorators, target, key, desc) {
|
|
|
14
14
|
//#endregion
|
|
15
15
|
//#region src/Middleware/AuthMiddleware.ts
|
|
16
16
|
/**
|
|
17
|
+
* Records auth signals.
|
|
18
|
+
*
|
|
19
|
+
* The toolkit comes from `@vercube/telemetry/instrument`, which is the only
|
|
20
|
+
* place in the framework that speaks to OpenTelemetry directly.
|
|
21
|
+
*/
|
|
22
|
+
const instrument = createInstrument("@vercube/auth");
|
|
23
|
+
/** Attribute carrying how an authentication attempt ended. */
|
|
24
|
+
const AUTH_OUTCOME = "vercube.auth.outcome";
|
|
25
|
+
/**
|
|
26
|
+
* Records the outcome of one authentication attempt.
|
|
27
|
+
*
|
|
28
|
+
* Recorded on the request span rather than as a span of its own: the check is
|
|
29
|
+
* part of serving the request, not a separate operation, and an extra span per
|
|
30
|
+
* request for something that usually takes microseconds buys nothing. No
|
|
31
|
+
* principal, token or header ever leaves this function.
|
|
32
|
+
*
|
|
33
|
+
* @param {string} outcome - `allowed`, `denied` or `unconfigured`
|
|
34
|
+
* @returns {void}
|
|
35
|
+
*/
|
|
36
|
+
function recordOutcome(outcome) {
|
|
37
|
+
instrument.activeSpan()?.addEvent("auth.decision", { [AUTH_OUTCOME]: outcome });
|
|
38
|
+
instrument.counter("vercube.auth.decisions", {
|
|
39
|
+
description: "Authentication decisions by outcome.",
|
|
40
|
+
unit: "{decision}",
|
|
41
|
+
valueType: ValueType.INT
|
|
42
|
+
}).add(1, { [AUTH_OUTCOME]: outcome });
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
17
45
|
* Middleware for auth
|
|
18
46
|
* @implements {BaseMiddleware}
|
|
19
47
|
* @description authorizes incoming request
|
|
@@ -23,8 +51,6 @@ function __decorate(decorators, target, key, desc) {
|
|
|
23
51
|
*/
|
|
24
52
|
var AuthMiddleware = class {
|
|
25
53
|
gContainer;
|
|
26
|
-
gLogger;
|
|
27
|
-
gAuthProvider;
|
|
28
54
|
/**
|
|
29
55
|
* Middleware function that processes the HTTP event.
|
|
30
56
|
*
|
|
@@ -32,21 +58,25 @@ var AuthMiddleware = class {
|
|
|
32
58
|
* @param {Response} response - The HTTP response to be processed
|
|
33
59
|
* @param {MiddlewareOptions} args - Additional arguments for the middleware
|
|
34
60
|
* @returns {Promise<void>} - A promise that resolves when the processing is complete.
|
|
61
|
+
* @throws {InternalServerError} - If the auth provider is not registered in the container
|
|
62
|
+
* @throws {UnauthorizedError} - If authentication fails
|
|
35
63
|
*/
|
|
36
64
|
async onRequest(request, response, args) {
|
|
37
|
-
|
|
38
|
-
|
|
65
|
+
const providerKey = args?.middlewareArgs?.provider ?? AuthProvider;
|
|
66
|
+
const provider = this.gContainer.getOptional(providerKey);
|
|
39
67
|
if (!provider) {
|
|
40
|
-
|
|
41
|
-
|
|
68
|
+
recordOutcome("unconfigured");
|
|
69
|
+
throw new InternalServerError(`AuthMiddleware: auth provider "${providerKey.name}" is not registered in the container`);
|
|
42
70
|
}
|
|
43
71
|
const authenticationError = await provider.validate(request, args.middlewareArgs);
|
|
44
|
-
if (authenticationError)
|
|
72
|
+
if (authenticationError) {
|
|
73
|
+
recordOutcome("denied");
|
|
74
|
+
throw new UnauthorizedError(authenticationError);
|
|
75
|
+
}
|
|
76
|
+
recordOutcome("allowed");
|
|
45
77
|
}
|
|
46
78
|
};
|
|
47
79
|
__decorate([Inject(Container)], AuthMiddleware.prototype, "gContainer", void 0);
|
|
48
|
-
__decorate([InjectOptional(Logger)], AuthMiddleware.prototype, "gLogger", void 0);
|
|
49
|
-
__decorate([InjectOptional(AuthProvider)], AuthMiddleware.prototype, "gAuthProvider", void 0);
|
|
50
80
|
//#endregion
|
|
51
81
|
//#region src/Decorators/Auth.ts
|
|
52
82
|
/**
|
|
@@ -84,7 +114,8 @@ var UserDecorator = class extends BaseDecorator {
|
|
|
84
114
|
*/
|
|
85
115
|
created() {
|
|
86
116
|
initializeMetadata(this.prototype);
|
|
87
|
-
|
|
117
|
+
const method = initializeMetadataMethod(this.prototype, this.propertyName);
|
|
118
|
+
setMetadataArg(method, {
|
|
88
119
|
idx: this.propertyIndex,
|
|
89
120
|
type: "custom",
|
|
90
121
|
resolver: async (event) => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vercube/auth",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.1",
|
|
4
4
|
"description": "Auth module for Vercube framework",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -22,9 +22,10 @@
|
|
|
22
22
|
"README.md"
|
|
23
23
|
],
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@vercube/core": "1.
|
|
26
|
-
"@vercube/
|
|
27
|
-
"@vercube/
|
|
25
|
+
"@vercube/core": "1.3.1",
|
|
26
|
+
"@vercube/di": "1.3.1",
|
|
27
|
+
"@vercube/logger": "1.3.1",
|
|
28
|
+
"@vercube/telemetry": "1.3.1"
|
|
28
29
|
},
|
|
29
30
|
"publishConfig": {
|
|
30
31
|
"access": "public"
|