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 311133b
Showing
2 changed files
with
75 additions
and
13 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
59 changes: 59 additions & 0 deletions
59
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,59 @@ | ||
import { z } from 'zod'; | ||
|
||
import { PASSWORD_EXPIRY_MS } from '../../defaults'; | ||
|
||
const addressSchema = z.string(); | ||
const passwordSchema = z.record(z.string(), z.number()); | ||
|
||
async function getPasswordExpiry (address: string): Promise<number> { | ||
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 0; | ||
} | ||
} else { | ||
return 0; | ||
} | ||
} | ||
|
||
async function setPassword (address: 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]: Date.now() + PASSWORD_EXPIRY_MS } }); | ||
} else { | ||
await chrome.storage.session.set({ savePass: { [address]: Date.now() + PASSWORD_EXPIRY_MS } }); | ||
} | ||
} 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, | ||
getPasswordExpiry, | ||
removePassword | ||
}; | ||
|
||
export default chromeStorage; |