|
| 1 | +<script lang="ts"> |
| 2 | + import { invalidate } from '$app/navigation'; |
| 3 | + import { Submit, trackError, trackEvent } from '$lib/actions/analytics'; |
| 4 | + import { CardGrid } from '$lib/components'; |
| 5 | + import { Dependencies } from '$lib/constants'; |
| 6 | + import { Button, Form, InputNumber, InputSwitch } from '$lib/elements/forms'; |
| 7 | + import { addNotification } from '$lib/stores/notifications'; |
| 8 | + import { sdk } from '$lib/stores/sdk'; |
| 9 | + import { Typography, Link, Layout } from '@appwrite.io/pink-svelte'; |
| 10 | + import type { Models } from '@appwrite.io/console'; |
| 11 | + import { onMount } from 'svelte'; |
| 12 | +
|
| 13 | + let { |
| 14 | + project |
| 15 | + }: { |
| 16 | + project: Models.Project; |
| 17 | + } = $props(); |
| 18 | +
|
| 19 | + let lastValidLimit = $state(5); |
| 20 | + let passwordHistory = $state(5); |
| 21 | + let passwordDictionary = $state(false); |
| 22 | + let passwordHistoryEnabled = $state(false); |
| 23 | + let authPersonalDataCheck = $state(false); |
| 24 | +
|
| 25 | + onMount(() => { |
| 26 | + // update initial states here in onMount. |
| 27 | + const historyValue = project.authPasswordHistory; |
| 28 | + if (historyValue && historyValue > 0) { |
| 29 | + passwordHistory = historyValue; |
| 30 | + lastValidLimit = historyValue; |
| 31 | + } |
| 32 | +
|
| 33 | + passwordHistoryEnabled = (historyValue ?? 0) !== 0; |
| 34 | + passwordDictionary = project.authPasswordDictionary ?? false; |
| 35 | + authPersonalDataCheck = project.authPersonalDataCheck ?? false; |
| 36 | + }); |
| 37 | +
|
| 38 | + $effect(() => { |
| 39 | + // restore last valid limit when enabling |
| 40 | + if (passwordHistoryEnabled && passwordHistory < 1) { |
| 41 | + passwordHistory = lastValidLimit; |
| 42 | + } |
| 43 | + }); |
| 44 | +
|
| 45 | + const hasChanges = $derived.by(() => { |
| 46 | + const dictChanged = passwordDictionary !== (project.authPasswordDictionary ?? false); |
| 47 | + const dataCheckChanged = authPersonalDataCheck !== (project.authPersonalDataCheck ?? false); |
| 48 | + const historyChanged = |
| 49 | + passwordHistoryEnabled !== ((project.authPasswordHistory ?? 0) !== 0); |
| 50 | + const limitChanged = |
| 51 | + passwordHistoryEnabled && |
| 52 | + Number(passwordHistory) !== (project.authPasswordHistory ?? 0); |
| 53 | +
|
| 54 | + return historyChanged || dictChanged || dataCheckChanged || limitChanged; |
| 55 | + }); |
| 56 | +
|
| 57 | + async function updatePasswordPolicies() { |
| 58 | + try { |
| 59 | + const projectSdk = sdk.forConsole.projects; |
| 60 | +
|
| 61 | + await projectSdk.updateAuthPasswordHistory({ |
| 62 | + projectId: project.$id, |
| 63 | + limit: passwordHistoryEnabled ? passwordHistory : 0 |
| 64 | + }); |
| 65 | +
|
| 66 | + await projectSdk.updateAuthPasswordDictionary({ |
| 67 | + projectId: project.$id, |
| 68 | + enabled: passwordDictionary |
| 69 | + }); |
| 70 | +
|
| 71 | + await projectSdk.updatePersonalDataCheck({ |
| 72 | + projectId: project.$id, |
| 73 | + enabled: authPersonalDataCheck |
| 74 | + }); |
| 75 | +
|
| 76 | + await invalidate(Dependencies.PROJECT); |
| 77 | + addNotification({ |
| 78 | + type: 'success', |
| 79 | + message: 'Updated password policies.' |
| 80 | + }); |
| 81 | + trackEvent(Submit.AuthPasswordHistoryUpdate); |
| 82 | + trackEvent(Submit.AuthPasswordDictionaryUpdate); |
| 83 | + trackEvent(Submit.AuthPersonalDataCheckUpdate); |
| 84 | + } catch (error) { |
| 85 | + addNotification({ |
| 86 | + type: 'error', |
| 87 | + message: error.message |
| 88 | + }); |
| 89 | + trackError(error, Submit.AuthPasswordHistoryUpdate); |
| 90 | + } |
| 91 | + } |
| 92 | +</script> |
| 93 | + |
| 94 | +<Form onSubmit={updatePasswordPolicies}> |
| 95 | + <CardGrid gap="xxl"> |
| 96 | + <svelte:fragment slot="title">Password policies</svelte:fragment> |
| 97 | + <svelte:fragment slot="aside"> |
| 98 | + <InputSwitch |
| 99 | + bind:value={passwordHistoryEnabled} |
| 100 | + id="passwordHistoryEnabled" |
| 101 | + label="Password history"> |
| 102 | + <svelte:fragment slot="description"> |
| 103 | + <Layout.Stack gap="m"> |
| 104 | + <Typography.Text> |
| 105 | + Enabling this option prevents users from reusing recent passwords by |
| 106 | + comparing the new password with their password history. |
| 107 | + </Typography.Text> |
| 108 | + {#if passwordHistoryEnabled} |
| 109 | + <InputNumber |
| 110 | + required |
| 111 | + max={20} |
| 112 | + min={1} |
| 113 | + autofocus |
| 114 | + label="Limit" |
| 115 | + id="password-history" |
| 116 | + bind:value={passwordHistory} |
| 117 | + helper="Maximum 20 passwords." /> |
| 118 | + {/if} |
| 119 | + </Layout.Stack> |
| 120 | + </svelte:fragment> |
| 121 | + </InputSwitch> |
| 122 | + |
| 123 | + <InputSwitch |
| 124 | + bind:value={passwordDictionary} |
| 125 | + id="passwordDictionary" |
| 126 | + label="Password dictionary"> |
| 127 | + <svelte:fragment slot="description"> |
| 128 | + <Typography.Text> |
| 129 | + Enabling this option prevents users from setting insecure passwords by |
| 130 | + comparing the user's password with the <Link.Anchor |
| 131 | + target="_blank" |
| 132 | + rel="noopener noreferrer" |
| 133 | + class="link" |
| 134 | + href="https://github.com/danielmiessler/SecLists/blob/master/Passwords/Common-Credentials/10k-most-common.txt" |
| 135 | + >10k most commonly used passwords.</Link.Anchor> |
| 136 | + </Typography.Text> |
| 137 | + </svelte:fragment> |
| 138 | + </InputSwitch> |
| 139 | + |
| 140 | + <InputSwitch |
| 141 | + bind:value={authPersonalDataCheck} |
| 142 | + id="personalDataCheck" |
| 143 | + label="Disallow personal data"> |
| 144 | + <svelte:fragment slot="description"> |
| 145 | + <Typography.Text> |
| 146 | + Do not allow passwords that contain any part of the user's personal data. |
| 147 | + This includes the user's <Typography.Code>name</Typography.Code>, <Typography.Code |
| 148 | + >email</Typography.Code |
| 149 | + >, or <Typography.Code>phone</Typography.Code>. |
| 150 | + </Typography.Text> |
| 151 | + </svelte:fragment> |
| 152 | + </InputSwitch> |
| 153 | + </svelte:fragment> |
| 154 | + |
| 155 | + <svelte:fragment slot="actions"> |
| 156 | + <Button disabled={!hasChanges} submit>Update</Button> |
| 157 | + </svelte:fragment> |
| 158 | + </CardGrid> |
| 159 | +</Form> |
0 commit comments