-
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
25 changed files
with
283 additions
and
135 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,29 @@ | ||
<script> | ||
import { Button, AppParagraph } from '$components'; | ||
import { dismissWindow } from '$lib/helpers'; | ||
function redirectToSetup() { | ||
window.open('setup/start.html', '_blank'); | ||
} | ||
</script> | ||
|
||
<div class="m-8"> | ||
<div class="flex justify-between items-center mb-4"> | ||
<img src="/img/holo_logo.svg" alt="Holo Key Manager Logo" /> | ||
<button on:click={dismissWindow} class="bg-transparent border-none"> | ||
<img src="/img/close.svg" alt="Close" /> | ||
</button> | ||
</div> | ||
|
||
<div class="flex flex-col justify-center items-center"> | ||
<img src="/img/setup.svg" alt="Setup" class="max-w-[48px]" /> | ||
<h1 class="font-bold text-2xl mt-4">Setup Required</h1> | ||
<AppParagraph | ||
extraProps="my-4 text-center max-w-xs" | ||
text="You are yet to setup Holo Key Manager. Click “start setup” to begin." | ||
/> | ||
</div> | ||
|
||
<Button label="Start setup" onClick={redirectToSetup} extraBottomMargin /> | ||
<Button label="Cancel" onClick={dismissWindow} color="secondary" /> | ||
</div> |
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,39 @@ | ||
<script> | ||
import { Button, AppParagraph } from '$components'; | ||
import { passwordExistStore, sessionStore } from '$stores'; | ||
import { dismissWindow } from '$lib/helpers'; | ||
let password = ''; | ||
async function login() { | ||
const isValid = await passwordExistStore.validate(password); | ||
if (isValid) { | ||
sessionStore.set({ session: true }); | ||
dismissWindow(); | ||
} else { | ||
alert('Invalid password'); | ||
} | ||
} | ||
</script> | ||
|
||
<div class="m-8"> | ||
<div class="flex justify-between items-center mb-4"> | ||
<img src="/img/holo_logo.svg" alt="Holo Key Manager Logo" /> | ||
<button on:click={dismissWindow} class="bg-transparent border-none"> | ||
<img src="/img/close.svg" alt="Close" /> | ||
</button> | ||
</div> | ||
|
||
<div class="flex flex-col justify-center items-center"> | ||
<img src="/img/logo.svg" alt="Login" class="max-w-[48px]" /> | ||
<h1 class="font-bold text-2xl mt-4">Login Required</h1> | ||
<AppParagraph | ||
extraProps="my-4 text-center max-w-xs" | ||
text="Please enter your password to login." | ||
/> | ||
<input bind:value={password} type="password" class="my-4" placeholder="Enter password" /> | ||
</div> | ||
|
||
<Button label="Login" onClick={login} extraBottomMargin /> | ||
<Button label="Cancel" onClick={dismissWindow} color="secondary" /> | ||
</div> |
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,8 @@ | ||
import { passwordExistStore, sessionStore } from '$stores'; | ||
import { derived, type Readable } from 'svelte/store'; | ||
|
||
export function createIsAuthenticated(): Readable<boolean> { | ||
return derived([sessionStore, passwordExistStore], ([$sessionStore, $passwordExistStore]) => | ||
Boolean($sessionStore.session && $passwordExistStore) | ||
); | ||
} |
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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
export * from './navigation'; | ||
export * from './other'; | ||
export * from './password'; | ||
export * from './auth'; |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export function dismissExtensionWindow() { | ||
export function dismissWindow() { | ||
return window.close(); | ||
} |
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,8 @@ | ||
export async function hashPassword(password: string): Promise<string> { | ||
const encoder = new TextEncoder(); | ||
const data = encoder.encode(password); | ||
const hashBuffer = await crypto.subtle.digest('SHA-256', data); | ||
const hashArray = Array.from(new Uint8Array(hashBuffer)); | ||
const hashHex = hashArray.map((byte) => byte.toString(16).padStart(2, '0')).join(''); | ||
return hashHex; | ||
} |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './generate-keys'; | ||
export * from './storage'; |
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,48 @@ | ||
import { isChromeStorageSafe } from '$helpers'; | ||
import type { AreaName, ChangesType, StorageKey } from '$types'; | ||
|
||
type StorageService = { | ||
set(key: StorageKey, value: unknown, area: 'local' | 'session'): void; | ||
get(key: StorageKey, callback: (value: unknown) => void, area: 'local' | 'session'): void; | ||
getWithoutCallback: (key: StorageKey, area: 'local' | 'session') => Promise<unknown>; | ||
addListener(listener: (changes: ChangesType, namespace: AreaName) => void): void; | ||
removeListener(listener: (changes: ChangesType, namespace: AreaName) => void): void; | ||
}; | ||
|
||
export const storageService: StorageService = { | ||
set: (key: StorageKey, value: unknown, area: 'local' | 'session') => { | ||
if (isChromeStorageSafe()) { | ||
chrome.storage[area].set({ [key]: value }); | ||
} | ||
}, | ||
get: (key: StorageKey, callback: (value: unknown) => void, area: 'local' | 'session') => { | ||
if (isChromeStorageSafe()) { | ||
chrome.storage[area].get([key], (result: ChangesType) => { | ||
callback(result[key]); | ||
}); | ||
} else { | ||
callback(null); | ||
} | ||
}, | ||
getWithoutCallback: (key: StorageKey, area: 'local' | 'session') => { | ||
if (isChromeStorageSafe()) { | ||
return new Promise((resolve) => { | ||
chrome.storage[area].get([key], (result: ChangesType) => { | ||
resolve(result[key]); | ||
}); | ||
}); | ||
} else { | ||
return Promise.resolve(null); | ||
} | ||
}, | ||
addListener: (listener: (changes: ChangesType, namespace: AreaName) => void) => { | ||
if (isChromeStorageSafe()) { | ||
chrome.storage.onChanged.addListener(listener); | ||
} | ||
}, | ||
removeListener: (listener: (changes: ChangesType, namespace: AreaName) => void) => { | ||
if (isChromeStorageSafe()) { | ||
chrome.storage.onChanged.removeListener(listener); | ||
} | ||
} | ||
}; |
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,41 @@ | ||
import { z } from 'zod'; | ||
import { writable } from 'svelte/store'; | ||
import { storageService } from '$services'; | ||
import type { AreaName, ChangesType } from '$types'; | ||
import { hashPassword } from '$helpers'; | ||
|
||
const createPasswordExistStore = () => { | ||
const { subscribe, set, update } = writable<boolean | null>(null, () => { | ||
const listener = (changes: ChangesType, namespace: AreaName) => { | ||
if (namespace === 'local' && 'password' in changes) { | ||
update(() => true); | ||
} | ||
}; | ||
|
||
storageService.get('password', (result) => set(!!result), 'local'); | ||
|
||
storageService.addListener(listener); | ||
|
||
return () => { | ||
storageService.removeListener(listener); | ||
}; | ||
}); | ||
|
||
return { | ||
subscribe, | ||
set: (value: boolean) => { | ||
set(value); | ||
storageService.set('password', value, 'local'); | ||
}, | ||
validate: async (password: string) => { | ||
const result = await storageService.getWithoutCallback('password', 'local'); | ||
const validatedResult = z.string().safeParse(result); | ||
|
||
return validatedResult.success && (await hashPassword(password)) === validatedResult.data; | ||
} | ||
}; | ||
}; | ||
|
||
const passwordExistStore = createPasswordExistStore(); | ||
|
||
export { passwordExistStore }; |
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 |
---|---|---|
@@ -1,39 +1,43 @@ | ||
import { isChromeStorageSafe } from '$helpers'; | ||
import { STORAGE_KEY, type ChangesType, type SessionState } from '$types'; | ||
import { writable } from 'svelte/store'; | ||
import { type SessionState, SessionStateSchema, type AreaName, type ChangesType } from '$types'; | ||
import { storageService } from '$services'; | ||
|
||
const createSessionStore = () => { | ||
const { subscribe, set, update } = writable<SessionState>({ session: false }, () => { | ||
const listener = (changes: ChangesType, namespace: string) => { | ||
if (namespace === 'session' && changes[STORAGE_KEY]?.newValue) { | ||
console.log('Session store updated from chrome.storage.session'); | ||
update(() => changes[STORAGE_KEY]?.newValue ?? { session: false }); | ||
const { subscribe, set, update } = writable<SessionState>({ session: null }, () => { | ||
const listener = (changes: ChangesType, namespace: AreaName) => { | ||
if (namespace === 'session') { | ||
const newValue = SessionStateSchema.safeParse(changes['sessionData']); | ||
update(() => (newValue.success ? newValue.data : { session: false })); | ||
} | ||
}; | ||
|
||
// When the store is first created, try to load existing state from chrome.storage.session | ||
isChromeStorageSafe() && | ||
chrome.storage.session.get([STORAGE_KEY], (result) => { | ||
result[STORAGE_KEY] && set(result[STORAGE_KEY]); | ||
}); | ||
storageService.get( | ||
'sessionData', | ||
(result) => { | ||
const validatedResult = SessionStateSchema.safeParse(result); | ||
if (validatedResult.success) { | ||
set(validatedResult.data); | ||
} else { | ||
set({ session: false }); | ||
} | ||
}, | ||
'session' | ||
); | ||
|
||
isChromeStorageSafe() && chrome.storage.onChanged.addListener(listener); | ||
storageService.addListener(listener); | ||
|
||
// Return a function that can be called to unsubscribe from the store | ||
return () => { | ||
isChromeStorageSafe() && chrome.storage.onChanged.removeListener(listener); | ||
storageService.removeListener(listener); | ||
}; | ||
}); | ||
|
||
return { | ||
subscribe, | ||
set: (value: SessionState) => { | ||
set(value); | ||
isChromeStorageSafe() && chrome.storage.session.set({ [STORAGE_KEY]: value }); | ||
storageService.set('sessionData', value, 'session'); | ||
} | ||
}; | ||
}; | ||
|
||
const sessionStore = createSessionStore(); | ||
|
||
export { sessionStore }; | ||
export const sessionStore = createSessionStore(); |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './password'; | ||
export * from './keys'; | ||
export * from './storage-service'; |
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
Oops, something went wrong.