forked from timvisee/send
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.js
299 lines (272 loc) · 7.75 KB
/
user.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import assets from '../common/assets';
import { getFileList, setFileList } from './api';
import { encryptStream, decryptStream } from './ece';
import { arrayToB64, b64ToArray, streamToArrayBuffer } from './utils';
import { blobStream } from './streams';
import { getFileListKey, prepareScopedBundleKey, preparePkce } from './fxa';
import storage from './storage';
const textEncoder = new TextEncoder();
const textDecoder = new TextDecoder();
const anonId = arrayToB64(crypto.getRandomValues(new Uint8Array(16)));
async function hashId(id) {
const d = new Date();
const month = d.getUTCMonth();
const year = d.getUTCFullYear();
const encoded = textEncoder.encode(`${id}:${year}:${month}`);
const hash = await crypto.subtle.digest('SHA-256', encoded);
return arrayToB64(new Uint8Array(hash.slice(16)));
}
export default class User {
constructor(storage, limits, authConfig) {
this.authConfig = authConfig;
this.limits = limits;
this.storage = storage;
this.data = storage.user || {};
}
get info() {
return this.data || this.storage.user || {};
}
set info(data) {
this.data = data;
this.storage.user = data;
}
get firstAction() {
return this.storage.get('firstAction');
}
set firstAction(action) {
this.storage.set('firstAction', action);
}
get surveyed() {
return this.storage.get('surveyed');
}
set surveyed(yes) {
this.storage.set('surveyed', yes);
}
get avatar() {
const defaultAvatar = assets.get('user.svg');
if (this.info.avatarDefault) {
return defaultAvatar;
}
return this.info.avatar || defaultAvatar;
}
get name() {
return this.info.displayName;
}
get email() {
return this.info.email;
}
get loggedIn() {
return !!this.info.access_token;
}
get bearerToken() {
return this.info.access_token;
}
get refreshToken() {
return this.info.refresh_token;
}
get maxSize() {
return this.loggedIn
? this.limits.MAX_FILE_SIZE
: this.limits.ANON.MAX_FILE_SIZE;
}
get maxExpireSeconds() {
return this.loggedIn
? this.limits.MAX_EXPIRE_SECONDS
: this.limits.ANON.MAX_EXPIRE_SECONDS;
}
get maxDownloads() {
return this.loggedIn
? this.limits.MAX_DOWNLOADS
: this.limits.ANON.MAX_DOWNLOADS;
}
async metricId() {
return this.loggedIn ? hashId(this.info.uid) : undefined;
}
async deviceId() {
return this.loggedIn ? hashId(this.storage.id) : hashId(anonId);
}
async startAuthFlow(trigger, utms = {}) {
this.utms = utms;
this.trigger = trigger;
this.flowId = null;
this.flowBeginTime = null;
}
async login(email) {
const state = arrayToB64(crypto.getRandomValues(new Uint8Array(16)));
storage.set('oauthState', state);
const keys_jwk = await prepareScopedBundleKey(this.storage);
const code_challenge = await preparePkce(this.storage);
const options = {
action: 'email',
access_type: 'offline',
client_id: this.authConfig.client_id,
code_challenge,
code_challenge_method: 'S256',
response_type: 'code',
scope: `profile ${this.authConfig.key_scope}`,
state,
keys_jwk
};
if (email) {
options.email = email;
}
if (this.flowId && this.flowBeginTime) {
options.flow_id = this.flowId;
options.flow_begin_time = this.flowBeginTime;
}
if (this.trigger) {
options.entrypoint = `send-${this.trigger}`;
}
if (this.utms) {
options.utm_campaign = this.utms.campaign || 'none';
options.utm_content = this.utms.content || 'none';
options.utm_medium = this.utms.medium || 'none';
options.utm_source = this.utms.source || 'send';
options.utm_term = this.utms.term || 'none';
}
const params = new URLSearchParams(options);
location.assign(
`${this.authConfig.authorization_endpoint}?${params.toString()}`
);
}
async finishLogin(code, state) {
const localState = storage.get('oauthState');
storage.remove('oauthState');
if (state !== localState) {
throw new Error('state mismatch');
}
const tokenResponse = await fetch(this.authConfig.token_endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
code,
client_id: this.authConfig.client_id,
code_verifier: this.storage.get('pkceVerifier')
})
});
const auth = await tokenResponse.json();
const infoResponse = await fetch(this.authConfig.userinfo_endpoint, {
method: 'GET',
headers: {
Authorization: `Bearer ${auth.access_token}`
}
});
const userInfo = await infoResponse.json();
userInfo.access_token = auth.access_token;
userInfo.refresh_token = auth.refresh_token;
userInfo.fileListKey = await getFileListKey(this.storage, auth.keys_jwe);
this.info = userInfo;
this.storage.remove('pkceVerifier');
}
async refresh() {
if (!this.refreshToken) {
return false;
}
try {
const tokenResponse = await fetch(this.authConfig.token_endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
client_id: this.authConfig.client_id,
grant_type: 'refresh_token',
refresh_token: this.refreshToken
})
});
if (tokenResponse.ok) {
const auth = await tokenResponse.json();
const info = { ...this.info, access_token: auth.access_token };
this.info = info;
return true;
}
} catch (e) {
console.error(e);
}
await this.logout();
return false;
}
async logout() {
try {
if (this.refreshToken) {
await fetch(this.authConfig.revocation_endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
refresh_token: this.refreshToken
})
});
}
if (this.bearerToken) {
await fetch(this.authConfig.revocation_endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
token: this.bearerToken
})
});
}
} catch (e) {
console.error(e);
// oh well, we tried
}
this.storage.clearLocalFiles();
this.info = {};
}
async syncFileList() {
let changes = { incoming: false, outgoing: false, downloadCount: false };
if (!this.loggedIn) {
return this.storage.merge();
}
let list = [];
const key = b64ToArray(this.info.fileListKey);
const sha = await crypto.subtle.digest('SHA-256', key);
const kid = arrayToB64(new Uint8Array(sha)).substring(0, 16);
const retry = async () => {
const refreshed = await this.refresh();
if (refreshed) {
return await this.syncFileList();
} else {
return { incoming: true };
}
};
try {
const encrypted = await getFileList(this.bearerToken, kid);
const decrypted = await streamToArrayBuffer(
decryptStream(blobStream(encrypted), key)
);
list = JSON.parse(textDecoder.decode(decrypted));
} catch (e) {
if (e.message === '401') {
return retry(e);
}
}
changes = await this.storage.merge(list);
if (!changes.outgoing) {
return changes;
}
try {
const blob = new Blob([
textEncoder.encode(JSON.stringify(this.storage.files))
]);
const encrypted = await streamToArrayBuffer(
encryptStream(blobStream(blob), key)
);
await setFileList(this.bearerToken, kid, encrypted);
} catch (e) {
if (e.message === '401') {
return retry(e);
}
}
return changes;
}
toJSON() {
return this.info;
}
}