Skip to content

Commit 6bed96e

Browse files
committed
Merge branch 'next' of github.com:devforth/adminforth into next
2 parents 77ed295 + 5c26211 commit 6bed96e

5 files changed

Lines changed: 74 additions & 5 deletions

File tree

adminforth/documentation/docs/tutorial/08-Plugins/01-agent.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ async function allowedForSuperAdmins({ adminUser }: { adminUser: AdminUser }): P
4343
}
4444

4545
export default {
46-
dataSource: 'sqlite',
46+
dataSource: 'maindb',
4747
table: 'sessions',
4848
resourceId: 'sessions',
4949
label: 'Sessions',
@@ -102,7 +102,7 @@ async function allowedForSuperAdmins({ adminUser }: { adminUser: AdminUser }): P
102102
}
103103

104104
export default {
105-
dataSource: 'sqlite',
105+
dataSource: 'maindb',
106106
table: 'turns',
107107
resourceId: 'turns',
108108
label: 'Turns',
@@ -514,7 +514,7 @@ import { AdminForthDataTypes } from 'adminforth';
514514
import type { AdminForthResourceInput } from 'adminforth';
515515

516516
export default {
517-
dataSource: 'sqlite',
517+
dataSource: 'maindb',
518518
table: 'checkpoints',
519519
resourceId: 'checkpoints',
520520
label: 'Checkpoints',

adminforth/documentation/docs/tutorial/08-Plugins/02-TwoFactorsAuth.md

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,52 @@ plugins: [
227227
228228
So such users will have suggestion to setup 2FA, but will be able to skip it with "Skip for now" button.
229229
230+
## Calling 2FA modal from backend
231+
If you wan't to call 2FA verification modal from the backend for verification, you can use
230232
231-
## Step-Up MFA (Two-Factor re-authentication on critical operations)
233+
```ts
234+
t2fa.verifyAuto(adminUser);
235+
```
236+
This method opens 2FA verification modal at the frontend and then returns verification result.
237+
238+
Here is an example:
239+
240+
```ts title="./api.ts"
241+
//diff-add
242+
app.get(`${admin.config.baseUrl}/api/test2faCall/`,
243+
//diff-add
244+
admin.express.authorize(
245+
//diff-add
246+
async (_req: IAdminUserExpressRequest, res: Response) => {
247+
//diff-add
248+
const { adminUser } = _req;
249+
//diff-add
250+
//diff-add
251+
const t2fa = admin.getPluginByClassName('TwoFactorsAuthPlugin');
252+
//diff-add
253+
const verifyResult = await t2fa.verifyAuto(adminUser);
254+
//diff-add
255+
if (verifyResult.ok) {
256+
//diff-add
257+
//some critical action
258+
//diff-add
259+
res.json({ ok: "true" });
260+
//diff-add
261+
}
262+
//diff-add
263+
res.json({ok: "false", message: "Verification failed"})
264+
//diff-add
265+
}
266+
//diff-add
267+
)
268+
//diff-add
269+
);
270+
```
271+
272+
Under the hood, this metod uses websocket.
273+
274+
## Manual Step-Up MFA (Two-Factor re-authentication on critical operations)
275+
But if you websocket doesn't work in you application, or you wan't to perform verification manually, here are manual verification examples
232276
233277
### Request 2FA on custom Actions
234278
@@ -719,11 +763,12 @@ Now, update the settings of the Two-Factor Authentication plugin:
719763

720764
plugins: [
721765
new TwoFactorsAuthPlugin ({
722-
keyValueAdapter: new RamKeyValueAdapter(),
723766
twoFaSecretFieldName: 'secret2fa',
724767
timeStepWindow: 1,
725768
//diff-add
726769
passkeys: {
770+
//diff-add
771+
keyValueAdapter: new RamKeyValueAdapter(),
727772
//diff-add
728773
credentialResourceID: "passkeys",
729774
//diff-add

adminforth/spa/src/App.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135
<component
136136
v-for="c in coreStore?.config?.globalInjections?.everyPageBottom || []"
137137
:is="getCustomComponent(c)"
138+
:adminUser="coreStore.adminUser || null"
138139
:meta="c.meta"
139140
/>
140141
</div>

dev-demo/api.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Express, Response } from "express";
22
import { IAdminForth, IAdminUserExpressRequest } from "adminforth";
33
import * as z from "zod";
4+
import TwoFactorsAuthPlugin from "../plugins/adminforth-two-factors-auth/index.js";
45

56
const DASHBOARD_CAR_SOURCES = [
67
{ resourceId: 'cars_sl', label: 'SQLite' },
@@ -172,4 +173,15 @@ export function initApi(app: Express, admin: IAdminForth) {
172173
)
173174
)
174175
);
176+
app.get(`${admin.config.baseUrl}/api/test2faCall/`,
177+
admin.express.authorize(
178+
async (_req: IAdminUserExpressRequest, res: Response) => {
179+
console.log('Received test2faCall');
180+
const { adminUser } = _req;
181+
const t2fa = admin.getPluginByClassName<TwoFactorsAuthPlugin>('TwoFactorsAuthPlugin');
182+
const verifyResult = await t2fa.verifyAuto(adminUser);
183+
res.json({ message: "2FA call received!" });
184+
}
185+
)
186+
);
175187
}

dev-demo/custom/AfComponents.vue

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,9 @@
389389
Refresh badge
390390
</Button>
391391

392+
<Button @click="doTest2faCall">
393+
Test 2FA API Call
394+
</Button>
392395
</div>
393396

394397

@@ -543,4 +546,12 @@ async function callHelloWorldApi() {
543546
console.error('API error:', error);
544547
}
545548
}
549+
550+
async function doTest2faCall() {
551+
try {
552+
const response = await callApi({ path: '/api/test2faCall/', method: 'GET' });
553+
} catch (error) {
554+
console.error('2FA API error:', error);
555+
}
556+
}
546557
</script>

0 commit comments

Comments
 (0)