Skip to content

Commit c860780

Browse files
committed
feat: add new mfa for account api
1 parent f9f505d commit c860780

1 file changed

Lines changed: 154 additions & 2 deletions

File tree

docs/end-user-flows/account-settings/by-account-api.mdx

Lines changed: 154 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ Some frequent use cases are listed below:
5151
To learn more about the available APIs, please visit [Logto Account API Reference](https://openapi.logto.io/group/endpoint-my-account) and [Logto Verification API Reference](https://openapi.logto.io/group/endpoint-verifications).
5252

5353
:::note
54-
Dedicated Account APIs for the following settings are coming soon: MFA, SSO, Custom data (user), and Account deletion. In the meantime, you can implement these features using the Logto Management APIs. See [Account settings by Management API](/end-user-flows/account-settings/by-management-api) for more details.
54+
Dedicated Account APIs for the following settings are coming soon: SSO, Custom data (user), and Account deletion. In the meantime, you can implement these features using the Logto Management APIs. See [Account settings by Management API](/end-user-flows/account-settings/by-management-api) for more details.
55+
56+
MFA management APIs (TOTP and backup codes) are currently under development and only available when the `isDevFeaturesEnabled` flag is set to `true`. WebAuthn passkey management is fully available.
5557
:::
5658

5759
## How to enable Account API \{#how-to-enable-account-api}
@@ -256,7 +258,7 @@ curl -X POST https://[tenant-id].logto.app/api/verifications/verification-code/v
256258
After verifying the code, you can now update the user's email, set the `verificationId` to the request body as `newIdentifierVerificationRecordId`.
257259

258260
```bash
259-
curl -X PATCH https://[tenant-id].logto.app/api/my-account/primary-email \
261+
curl -X POST https://[tenant-id].logto.app/api/my-account/primary-email \
260262
-H 'authorization: Bearer <access_token>' \
261263
-H 'logto-verification-id: <verification_record_id>' \
262264
-H 'content-type: application/json' \
@@ -460,3 +462,153 @@ curl -X DELETE https://[tenant-id].logto.app/api/my-account/mfa-verifications/{v
460462
-H 'authorization: Bearer <access_token>' \
461463
-H 'logto-verification-id: <verification_record_id>'
462464
```
465+
466+
### Link a new TOTP \{#link-a-new-totp}
467+
468+
:::note
469+
Remember to [enable MFA and TOTP](/end-user-flows/mfa) first.
470+
:::
471+
472+
:::note
473+
To use this method, you need to enable the `mfa` field in the account center settings.
474+
:::
475+
476+
**Step 1: Generate a TOTP secret.**
477+
478+
```bash
479+
curl -X POST https://[tenant-id].logto.app/api/my-account/mfa-verifications/totp-secret/generate \
480+
-H 'authorization: Bearer <access_token>' \
481+
-H 'content-type: application/json'
482+
```
483+
484+
The response body would be like:
485+
486+
```json
487+
{
488+
"secret": "..."
489+
}
490+
```
491+
492+
**Step 2: Display the TOTP secret to the user.**
493+
494+
Use the secret to generate a QR code or display it directly to the user. The user should add it to their authenticator app (such as Google Authenticator, Microsoft Authenticator, or Authy).
495+
496+
The URI format for the QR code should be:
497+
498+
```
499+
otpauth://totp/[Issuer]:[Account]?secret=[Secret]&issuer=[Issuer]
500+
```
501+
502+
Example:
503+
504+
```
505+
otpauth://totp/YourApp:user@example.com?secret=JBSWY3DPEHPK3PXP&issuer=YourApp
506+
```
507+
508+
**Step 3: Bind the TOTP factor.**
509+
510+
After the user has added the secret to their authenticator app, they need to verify it and bind it to their account:
511+
512+
```bash
513+
curl -X POST https://[tenant-id].logto.app/api/my-account/mfa-verifications \
514+
-H 'authorization: Bearer <access_token>' \
515+
-H 'logto-verification-id: <verification_record_id>' \
516+
-H 'content-type: application/json' \
517+
--data-raw '{"type":"Totp","secret":"..."}'
518+
```
519+
520+
- `verification_record_id`: a valid verification record ID, granted by verifying the user's existing factor. You can refer to the [Get a verification record ID](#get-a-verification-record-id) section for more details.
521+
- `type`: must be `Totp`.
522+
- `secret`: the TOTP secret generated in step 1.
523+
524+
:::note
525+
A user can only have one TOTP factor at a time. If the user already has a TOTP factor, attempting to add another one will result in a 422 error.
526+
:::
527+
528+
### Manage backup codes \{#manage-backup-codes}
529+
530+
:::note
531+
Remember to [enable MFA and backup codes](/end-user-flows/mfa) first.
532+
:::
533+
534+
:::note
535+
To use this method, you need to enable the `mfa` field in the account center settings.
536+
:::
537+
538+
**Step 1: Generate new backup codes:**
539+
540+
```bash
541+
curl -X POST https://[tenant-id].logto.app/api/my-account/mfa-verifications/backup-codes/generate \
542+
-H 'authorization: Bearer <access_token>' \
543+
-H 'content-type: application/json'
544+
```
545+
546+
The response body would be like:
547+
548+
```json
549+
{
550+
"codes": ["...", "...", "..."]
551+
}
552+
```
553+
554+
**Step 2: Display backup codes to the user:**
555+
556+
:::important
557+
Before binding the backup codes to the user's account, you must display them to the user and instruct them to:
558+
559+
- Download or write down these codes immediately
560+
- Store them in a secure location
561+
- Understand that each code can only be used once
562+
- Know that these codes are their last resort if they lose access to their primary MFA methods
563+
564+
You should display the codes in a clear, easy-to-copy format and consider providing a download option (e.g., as a text file or PDF).
565+
:::
566+
567+
**Step 3: Bind backup codes to the user account:**
568+
569+
```bash
570+
curl -X POST https://[tenant-id].logto.app/api/my-account/mfa-verifications \
571+
-H 'authorization: Bearer <access_token>' \
572+
-H 'logto-verification-id: <verification_record_id>' \
573+
-H 'content-type: application/json' \
574+
--data-raw '{"type":"BackupCode","codes":["...","...","..."]}'
575+
```
576+
577+
- `verification_record_id`: a valid verification record ID, granted by verifying the user's existing factor. You can refer to the [Get a verification record ID](#get-a-verification-record-id) section for more details.
578+
- `type`: must be `BackupCode`.
579+
- `codes`: the array of backup codes generated in the previous step.
580+
581+
:::note
582+
583+
- A user can only have one set of backup codes at a time. If all codes have been used, the user needs to generate and bind new codes.
584+
- Backup codes cannot be the only MFA factor. The user must have at least one other MFA factor (such as WebAuthn or TOTP) enabled.
585+
- Each backup code can only be used once.
586+
587+
:::
588+
589+
**View existing backup codes:**
590+
591+
```bash
592+
curl https://[tenant-id].logto.app/api/my-account/mfa-verifications/backup-codes \
593+
-H 'authorization: Bearer <access_token>'
594+
```
595+
596+
The response body would be like:
597+
598+
```json
599+
{
600+
"codes": [
601+
{
602+
"code": "...",
603+
"usedAt": null
604+
},
605+
{
606+
"code": "...",
607+
"usedAt": "2024-01-15T10:30:00.000Z"
608+
}
609+
]
610+
}
611+
```
612+
613+
- `code`: the backup code.
614+
- `usedAt`: the timestamp when the code was used, `null` if not used yet.

0 commit comments

Comments
 (0)