Skip to content

Commit

Permalink
Fix password change flow
Browse files Browse the repository at this point in the history
  • Loading branch information
mrruby committed Dec 8, 2023
1 parent 210880f commit c8cbc77
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 45 deletions.
1 change: 1 addition & 0 deletions src/lib/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './navigation';
export * from './other';
export * from './encryption';
export * from './storage';
8 changes: 8 additions & 0 deletions src/lib/helpers/storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { LOCAL, PASSWORD } from '$const';
import { storageService } from '$services';
import { HashSaltSchema } from '$types';

export const getPassword = async () => {
const data = await storageService.getWithoutCallback({ key: PASSWORD, area: LOCAL });
return HashSaltSchema.safeParse(data);
};
13 changes: 2 additions & 11 deletions src/lib/queries/password.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,11 @@ import {
SESSION_DATA_KEY,
SETUP_PASSWORD
} from '$const';
import { hashPassword, verifyPassword } from '$helpers';
import { getPassword, hashPassword, verifyPassword } from '$helpers';
import { lockKey, storageService, unlockKey } from '$services';
import { EncryptedDeviceKeySchema, HashSaltSchema } from '$types';
import { EncryptedDeviceKeySchema } from '$types';
import { createMutation, createQuery, type QueryClient } from '@tanstack/svelte-query';

const getPassword = async () => {
const data = await storageService.getWithoutCallback({ key: PASSWORD, area: LOCAL });
return HashSaltSchema.safeParse(data);
};

export function createSetupPasswordQuery() {
return createQuery({
queryKey: [SETUP_PASSWORD],
Expand Down Expand Up @@ -87,11 +82,7 @@ export function createChangePasswordWithDeviceKeyMutation(queryClient: QueryClie
throw new Error('Invalid device key');
}

console.log(parsedDeviceKey.data);
console.log(parsedResult.data.hash);

const decryptedKey = await unlockKey(parsedDeviceKey.data, parsedResult.data.hash);
console.log('test2');
const newHashSalt = await hashPassword(mutationData.newPassword);

storageService.set({
Expand Down
2 changes: 0 additions & 2 deletions src/lib/queries/sessionAndKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ export function createStoreDeviceKey(queryClient: QueryClient) {
value: deviceKey,
area: LOCAL
});

console.log(deviceKey);
},

onSuccess: () => {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/services/generate-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const uint8ArrayToBase64 = (bytes: Uint8Array) => btoa(String.fromCharCode(...by

const base64ToArrayBuffer = (base64: string) => {
const binaryString = atob(base64);
return new Uint8Array([...binaryString].map((char) => char.charCodeAt(0))).buffer;
return new Uint8Array([...binaryString].map((char) => char.charCodeAt(0)));
};

const lock = (root: never, password: string) =>
Expand Down
1 change: 0 additions & 1 deletion src/lib/stores/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './passphrase';
export * from './password';
export * from './keys-store';
9 changes: 7 additions & 2 deletions src/lib/stores/keys-store.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { KeysState } from '$types';
import { writable } from 'svelte/store';
import { generateKeys } from '../services/generate-keys';
import { getPassword } from '$helpers';

const initKeysStore = () => {
const initialState: KeysState = {
Expand All @@ -19,10 +20,14 @@ const initKeysStore = () => {
isInitialState: () =>
subscribe((state) => JSON.stringify(state) === JSON.stringify(initialState)),
subscribe,
generate: async (passphrase: string, extensionPassword: string) => {
generate: async (passphrase: string) => {
update((state) => ({ ...state, loading: true }));
try {
const keys = await generateKeys(passphrase, extensionPassword);
const hashSalt = await getPassword();
if (!hashSalt.success) {
throw new Error('Password missing');
}
const keys = await generateKeys(passphrase, hashSalt.data.hash);
set({ keys, loading: false });
} catch (error) {
set(initialState);
Expand Down
17 changes: 0 additions & 17 deletions src/lib/stores/password.ts

This file was deleted.

8 changes: 4 additions & 4 deletions src/routes/setup-keys/generate-keys/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts">
import { onMount } from 'svelte';
import { keysStore, passphraseStore, passwordStore } from '$stores';
import { keysStore, passphraseStore } from '$stores';
import { goto } from '$app/navigation';
import { Button, Title, AppParagraph } from '$components';
import { sessionStorageQueries } from '$queries';
Expand All @@ -14,11 +14,11 @@
});
async function generate() {
await keysStore.generate($passphraseStore, $passwordStore);
console.log($keysStore.keys);
await keysStore.generate($passphraseStore);
if ($keysStore.keys) {
passphraseStore.clean();
passwordStore.clean();
if ($keysStore.keys.encodedDeviceWithExtensionPassword) {
$storeDeviceKey.mutate($keysStore.keys.encodedDeviceWithExtensionPassword, {
onSuccess: () => {
Expand Down
12 changes: 6 additions & 6 deletions src/routes/setup-pass/app-password/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<script lang="ts">
import { goto } from '$app/navigation';
import { AppParagraph, Button, Title } from '$components';
import { passwordStore } from '$stores';
import { sessionStorageQueries } from '$queries';
import { dismissWindow } from '$helpers';
import InputPassword from '$components/InputPassword.svelte';
const { createPassword } = sessionStorageQueries();
let password = '';
let confirmPassword = '';
$: charCount = $passwordStore.length;
$: isDisabled = charCount < 8 || confirmPassword !== $passwordStore;
$: charCount = password.length;
$: isDisabled = charCount < 8 || confirmPassword !== password;
</script>

<Title>Set Key Manager Password</Title>
Expand All @@ -21,7 +21,7 @@
/>
<div class="w-full p-6">
<InputPassword
bind:value={$passwordStore}
bind:value={password}
label="New Password (8 Characters min)"
extraProps="mb-6"
error={charCount < 8 ? 'Please enter a minimum of 8 Characters' : ''}
Expand All @@ -30,7 +30,7 @@
bind:value={confirmPassword}
label="Confirm New Password"
extraProps="mb-4"
error={confirmPassword !== $passwordStore ? "Password doesn't Match" : ''}
error={confirmPassword !== password ? "Password doesn't Match" : ''}
/>
</div>

Expand All @@ -40,7 +40,7 @@
disabled={isDisabled}
label="Set password"
onClick={() =>
$createPassword.mutate($passwordStore, {
$createPassword.mutate(password, {
onSuccess: () => {
goto('enter-passphrase');
}
Expand Down
2 changes: 1 addition & 1 deletion static/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "Holo key manager",
"description": "A browser extension to manage holo keys",
"version": "0.0.25",
"version": "0.0.26",
"manifest_version": 3,
"browser_specific_settings": {
"gecko": {
Expand Down

0 comments on commit c8cbc77

Please sign in to comment.