forked from polkadot-js/extension
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A0-3946: Fix password saving function not working properly
- Loading branch information
Roberts
committed
Feb 2, 2024
1 parent
77b1b52
commit 6f4415b
Showing
3 changed files
with
86 additions
and
19 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
61 changes: 61 additions & 0 deletions
61
packages/extension-base/src/background/handlers/chromeStorage.ts
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,61 @@ | ||
import { z } from 'zod'; | ||
|
||
import { PASSWORD_EXPIRY_MS } from '../../defaults'; | ||
|
||
const addressSchema = z.string(); | ||
const passwordSchema = z.record(z.string(), z.object({ expiry: z.number(), password: z.string() })); | ||
|
||
type Password = { expiry: number, password?: string } | ||
|
||
async function getPassword (address: string): Promise<Password> { | ||
if (addressSchema.safeParse(address).success) { | ||
const { savePass } = await chrome.storage.session.get('savePass'); | ||
|
||
const pass = passwordSchema.safeParse(savePass); | ||
|
||
if (pass.success) { | ||
return pass.data[address]; | ||
} else { | ||
return { expiry: 0 }; | ||
} | ||
} else { | ||
return { expiry: 0 }; | ||
} | ||
} | ||
|
||
async function setPassword (address: string, password: string): Promise<void> { | ||
if (addressSchema.safeParse(address).success) { | ||
const { savePass } = await chrome.storage.session.get('savePass'); | ||
|
||
const savedPasswords = passwordSchema.safeParse(savePass); | ||
|
||
if (savedPasswords.success) { | ||
await chrome.storage.session.set({ savePass: { ...savedPasswords.data, [address]: { password, expiry: Date.now() + PASSWORD_EXPIRY_MS } } }); | ||
} else { | ||
await chrome.storage.session.set({ savePass: { [address]: { expiry: Date.now() + PASSWORD_EXPIRY_MS, password } } }); | ||
} | ||
} else { | ||
console.error('Provided address did not pass validation'); | ||
} | ||
} | ||
|
||
async function removePassword (address: string): Promise<void> { | ||
if (addressSchema.safeParse(address).success) { | ||
const { savePass } = await chrome.storage.session.get('savePass'); | ||
|
||
const pass = passwordSchema.safeParse(savePass); | ||
|
||
if (pass.success) { | ||
delete pass.data[address]; | ||
await chrome.storage.session.set({ savePass: pass.data }); | ||
} | ||
} | ||
} | ||
|
||
const chromeStorage = { | ||
setPassword, | ||
getPassword, | ||
removePassword | ||
}; | ||
|
||
export default chromeStorage; |
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