-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyManager.js
59 lines (50 loc) · 1.38 KB
/
keyManager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import authManager from './authManager';
export default class KeyManager {
constructor(appId) {
this.appId = appId;
}
async generateNewKeypair() {
return import(
/*
webpackChunkName: "keyGenerator",
webpackPreload: true
*/
'./keyGenerator').then(({ default: keyGenerator }) => {
return keyGenerator.generate(this.appId).then((keypair) => {
return keypair;
});
});
}
async getKeys() {
return this.readKeysFromStorage() || await this.generateNewKeypair();
}
readKeysFromStorage() {
const publicKey = authManager.getAppProp(this.appId, 'publicKey');
const privateKey = authManager.getAppProp(this.appId, 'privateKey');
return (publicKey && privateKey) ?
{
public: publicKey,
private: privateKey
} : null;
}
async getPublicKey() {
return (await this.getKeys())['public'];
}
async getPrivateKey() {
return (await this.getKeys())['private'];
}
async decryptPayload(payload) {
return this.getPrivateKey().then(privateKey => {
return import(
/*
webpackChunkName: "jsencrypt",
webpackPreload: true
*/
'jsencrypt').then(({ default: Jsencrypt }) => {
const jsencrypt = new Jsencrypt();
jsencrypt.setPrivateKey(privateKey);
return JSON.parse(jsencrypt.decrypt(payload));
});
});
}
};