@vercube/auth 1.2.0 → 1.3.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/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,7 +84,7 @@ 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
89
  private gLogger;
90
90
  private gAuthProvider;
@@ -98,5 +98,4 @@ declare class AuthMiddleware implements BaseMiddleware<AuthTypes.MiddlewareOptio
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,11 @@
1
1
  import { UnauthorizedError, addMetadataMiddleware, initializeMetadata, initializeMetadataMethod, setMetadataArg } from "@vercube/core";
2
2
  import { BaseDecorator, Container, Inject, InjectOptional, createDecorator } from "@vercube/di";
3
3
  import { Logger } from "@vercube/logger";
4
+ import { ValueType, createInstrument } from "@vercube/telemetry/instrument";
4
5
  //#region src/Services/AuthProvider.ts
5
6
  var AuthProvider = class {};
6
7
  //#endregion
7
- //#region \0@oxc-project+runtime@0.140.0/helpers/esm/decorate.js
8
+ //#region \0@oxc-project+runtime@0.150.0/helpers/esm/decorate.js
8
9
  function __decorate(decorators, target, key, desc) {
9
10
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
10
11
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
@@ -14,6 +15,34 @@ function __decorate(decorators, target, key, desc) {
14
15
  //#endregion
15
16
  //#region src/Middleware/AuthMiddleware.ts
16
17
  /**
18
+ * Records auth signals.
19
+ *
20
+ * The toolkit comes from `@vercube/telemetry/instrument`, which is the only
21
+ * place in the framework that speaks to OpenTelemetry directly.
22
+ */
23
+ const instrument = createInstrument("@vercube/auth");
24
+ /** Attribute carrying how an authentication attempt ended. */
25
+ const AUTH_OUTCOME = "vercube.auth.outcome";
26
+ /**
27
+ * Records the outcome of one authentication attempt.
28
+ *
29
+ * Recorded on the request span rather than as a span of its own: the check is
30
+ * part of serving the request, not a separate operation, and an extra span per
31
+ * request for something that usually takes microseconds buys nothing. No
32
+ * principal, token or header ever leaves this function.
33
+ *
34
+ * @param {string} outcome - `allowed`, `denied` or `unconfigured`
35
+ * @returns {void}
36
+ */
37
+ function recordOutcome(outcome) {
38
+ instrument.activeSpan()?.addEvent("auth.decision", { [AUTH_OUTCOME]: outcome });
39
+ instrument.counter("vercube.auth.decisions", {
40
+ description: "Authentication decisions by outcome.",
41
+ unit: "{decision}",
42
+ valueType: ValueType.INT
43
+ }).add(1, { [AUTH_OUTCOME]: outcome });
44
+ }
45
+ /**
17
46
  * Middleware for auth
18
47
  * @implements {BaseMiddleware}
19
48
  * @description authorizes incoming request
@@ -38,10 +67,15 @@ var AuthMiddleware = class {
38
67
  if (args?.middlewareArgs?.provider) provider = this.gContainer.getOptional(args.middlewareArgs.provider);
39
68
  if (!provider) {
40
69
  this.gLogger?.warn("AuthMiddleware::AuthProvider is not registered");
70
+ recordOutcome("unconfigured");
41
71
  return;
42
72
  }
43
73
  const authenticationError = await provider.validate(request, args.middlewareArgs);
44
- if (authenticationError) throw new UnauthorizedError(authenticationError);
74
+ if (authenticationError) {
75
+ recordOutcome("denied");
76
+ throw new UnauthorizedError(authenticationError);
77
+ }
78
+ recordOutcome("allowed");
45
79
  }
46
80
  };
47
81
  __decorate([Inject(Container)], AuthMiddleware.prototype, "gContainer", void 0);
@@ -84,7 +118,8 @@ var UserDecorator = class extends BaseDecorator {
84
118
  */
85
119
  created() {
86
120
  initializeMetadata(this.prototype);
87
- setMetadataArg(initializeMetadataMethod(this.prototype, this.propertyName), {
121
+ const method = initializeMetadataMethod(this.prototype, this.propertyName);
122
+ setMetadataArg(method, {
88
123
  idx: this.propertyIndex,
89
124
  type: "custom",
90
125
  resolver: async (event) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vercube/auth",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
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.2.0",
26
- "@vercube/di": "1.2.0",
27
- "@vercube/logger": "1.2.0"
25
+ "@vercube/core": "1.3.0",
26
+ "@vercube/di": "1.3.0",
27
+ "@vercube/logger": "1.3.0",
28
+ "@vercube/telemetry": "1.3.0"
28
29
  },
29
30
  "publishConfig": {
30
31
  "access": "public"