-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
memoize
me
, my userKeys
and my browserKeys
- Loading branch information
1 parent
dc27428
commit ec5488e
Showing
10 changed files
with
128 additions
and
102 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { base64 } from 'rfc4648'; | ||
import backend, { DeviceDto, UserDto } from './backend'; | ||
import { BrowserKeys, UserKeys } from './crypto'; | ||
|
||
class UserData { | ||
|
||
#me?: Promise<UserDto>; | ||
#browserKeys?: Promise<BrowserKeys | undefined>; | ||
|
||
public get me(): Promise<UserDto> { | ||
if (!this.#me) { | ||
this.#me = backend.users.me(true); | ||
} | ||
return this.#me; | ||
} | ||
|
||
public get browserKeys(): Promise<BrowserKeys | undefined> { | ||
return this.me.then(me => { | ||
if (!this.#browserKeys) { | ||
this.#browserKeys = BrowserKeys.load(me.id); | ||
} | ||
return this.#browserKeys; | ||
}); | ||
} | ||
|
||
public get browser(): Promise<DeviceDto | undefined> { | ||
return this.me.then(async me => { | ||
const browserKeys = await this.browserKeys; | ||
if (browserKeys == null) { | ||
return undefined; | ||
} else { | ||
const browserId = await browserKeys.id(); | ||
return me.devices.find(d => d.id == browserId); | ||
} | ||
}); | ||
} | ||
|
||
public async reload() { | ||
this.#me = backend.users.me(true); | ||
this.#browserKeys = undefined; | ||
} | ||
|
||
public async createBrowserKeys(): Promise<BrowserKeys> { | ||
const me = await this.me; | ||
const browserKeys = await BrowserKeys.create(); | ||
await browserKeys.store(me.id); | ||
this.#browserKeys = Promise.resolve(browserKeys); | ||
return browserKeys; | ||
} | ||
|
||
public async decryptUserKeysWithSetupCode(setupCode: string): Promise<UserKeys> { | ||
const me = await this.me; | ||
if (!me.privateKey || !me.ecdhPublicKey) { | ||
throw new Error('User not initialized.'); | ||
} | ||
const ecdhPublicKey = base64.parse(me.ecdhPublicKey); | ||
const ecdsaPublicKey = me.ecdsaPublicKey ? base64.parse(me.ecdsaPublicKey) : undefined; // TODO keep ecdsa key optional? | ||
return await UserKeys.recover(me.privateKey, setupCode, ecdhPublicKey, ecdsaPublicKey); | ||
} | ||
|
||
public async decryptUserKeysWithBrowser(): Promise<UserKeys> { | ||
const me = await this.me; | ||
if (!me.privateKey || !me.ecdhPublicKey || !me.ecdsaPublicKey) { | ||
throw new Error('User not initialized.'); | ||
} | ||
const browserKeys = await this.browserKeys; | ||
if (!browserKeys) { | ||
throw new Error('Browser keys not found.'); | ||
} | ||
const browser = await this.browser; | ||
if (!browser) { | ||
throw new Error('Device not initialized.'); | ||
} | ||
return await UserKeys.decryptOnBrowser(browser.userPrivateKey, browserKeys.keyPair.privateKey, base64.parse(me.ecdhPublicKey), base64.parse(me.ecdsaPublicKey)); | ||
} | ||
|
||
} | ||
|
||
const instance = new UserData(); | ||
export default instance; |
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
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
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
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.