@quatrain/auth 1.2.2 → 1.2.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.
@@ -1,6 +1,7 @@
1
1
  import { AuthParameters, AuthParametersKeys } from './Auth';
2
2
  import { User } from '@quatrain/backend';
3
3
  import { AuthInterface } from './types/AuthInterface';
4
+ import { ApiMiddleware } from '@quatrain/api';
4
5
  /**
5
6
  * Abstract base class that defines the contract for all authentication adapters.
6
7
  * Implementations should adapt standard authentication methods to specific providers
@@ -12,6 +13,12 @@ export declare abstract class AbstractAuthAdapter implements AuthInterface {
12
13
  protected _alias: string;
13
14
  protected _params: AuthParameters;
14
15
  constructor(params?: AuthParameters);
16
+ /**
17
+ * Express middleware capturing Bearer JWT tokens to execute auth verification.
18
+ *
19
+ * @returns The middleware function.
20
+ */
21
+ middleware(): ApiMiddleware;
15
22
  /**
16
23
  * Assigns a configuration parameter for the adapter.
17
24
  *
@@ -95,4 +102,11 @@ export declare abstract class AbstractAuthAdapter implements AuthInterface {
95
102
  * @returns Action response from the provider.
96
103
  */
97
104
  abstract setCustomUserClaims(id: string, claims: any): any;
105
+ /**
106
+ * Initiates password recovery/reset process for a user.
107
+ *
108
+ * @param email - The user email.
109
+ * @param redirectTo - Optional redirect destination.
110
+ */
111
+ recoverPassword(email: string, redirectTo?: string): Promise<any>;
98
112
  }
@@ -1,7 +1,18 @@
1
1
  "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
2
11
  Object.defineProperty(exports, "__esModule", { value: true });
3
12
  exports.AbstractAuthAdapter = void 0;
13
+ const Auth_1 = require("./Auth");
4
14
  const backend_1 = require("@quatrain/backend");
15
+ const http_1 = require("@quatrain/http");
5
16
  /**
6
17
  * Abstract base class that defines the contract for all authentication adapters.
7
18
  * Implementations should adapt standard authentication methods to specific providers
@@ -13,6 +24,31 @@ class AbstractAuthAdapter {
13
24
  this._params = {};
14
25
  this._params = params;
15
26
  }
27
+ /**
28
+ * Express middleware capturing Bearer JWT tokens to execute auth verification.
29
+ *
30
+ * @returns The middleware function.
31
+ */
32
+ middleware() {
33
+ return (req, res) => __awaiter(this, void 0, void 0, function* () {
34
+ var _a;
35
+ const bearer = http_1.HttpHelper.parseBearerToken((_a = req.headers) === null || _a === void 0 ? void 0 : _a.authorization);
36
+ if (bearer) {
37
+ try {
38
+ const user = yield this.getAuthToken(bearer);
39
+ if (user) {
40
+ return true; // Authorized
41
+ }
42
+ }
43
+ catch (e) {
44
+ Auth_1.Auth.error(`[${this.constructor.name}] Middleware token verification failed: ${e.message}`);
45
+ }
46
+ }
47
+ res.setHeader('WWW-Authenticate', 'Bearer realm="Core API"');
48
+ res.status(http_1.HttpStatus.UNAUTHORIZED).send('Authentication required.');
49
+ return false;
50
+ });
51
+ }
16
52
  /**
17
53
  * Assigns a configuration parameter for the adapter.
18
54
  *
@@ -37,6 +73,15 @@ class AbstractAuthAdapter {
37
73
  get alias() {
38
74
  return this._alias;
39
75
  }
76
+ /**
77
+ * Initiates password recovery/reset process for a user.
78
+ *
79
+ * @param email - The user email.
80
+ * @param redirectTo - Optional redirect destination.
81
+ */
82
+ recoverPassword(email, redirectTo) {
83
+ throw new Error('Password recovery not implemented for this adapter');
84
+ }
40
85
  }
41
86
  exports.AbstractAuthAdapter = AbstractAuthAdapter;
42
87
  /** The `User` class reference to be used by the adapter. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quatrain/auth",
3
- "version": "1.2.2",
3
+ "version": "1.2.4",
4
4
  "license": "AGPL-3.0-only",
5
5
  "description": "Auth adapters commons",
6
6
  "main": "dist/index.js",
@@ -20,8 +20,10 @@
20
20
  },
21
21
  "author": "Quatrain Développement SAS <developers@quatrain.com>",
22
22
  "dependencies": {
23
- "@quatrain/backend": "^1.2.6",
24
- "@quatrain/core": "^1.2.5"
23
+ "@quatrain/api": "^1.1.6",
24
+ "@quatrain/backend": "^1.2.13",
25
+ "@quatrain/core": "^1.2.16",
26
+ "@quatrain/http": "^1.0.0"
25
27
  },
26
28
  "devDependencies": {
27
29
  "@tsconfig/recommended": "^1.0.1",
@@ -1,6 +1,8 @@
1
- import { AuthParameters, AuthParametersKeys } from './Auth'
1
+ import { Auth, AuthParameters, AuthParametersKeys } from './Auth'
2
2
  import { User } from '@quatrain/backend'
3
3
  import { AuthInterface } from './types/AuthInterface'
4
+ import { ApiMiddleware, ApiRequest, ApiResponse } from '@quatrain/api'
5
+ import { HttpStatus, HttpHelper } from '@quatrain/http'
4
6
 
5
7
  /**
6
8
  * Abstract base class that defines the contract for all authentication adapters.
@@ -17,6 +19,32 @@ export abstract class AbstractAuthAdapter implements AuthInterface {
17
19
  this._params = params
18
20
  }
19
21
 
22
+ /**
23
+ * Express middleware capturing Bearer JWT tokens to execute auth verification.
24
+ *
25
+ * @returns The middleware function.
26
+ */
27
+ public middleware(): ApiMiddleware {
28
+ return async (req: ApiRequest, res: ApiResponse): Promise<boolean> => {
29
+ const bearer = HttpHelper.parseBearerToken(req.headers?.authorization as string)
30
+
31
+ if (bearer) {
32
+ try {
33
+ const user = await this.getAuthToken(bearer)
34
+ if (user) {
35
+ return true // Authorized
36
+ }
37
+ } catch(e) {
38
+ Auth.error(`[${this.constructor.name}] Middleware token verification failed: ${(e as Error).message}`)
39
+ }
40
+ }
41
+
42
+ res.setHeader('WWW-Authenticate', 'Bearer realm="Core API"')
43
+ res.status(HttpStatus.UNAUTHORIZED).send('Authentication required.')
44
+ return false
45
+ }
46
+ }
47
+
20
48
  /**
21
49
  * Assigns a configuration parameter for the adapter.
22
50
  *
@@ -120,4 +148,14 @@ export abstract class AbstractAuthAdapter implements AuthInterface {
120
148
  * @returns Action response from the provider.
121
149
  */
122
150
  abstract setCustomUserClaims(id: string, claims: any): any
151
+
152
+ /**
153
+ * Initiates password recovery/reset process for a user.
154
+ *
155
+ * @param email - The user email.
156
+ * @param redirectTo - Optional redirect destination.
157
+ */
158
+ recoverPassword(email: string, redirectTo?: string): Promise<any> {
159
+ throw new Error('Password recovery not implemented for this adapter')
160
+ }
123
161
  }