Implement Register Stage

This commit is contained in:
Bradley Schofield committed 2024-06-24 15:48:42 +09:00
1 parent 412bc33318
commit fa79662ab2
6 files changed
+170

No files matched your search

+14
View File
@@ -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",
+1
View File
@@ -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",
+68
View File
@@ -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<WebauthnRegisterChallenge> {
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);
}
}
+3
View File
@@ -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),
@@ -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}
</div>
</div>
<div
class="method u-flex u-flex-vertical-mobile u-gap-16 u-main-space-between u-sep-block-end"
style="padding-block-end: 16px">
<div class="u-flex u-gap-8">
<div class="avatar is-size-x-small">
<span class="icon-key" aria-hidden="true" />
</div>
<div class="u-flex-vertical u-gap-4 body-text-2">
<span class="u-bold">Security Key</span>
<span
>Use a security key to provide two-factor for your account.</span>
</div>
</div>
<div class="method-button">
{#if $factors.totp}
<Button
text
class="is-not-mobile"
on:click={() => (showWebauthnDelete = true)}>Delete</Button>
<Button
text
class="is-only-mobile"
noMargin
on:click={() => (showWebauthnDelete = true)}>Delete</Button>
{:else}
<Button secondary on:click={() => (showWebauthnSetup = true)}>Add</Button>
{/if}
</div>
</div>
{#if $factors.email}
<div
@@ -227,6 +259,9 @@
{#if showSetup}
<Mfa bind:showSetup />
{/if}
{#if showWebauthnSetup}
<WebauthnMfa bind:showWebauthnSetup />
{/if}
<DeleteMfa bind:showDelete />
<MfaRecoveryCodes bind:showRecoveryCodes {codes} />
@@ -0,0 +1,49 @@
<script lang="ts">
import { Submit, trackEvent } from '$lib/actions/analytics';
import { Button } from '$lib/elements/forms';
import Modal from '$lib/components/modal.svelte';
import { sdk } from '$lib/stores/sdk';
export let showWebauthnSetup = false;
let step = 1;
let error = '';
async function addAuthenticator() {
try {
await sdk.forConsole.webauthnAccount.createMfaWebauthnAuthenticatorHelper();
} catch (e) {
error = e.message;
}
trackEvent(Submit.AccountAuthenticatorCreate);
step = 2;
}
</script>
<Modal
title="Add security key"
bind:show={showWebauthnSetup}
{error}>
{#if step === 1}
<p>
Follow the instructions provided by your browser to register a new security key.
</p>
{#await addAuthenticator()}
<div class="loading">
<div class="loader"></div>
</div>
{/await}
{:else if step === 2}
<p>
Security key added successfully.
</p>
{/if}
<svelte:fragment slot="footer">
<Button text on:click={() => (showWebauthnSetup = false)}>Cancel</Button>
{#if step === 2}
<Button submit>Finish</Button>
{/if}
</svelte:fragment>
</Modal>