@adminforth/two-factors-auth 4.0.1 → 4.0.2

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/build.log CHANGED
@@ -16,5 +16,5 @@ custom/pnpm-lock.yaml
16
16
  custom/tsconfig.json
17
17
  custom/utils.js
18
18
 
19
- sent 286,243 bytes received 229 bytes 572,944.00 bytes/sec
20
- total size is 285,337 speedup is 1.00
19
+ sent 286,356 bytes received 229 bytes 573,170.00 bytes/sec
20
+ total size is 285,450 speedup is 1.00
@@ -110,6 +110,7 @@
110
110
  import { getPasskey } from './utils.js'
111
111
  import { useAdminforth } from '@/adminforth';
112
112
  import websocket from '@/websocket';
113
+ import { getAdminForthClientId } from '@/utils/clientId';
113
114
  import type { AdminUser } from '@/types/Common';
114
115
 
115
116
  type TwoFaConfirmationResult = { mode: 'totp'; result: string } | { mode: 'passkey'; result: Record<string, any> };
@@ -150,7 +151,8 @@
150
151
  watch( props, () => {
151
152
  if (props.adminUser) {
152
153
  websocket.unsubscribeByPrefix(`/user2fa/`);
153
- websocket.subscribe(`/user2fa/${props.adminUser.pk}`, async (data: {sessionId: string}) => {
154
+ const user2FaTopic = `/user2fa/${props.adminUser.pk}/${getAdminForthClientId()}`;
155
+ websocket.subscribe(user2FaTopic, async (data: {sessionId: string}) => {
154
156
  if (!allowAddNewSessions) {
155
157
  alert({message: t('Some process or user tries to add new actions to confirm. Action was blocked'), variant: 'warning'});
156
158
  return;
@@ -188,7 +190,7 @@
188
190
  }
189
191
  allowAddNewSessions = true;
190
192
  });
191
- websocket.subscribe(`/user2fa/${props.adminUser.pk}-resolve`, async (data: {sessionId: string}) => {
193
+ websocket.subscribe(`${user2FaTopic}/resolve`, async (data: {sessionId: string}) => {
192
194
  if (sessionsIdsToResolve.value.includes(data.sessionId) && rejectFn && modelShow.value) {
193
195
  onCancel();
194
196
  sessionsIdsToResolve.value = sessionsIdsToResolve.value.filter(id => id !== data.sessionId);
@@ -110,6 +110,7 @@
110
110
  import { getPasskey } from './utils.js'
111
111
  import { useAdminforth } from '@/adminforth';
112
112
  import websocket from '@/websocket';
113
+ import { getAdminForthClientId } from '@/utils/clientId';
113
114
  import type { AdminUser } from '@/types/Common';
114
115
 
115
116
  type TwoFaConfirmationResult = { mode: 'totp'; result: string } | { mode: 'passkey'; result: Record<string, any> };
@@ -150,7 +151,8 @@
150
151
  watch( props, () => {
151
152
  if (props.adminUser) {
152
153
  websocket.unsubscribeByPrefix(`/user2fa/`);
153
- websocket.subscribe(`/user2fa/${props.adminUser.pk}`, async (data: {sessionId: string}) => {
154
+ const user2FaTopic = `/user2fa/${props.adminUser.pk}/${getAdminForthClientId()}`;
155
+ websocket.subscribe(user2FaTopic, async (data: {sessionId: string}) => {
154
156
  if (!allowAddNewSessions) {
155
157
  alert({message: t('Some process or user tries to add new actions to confirm. Action was blocked'), variant: 'warning'});
156
158
  return;
@@ -188,7 +190,7 @@
188
190
  }
189
191
  allowAddNewSessions = true;
190
192
  });
191
- websocket.subscribe(`/user2fa/${props.adminUser.pk}-resolve`, async (data: {sessionId: string}) => {
193
+ websocket.subscribe(`${user2FaTopic}/resolve`, async (data: {sessionId: string}) => {
192
194
  if (sessionsIdsToResolve.value.includes(data.sessionId) && rejectFn && modelShow.value) {
193
195
  onCancel();
194
196
  sessionsIdsToResolve.value = sessionsIdsToResolve.value.filter(id => id !== data.sessionId);
package/dist/index.js CHANGED
@@ -8,6 +8,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import { AdminForthPlugin, suggestIfTypo } from "adminforth";
11
+ import * as AdminForthRuntime from "adminforth";
11
12
  import { PasskeyService } from "./services/passkeyService.js";
12
13
  import { TotpService } from "./services/totpService.js";
13
14
  import { verifyMfaConfirmation } from "./services/verifyMfaConfirmation.js";
@@ -19,6 +20,7 @@ import { createPasskeyHandlers } from "./handlers/createPasskeyHandlers.js";
19
20
  import { createTwoFaHandlers } from "./handlers/createTwoFaHandlers.js";
20
21
  import { parsePeriod } from "./utils/parsePeriod.js";
21
22
  import crypto from 'crypto';
23
+ const getRequestWebsocketClientId = AdminForthRuntime.getRequestWebsocketClientId;
22
24
  export default class TwoFactorsAuthPlugin extends AdminForthPlugin {
23
25
  constructor(options) {
24
26
  super(options, import.meta.url);
@@ -77,12 +79,16 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin {
77
79
  }
78
80
  verifyAuto(adminUser) {
79
81
  return __awaiter(this, void 0, void 0, function* () {
82
+ const websocketClientId = getRequestWebsocketClientId === null || getRequestWebsocketClientId === void 0 ? void 0 : getRequestWebsocketClientId();
83
+ if (!websocketClientId) {
84
+ return { ok: false, error: '2FA auto verification requires an active browser request context' };
85
+ }
80
86
  const sessionId = crypto.randomUUID();
81
87
  const jwt = this.adminforth.auth.issueJWT({ sessionId, adminUserPk: adminUser.pk }, 'auto2FA', '5m');
82
88
  const resultPromise = this.waitForResponse(jwt, 5 * 60 * 1000);
83
- this.adminforth.websocket.publish(`/user2fa/${adminUser.pk}`, { sessionId: jwt });
89
+ this.adminforth.websocket.publish(`/user2fa/${adminUser.pk}/${websocketClientId}`, { sessionId: jwt });
84
90
  const result = yield resultPromise;
85
- this.adminforth.websocket.publish(`/user2fa/${adminUser.pk}-resolve`, { sessionId: jwt });
91
+ this.adminforth.websocket.publish(`/user2fa/${adminUser.pk}/${websocketClientId}/resolve`, { sessionId: jwt });
86
92
  return result;
87
93
  });
88
94
  }
package/index.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { AdminForthPlugin, suggestIfTypo, HttpExtra } from "adminforth";
2
+ import * as AdminForthRuntime from "adminforth";
2
3
  import type { AdminForthResource, AdminUser, IAdminForth, IHttpServer, IAdminForthAuth, IAdminForthHttpResponse } from "adminforth";
3
4
  import { PluginOptions } from "./types.js"
4
5
  import { PasskeyService } from "./services/passkeyService.js";
@@ -13,6 +14,10 @@ import { createTwoFaHandlers } from "./handlers/createTwoFaHandlers.js";
13
14
  import { parsePeriod } from "./utils/parsePeriod.js";
14
15
  import crypto from 'crypto';
15
16
 
17
+ const getRequestWebsocketClientId = (AdminForthRuntime as typeof AdminForthRuntime & {
18
+ getRequestWebsocketClientId?: () => string | undefined;
19
+ }).getRequestWebsocketClientId;
20
+
16
21
  export default class TwoFactorsAuthPlugin extends AdminForthPlugin {
17
22
  options: PluginOptions;
18
23
  adminforth: IAdminForth;
@@ -87,15 +92,19 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin {
87
92
  }
88
93
 
89
94
  public async verifyAuto(adminUser: AdminUser) {
95
+ const websocketClientId = getRequestWebsocketClientId?.();
96
+ if (!websocketClientId) {
97
+ return { ok: false, error: '2FA auto verification requires an active browser request context' };
98
+ }
90
99
  const sessionId = crypto.randomUUID();
91
100
  const jwt = this.adminforth.auth.issueJWT({sessionId, adminUserPk: adminUser.pk}, 'auto2FA', '5m');
92
101
  const resultPromise = this.waitForResponse(jwt, 5 * 60 * 1000);
93
102
 
94
- this.adminforth.websocket.publish(`/user2fa/${adminUser.pk}`, { sessionId: jwt });
103
+ this.adminforth.websocket.publish(`/user2fa/${adminUser.pk}/${websocketClientId}`, { sessionId: jwt });
95
104
 
96
105
  const result = await resultPromise;
97
106
 
98
- this.adminforth.websocket.publish(`/user2fa/${adminUser.pk}-resolve`, { sessionId: jwt });
107
+ this.adminforth.websocket.publish(`/user2fa/${adminUser.pk}/${websocketClientId}/resolve`, { sessionId: jwt });
99
108
 
100
109
  return result;
101
110
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adminforth/two-factors-auth",
3
- "version": "4.0.1",
3
+ "version": "4.0.2",
4
4
  "description": "AdminForth plugin for two factors authentication",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",