diff --git a/package-lock.json b/package-lock.json index 944d0f2e8..73a025a09 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,7 @@ "@popperjs/core": "^2.11.8", "@sentry/svelte": "^7.66.0", "@sentry/tracing": "^7.66.0", + "@simplewebauthn/browser": "^10.0.0", "@stripe/stripe-js": "^3.4.0", "ai": "^2.2.11", "analytics": "^0.8.9", @@ -2309,6 +2310,19 @@ "node": ">=8" } }, + "node_modules/@simplewebauthn/browser": { + "version": "10.0.0", + "resolved": "https://registry.npmjs.org/@simplewebauthn/browser/-/browser-10.0.0.tgz", + "integrity": "sha512-hG0JMZD+LiLUbpQcAjS4d+t4gbprE/dLYop/CkE01ugU/9sKXflxV5s0DRjdz3uNMFecatRfb4ZLG3XvF8m5zg==", + "dependencies": { + "@simplewebauthn/types": "^10.0.0" + } + }, + "node_modules/@simplewebauthn/types": { + "version": "10.0.0", + "resolved": "https://registry.npmjs.org/@simplewebauthn/types/-/types-10.0.0.tgz", + "integrity": "sha512-SFXke7xkgPRowY2E+8djKbdEznTVnD5R6GO7GPTthpHrokLvNKw8C3lFZypTxLI7KkCfGPfhtqB3d7OVGGa9jQ==" + }, "node_modules/@sinclair/typebox": { "version": "0.27.8", "resolved": "https://registry.npmjs.org/@sinclair/typebox/-/typebox-0.27.8.tgz", diff --git a/package.json b/package.json index 4cd051ff0..3ce486181 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "@popperjs/core": "^2.11.8", "@sentry/svelte": "^7.66.0", "@sentry/tracing": "^7.66.0", + "@simplewebauthn/browser": "^10.0.0", "@stripe/stripe-js": "^3.4.0", "ai": "^2.2.11", "analytics": "^0.8.9", diff --git a/src/lib/sdk/account.ts b/src/lib/sdk/account.ts new file mode 100644 index 000000000..4ec1b22a9 --- /dev/null +++ b/src/lib/sdk/account.ts @@ -0,0 +1,68 @@ +import type { Client, Models } from '@appwrite.io/console'; +import { startRegistration } from '@simplewebauthn/browser'; + +export type WebauthnRegisterChallenge = { + $id: string; + userId: string; + rp: { + id: string; + name: string; + icon: string; + }, + user: { + id: string; + name: string; + displayName: string; + }, + challenge: string; + pubKeyCredParams: any; + timeout: number; +} + +export class Account { + client: Client; + + constructor(client: Client) { + this.client = client; + } + + async createMfaWebauthnAuthenticator(): Promise { + const path = '/account/mfa/authenticators/webauthn'; + + const uri = new URL(this.client.config.endpoint + path); + return await this.client.call( + 'POST', + uri, + { + 'content-type': 'application/json' + }, + ); + } + + async completeWebauthnMfaAuthenticator(credential: Credential) { + const path = '/account/mfa/authenticators/webauthn'; + + const uri = new URL(this.client.config.endpoint + path); + return await this.client.call( + 'PUT', + uri, + { + 'content-type': 'application/json' + }, + { + challengeResponse: JSON.stringify(credential) + } + ); + } + + async createMfaWebauthnAuthenticatorHelper() { + let challenge = await this.createMfaWebauthnAuthenticator(); + + //TODO Replace with native navigator.credentials.create + let credential = await startRegistration(challenge); + + let session = await this.completeWebauthnMfaAuthenticator(credential); + console.log(session); + } + +} \ No newline at end of file diff --git a/src/lib/stores/sdk.ts b/src/lib/stores/sdk.ts index 8dacdc079..d497dbbe2 100644 --- a/src/lib/stores/sdk.ts +++ b/src/lib/stores/sdk.ts @@ -23,6 +23,7 @@ import { } from '@appwrite.io/console'; import { Billing } from '../sdk/billing'; import { Sources } from '$lib/sdk/sources'; +import { Account as WebauthnAccount } from '$lib/sdk/account' const endpoint = VARS.APPWRITE_ENDPOINT ?? `${globalThis?.location?.origin}/v1`; @@ -35,6 +36,7 @@ clientProject.setEndpoint(endpoint).setMode('admin'); const sdkForProject = { client: clientProject, account: new Account(clientProject), + webauthnAccount: new WebauthnAccount(clientProject), avatars: new Avatars(clientProject), databases: new Databases(clientProject), functions: new Functions(clientProject), @@ -63,6 +65,7 @@ export const sdk = { forConsole: { client: clientConsole, account: new Account(clientConsole), + webauthnAccount: new WebauthnAccount(clientConsole), avatars: new Avatars(clientConsole), functions: new Functions(clientConsole), health: new Health(clientConsole), diff --git a/src/routes/console/account/updateMfa.svelte b/src/routes/console/account/updateMfa.svelte index 86e8eccca..e95c29b67 100644 --- a/src/routes/console/account/updateMfa.svelte +++ b/src/routes/console/account/updateMfa.svelte @@ -14,9 +14,12 @@ import type { Models } from '@appwrite.io/console'; import MfaRegenerateCodes from './mfaRegenerateCodes.svelte'; import { Pill } from '$lib/elements'; + import WebauthnMfa from './webauthnMfa.svelte'; let showSetup: boolean = false; let showDelete: boolean = false; + let showWebauthnSetup: boolean = false; + let showWebauthnDelete: boolean = false; let showRecoveryCodes = false; let showRegenerateRecoveryCodes = false; let codes: Models.MfaRecoveryCodes = null; @@ -136,6 +139,35 @@ {/if} +
+
+
+
+
+ Security Key + Use a security key to provide two-factor for your account. +
+
+
+ {#if $factors.totp} + + + {:else} + + {/if} +
+
{#if $factors.email}
{/if} +{#if showWebauthnSetup} + +{/if} diff --git a/src/routes/console/account/webauthnMfa.svelte b/src/routes/console/account/webauthnMfa.svelte new file mode 100644 index 000000000..be49f74f4 --- /dev/null +++ b/src/routes/console/account/webauthnMfa.svelte @@ -0,0 +1,49 @@ + + + + {#if step === 1} +

+ Follow the instructions provided by your browser to register a new security key. +

+ {#await addAuthenticator()} +
+
+
+ {/await} + {:else if step === 2} +

+ Security key added successfully. +

+ {/if} + + + + {#if step === 2} + + {/if} + +
\ No newline at end of file