@quatrain/auth 1.2.2 → 1.2.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.
@@ -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
  *
@@ -1,6 +1,16 @@
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");
5
15
  /**
6
16
  * Abstract base class that defines the contract for all authentication adapters.
@@ -13,6 +23,31 @@ class AbstractAuthAdapter {
13
23
  this._params = {};
14
24
  this._params = params;
15
25
  }
26
+ /**
27
+ * Express middleware capturing Bearer JWT tokens to execute auth verification.
28
+ *
29
+ * @returns The middleware function.
30
+ */
31
+ middleware() {
32
+ return (req, res) => __awaiter(this, void 0, void 0, function* () {
33
+ var _a;
34
+ const bearer = (((_a = req.headers) === null || _a === void 0 ? void 0 : _a.authorization) || '').split(' ')[1] || '';
35
+ if (bearer) {
36
+ try {
37
+ const user = yield this.getAuthToken(bearer);
38
+ if (user) {
39
+ return true; // Authorized
40
+ }
41
+ }
42
+ catch (e) {
43
+ Auth_1.Auth.error(`[${this.constructor.name}] Middleware token verification failed: ${e.message}`);
44
+ }
45
+ }
46
+ res.setHeader('WWW-Authenticate', 'Bearer realm="Core API"');
47
+ res.status(401).send('Authentication required.');
48
+ return false;
49
+ });
50
+ }
16
51
  /**
17
52
  * Assigns a configuration parameter for the adapter.
18
53
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quatrain/auth",
3
- "version": "1.2.2",
3
+ "version": "1.2.3",
4
4
  "license": "AGPL-3.0-only",
5
5
  "description": "Auth adapters commons",
6
6
  "main": "dist/index.js",
@@ -20,8 +20,9 @@
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.5",
24
+ "@quatrain/backend": "^1.2.11",
25
+ "@quatrain/core": "^1.2.14"
25
26
  },
26
27
  "devDependencies": {
27
28
  "@tsconfig/recommended": "^1.0.1",
@@ -1,6 +1,7 @@
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'
4
5
 
5
6
  /**
6
7
  * Abstract base class that defines the contract for all authentication adapters.
@@ -17,6 +18,32 @@ export abstract class AbstractAuthAdapter implements AuthInterface {
17
18
  this._params = params
18
19
  }
19
20
 
21
+ /**
22
+ * Express middleware capturing Bearer JWT tokens to execute auth verification.
23
+ *
24
+ * @returns The middleware function.
25
+ */
26
+ public middleware(): ApiMiddleware {
27
+ return async (req: ApiRequest, res: ApiResponse): Promise<boolean> => {
28
+ const bearer = ((req.headers?.authorization as string) || '').split(' ')[1] || ''
29
+
30
+ if (bearer) {
31
+ try {
32
+ const user = await this.getAuthToken(bearer)
33
+ if (user) {
34
+ return true // Authorized
35
+ }
36
+ } catch(e) {
37
+ Auth.error(`[${this.constructor.name}] Middleware token verification failed: ${(e as Error).message}`)
38
+ }
39
+ }
40
+
41
+ res.setHeader('WWW-Authenticate', 'Bearer realm="Core API"')
42
+ res.status(401).send('Authentication required.')
43
+ return false
44
+ }
45
+ }
46
+
20
47
  /**
21
48
  * Assigns a configuration parameter for the adapter.
22
49
  *