/
login.js
175 lines (149 loc) · 4.94 KB
/
login.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// @flow
import type { Account as OlmAccount } from '@commapp/olm';
import { getRustAPI } from 'rust-node-addon';
import type { OLMOneTimeKeys } from 'lib/types/crypto-types';
import { getCommConfig } from 'lib/utils/comm-config.js';
import { ServerError } from 'lib/utils/errors.js';
import { values } from 'lib/utils/objects.js';
import {
saveIdentityInfo,
fetchIdentityInfo,
type IdentityInfo,
} from './identity.js';
import { getMessageForException } from '../responders/utils.js';
import { fetchCallUpdateOlmAccount } from '../updaters/olm-account-updater.js';
import { validateAccountPrekey } from '../utils/olm-utils.js';
type UserCredentials = { +username: string, +password: string };
export type AccountKeysSet = {
+identityKeys: string,
+prekey: string,
+prekeySignature: string,
+oneTimeKey: $ReadOnlyArray<string>,
};
function getOneTimeKeyValues(keyBlob: string): $ReadOnlyArray<string> {
const content: OLMOneTimeKeys = JSON.parse(keyBlob);
const keys: $ReadOnlyArray<string> = values(content.curve25519);
return keys;
}
function retrieveAccountKeysSet(account: OlmAccount): AccountKeysSet {
const identityKeys = account.identity_keys();
validateAccountPrekey(account);
const prekeyMap = JSON.parse(account.prekey()).curve25519;
const [prekey] = values(prekeyMap);
const prekeySignature = account.prekey_signature();
if (!prekeySignature || !prekey) {
throw new ServerError('invalid_prekey');
}
if (getOneTimeKeyValues(account.one_time_keys()).length < 10) {
account.generate_one_time_keys(10);
}
const oneTimeKey = getOneTimeKeyValues(account.one_time_keys());
return { identityKeys, oneTimeKey, prekey, prekeySignature };
}
// After register or login is successful
function markKeysAsPublished(account: OlmAccount) {
account.mark_prekey_as_published();
account.mark_keys_as_published();
}
async function verifyUserLoggedIn(): Promise<IdentityInfo> {
const result = await fetchIdentityInfo();
if (result) {
return result;
}
const identityInfo = await registerOrLogin();
await saveIdentityInfo(identityInfo);
return identityInfo;
}
async function registerOrLogin(): Promise<IdentityInfo> {
const rustAPIPromise = getRustAPI();
const userInfo = await getCommConfig<UserCredentials>({
folder: 'secrets',
name: 'user_credentials',
});
if (!userInfo) {
throw new ServerError('missing_user_credentials');
}
const {
identityKeys: notificationsIdentityKeys,
prekey: notificationsPrekey,
prekeySignature: notificationsPrekeySignature,
oneTimeKey: notificationsOneTimeKey,
} = await fetchCallUpdateOlmAccount('notifications', retrieveAccountKeysSet);
const contentAccountCallback = async (account: OlmAccount) => {
const {
identityKeys: contentIdentityKeys,
oneTimeKey,
prekey,
prekeySignature,
} = await retrieveAccountKeysSet(account);
const identityKeysBlob = {
primaryIdentityPublicKeys: JSON.parse(contentIdentityKeys),
notificationIdentityPublicKeys: JSON.parse(notificationsIdentityKeys),
};
const identityKeysBlobPayload = JSON.stringify(identityKeysBlob);
const signedIdentityKeysBlob = {
payload: identityKeysBlobPayload,
signature: account.sign(identityKeysBlobPayload),
};
return {
signedIdentityKeysBlob,
oneTimeKey,
prekey,
prekeySignature,
};
};
const [
rustAPI,
{
signedIdentityKeysBlob,
prekey: contentPrekey,
prekeySignature: contentPrekeySignature,
oneTimeKey: contentOneTimeKey,
},
] = await Promise.all([
rustAPIPromise,
fetchCallUpdateOlmAccount('content', contentAccountCallback),
]);
try {
const identity_info = await rustAPI.loginUser(
userInfo.username,
userInfo.password,
signedIdentityKeysBlob,
contentPrekey,
contentPrekeySignature,
notificationsPrekey,
notificationsPrekeySignature,
contentOneTimeKey,
notificationsOneTimeKey,
);
await Promise.all([
fetchCallUpdateOlmAccount('content', markKeysAsPublished),
fetchCallUpdateOlmAccount('notifications', markKeysAsPublished),
]);
return identity_info;
} catch (e) {
console.warn('Failed to login user: ' + getMessageForException(e));
try {
const identity_info = await rustAPI.registerUser(
userInfo.username,
userInfo.password,
signedIdentityKeysBlob,
contentPrekey,
contentPrekeySignature,
notificationsPrekey,
notificationsPrekeySignature,
contentOneTimeKey,
notificationsOneTimeKey,
);
await Promise.all([
fetchCallUpdateOlmAccount('content', markKeysAsPublished),
fetchCallUpdateOlmAccount('notifications', markKeysAsPublished),
]);
return identity_info;
} catch (err) {
console.warn('Failed to register user: ' + getMessageForException(err));
throw new ServerError('identity_auth_failed');
}
}
}
export { verifyUserLoggedIn };