From 4a0ff58dc6f52fd015f4b659275d43b5b0bfa8a0 Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Thu, 4 Jul 2024 11:22:44 +0900 Subject: [PATCH] Complete MFA Flow --- .../components/mfaChallengeFormList.svelte | 78 +++++++++- src/lib/sdk/account.ts | 43 +++++- .../console/account/deleteWebauthn.svelte | 58 +++++++ src/routes/console/account/updateMfa.svelte | 11 +- src/routes/console/account/webauthnMfa.svelte | 141 +++++++++++++++--- 5 files changed, 294 insertions(+), 37 deletions(-) create mode 100644 src/routes/console/account/deleteWebauthn.svelte diff --git a/src/lib/components/mfaChallengeFormList.svelte b/src/lib/components/mfaChallengeFormList.svelte index 96e77daf2..02f06171a 100644 --- a/src/lib/components/mfaChallengeFormList.svelte +++ b/src/lib/components/mfaChallengeFormList.svelte @@ -6,7 +6,12 @@ AuthenticationFactor.Totp ); } - await sdk.forConsole.account.updateMfaChallenge(challenge.$id, code); + + if (code = 'webauthn') { + await sdk.forConsole.webauthnAccount.createMfaWebauthnChallengeHelper(); + } else { + await sdk.forConsole.account.updateMfaChallenge(challenge.$id, code); + } await invalidate(Dependencies.ACCOUNT); trackEvent(Submit.AccountCreate); } catch (error) { @@ -53,6 +58,9 @@ createChallenge(AuthenticationFactor.Phone); } else if (factors.email) { createChallenge(AuthenticationFactor.Email); + } else if (factors.webauthn) { + code = 'webauthn'; + showVerifyButton = false; } } }); @@ -65,14 +73,68 @@

{:else} - {#if factors.totp && (challengeType == AuthenticationFactor.Totp || challengeType == null)} -

Enter below a 6-digit one-time code generated by your authentication app.

- {:else if challengeType == AuthenticationFactor.Email} -

A 6-digit verification code was sent to your email, enter it below.

- {:else if challengeType == AuthenticationFactor.Phone} -

A 6-digit verification code was sent to your phone, enter it below.

+ {#if !factors.webauthn} + {#if factors.totp && (challengeType == AuthenticationFactor.Totp || challengeType == null)} +

Enter below a 6-digit one-time code generated by your authentication app.

+ {:else if challengeType == AuthenticationFactor.Email} +

A 6-digit verification code was sent to your email, enter it below.

+ {:else if challengeType == AuthenticationFactor.Phone} +

A 6-digit verification code was sent to your phone, enter it below.

+ {/if} + + {:else} +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+

Insert your security key into your device and press authenticate.

+
+ {/if} - {/if} {#if showVerifyButton} diff --git a/src/lib/sdk/account.ts b/src/lib/sdk/account.ts index 4ec1b22a9..8da8a1493 100644 --- a/src/lib/sdk/account.ts +++ b/src/lib/sdk/account.ts @@ -1,5 +1,5 @@ import type { Client, Models } from '@appwrite.io/console'; -import { startRegistration } from '@simplewebauthn/browser'; +import { startRegistration, startAuthentication } from '@simplewebauthn/browser'; export type WebauthnRegisterChallenge = { $id: string; @@ -61,8 +61,45 @@ export class Account { //TODO Replace with native navigator.credentials.create let credential = await startRegistration(challenge); - let session = await this.completeWebauthnMfaAuthenticator(credential); - console.log(session); + await this.completeWebauthnMfaAuthenticator(credential); } + async createMfaWebauthnChallenge() { + const path = '/account/mfa/challenge/webauthn'; + + const uri = new URL(this.client.config.endpoint + path); + return await this.client.call( + 'POST', + uri, + { + 'content-type': 'application/json' + }, + ); + } + + async completeMfaWebauthnChallenge(challengeId: string, credential: Credential) { + const path = '/account/mfa/challenge/webauthn'; + + const uri = new URL(this.client.config.endpoint + path); + return await this.client.call( + 'PUT', + uri, + { + 'content-type': 'application/json' + }, + { + challengeId: challengeId, + challengeResponse: JSON.stringify(credential) + } + ); + } + + async createMfaWebauthnChallengeHelper() { + let challenge = await this.createMfaWebauthnChallenge(); + + //TODO Replace with native navigator.credentials.create + let credential = await startAuthentication(challenge); + + await this.completeMfaWebauthnChallenge(challenge.$id, credential); + } } \ No newline at end of file diff --git a/src/routes/console/account/deleteWebauthn.svelte b/src/routes/console/account/deleteWebauthn.svelte new file mode 100644 index 000000000..05a96219d --- /dev/null +++ b/src/routes/console/account/deleteWebauthn.svelte @@ -0,0 +1,58 @@ + + + +

Please authenticate with your Webauthn Device to continue.

+ + + + + + + + +
diff --git a/src/routes/console/account/updateMfa.svelte b/src/routes/console/account/updateMfa.svelte index e95c29b67..2353b987c 100644 --- a/src/routes/console/account/updateMfa.svelte +++ b/src/routes/console/account/updateMfa.svelte @@ -15,6 +15,7 @@ import MfaRegenerateCodes from './mfaRegenerateCodes.svelte'; import { Pill } from '$lib/elements'; import WebauthnMfa from './webauthnMfa.svelte'; + import DeleteWebauthn from './deleteWebauthn.svelte'; let showSetup: boolean = false; let showDelete: boolean = false; @@ -147,13 +148,18 @@