-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
234 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import { | ||
DEVICE_KEY, | ||
LOCAL, | ||
PASSWORD, | ||
SESSION, | ||
SESSION_DATA, | ||
SESSION_DATA_KEY, | ||
SETUP_PASSWORD | ||
} from '$const'; | ||
import { decryptData, encryptData, hashPassword } from '$helpers'; | ||
import { storageService } from '$services'; | ||
import { Password, SecureDataSchema } from '$types'; | ||
import { createMutation, createQuery, type QueryClient } from '@tanstack/svelte-query'; | ||
|
||
const getPassword = async () => { | ||
const data = await storageService.getWithoutCallback({ key: PASSWORD, area: LOCAL }); | ||
return Password.safeParse(data); | ||
}; | ||
|
||
export function createSetupPasswordQuery() { | ||
return createQuery({ | ||
queryKey: [SETUP_PASSWORD], | ||
queryFn: async () => { | ||
const validatedResult = await getPassword(); | ||
return validatedResult.success; | ||
} | ||
}); | ||
} | ||
|
||
export function createPasswordMutation(queryClient: QueryClient) { | ||
return createMutation({ | ||
mutationFn: async (password: string) => { | ||
const hash = await hashPassword(password); | ||
storageService.set({ | ||
key: PASSWORD, | ||
value: hash, | ||
area: LOCAL | ||
}); | ||
}, | ||
onSuccess: () => { | ||
queryClient.invalidateQueries({ queryKey: [SETUP_PASSWORD] }); | ||
} | ||
}); | ||
} | ||
|
||
export function createSignInMutation(queryClient: QueryClient) { | ||
return createMutation({ | ||
mutationFn: async (password: string) => { | ||
const validatedResult = await getPassword(); | ||
|
||
if (validatedResult.success && (await hashPassword(password)) === validatedResult.data) { | ||
return storageService.set({ | ||
key: SESSION_DATA, | ||
value: true, | ||
area: SESSION | ||
}); | ||
} | ||
|
||
throw new Error('Invalid password or data'); | ||
}, | ||
onSuccess: () => { | ||
queryClient.invalidateQueries({ queryKey: [SESSION_DATA_KEY] }); | ||
} | ||
}); | ||
} | ||
|
||
export function createChangePasswordWithDeviceKeyMutation(queryClient: QueryClient) { | ||
return createMutation({ | ||
mutationFn: async (mutationData: { newPassword: string; oldPassword: string }) => { | ||
const oldHash = await hashPassword(mutationData.oldPassword); | ||
|
||
const validatePassword = await getPassword(); | ||
|
||
if (!validatePassword.success || oldHash !== validatePassword.data) { | ||
throw new Error('Invalid password'); | ||
} | ||
|
||
const deviceKey = await storageService.getWithoutCallback({ | ||
key: DEVICE_KEY, | ||
area: LOCAL | ||
}); | ||
|
||
const validatedDeviceKey = SecureDataSchema.safeParse(deviceKey); | ||
|
||
if (!validatedDeviceKey.success) { | ||
throw new Error('Invalid device key'); | ||
} | ||
|
||
const decryptedData = await decryptData(validatedDeviceKey.data, oldHash); | ||
const newHash = await hashPassword(mutationData.newPassword); | ||
|
||
storageService.set({ | ||
key: PASSWORD, | ||
value: newHash, | ||
area: LOCAL | ||
}); | ||
storageService.set({ | ||
key: DEVICE_KEY, | ||
value: await encryptData(decryptedData, newHash), | ||
area: LOCAL | ||
}); | ||
storageService.set({ | ||
key: SESSION_DATA, | ||
value: false, | ||
area: SESSION | ||
}); | ||
}, | ||
|
||
onSuccess: () => { | ||
queryClient.invalidateQueries({ queryKey: [SESSION_DATA_KEY] }); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { | ||
DEVICE_KEY, | ||
LOCAL, | ||
PASSWORD, | ||
SESSION, | ||
SESSION_DATA, | ||
SESSION_DATA_KEY, | ||
SETUP_KEY | ||
} from '$const'; | ||
import { encryptData } from '$helpers'; | ||
import { storageService } from '$services'; | ||
import { Password, SecureDataSchema, SessionStateSchema } from '$types'; | ||
import { QueryClient, createMutation, createQuery } from '@tanstack/svelte-query'; | ||
|
||
export function createSessionQuery() { | ||
return createQuery({ | ||
queryKey: [SESSION_DATA_KEY], | ||
queryFn: async () => { | ||
const data = await storageService.getWithoutCallback({ | ||
key: SESSION_DATA, | ||
area: SESSION | ||
}); | ||
const validatedData = SessionStateSchema.safeParse(data); | ||
return validatedData.success ? validatedData.data : false; | ||
} | ||
}); | ||
} | ||
|
||
export function createSetupDeviceKeyQuery() { | ||
return createQuery({ | ||
queryKey: [SETUP_KEY], | ||
queryFn: async () => { | ||
const data = await storageService.getWithoutCallback({ | ||
key: DEVICE_KEY, | ||
area: LOCAL | ||
}); | ||
const validatedData = SecureDataSchema.safeParse(data); | ||
return validatedData.success; | ||
} | ||
}); | ||
} | ||
|
||
export function createStoreDeviceKey(queryClient: QueryClient) { | ||
return createMutation({ | ||
mutationFn: async (deviceKey: Uint8Array) => { | ||
const result = await storageService.getWithoutCallback({ | ||
key: PASSWORD, | ||
area: LOCAL | ||
}); | ||
const validatePassword = Password.safeParse(result); | ||
|
||
if (validatePassword.success) { | ||
storageService.set({ | ||
key: DEVICE_KEY, | ||
value: await encryptData(deviceKey, validatePassword.data), | ||
area: LOCAL | ||
}); | ||
} | ||
|
||
throw new Error('Something went wrong'); | ||
}, | ||
|
||
onSuccess: () => { | ||
queryClient.invalidateQueries({ queryKey: [SETUP_KEY] }); | ||
} | ||
}); | ||
} |
Oops, something went wrong.