2018-08-08 00:40:17 +02:00
|
|
|
import assets from '../common/assets';
|
|
|
|
import { getFileList, setFileList } from './api';
|
|
|
|
import { encryptStream, decryptStream } from './ece';
|
2018-09-21 23:16:56 +02:00
|
|
|
import { arrayToB64, b64ToArray, streamToArrayBuffer } from './utils';
|
2018-08-08 00:40:17 +02:00
|
|
|
import { blobStream } from './streams';
|
2018-09-14 17:00:33 +02:00
|
|
|
import { getFileListKey, prepareScopedBundleKey, preparePkce } from './fxa';
|
2018-09-21 23:16:56 +02:00
|
|
|
import storage from './storage';
|
2018-08-08 00:40:17 +02:00
|
|
|
|
|
|
|
const textEncoder = new TextEncoder();
|
|
|
|
const textDecoder = new TextDecoder();
|
2019-02-12 20:50:06 +01:00
|
|
|
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)));
|
|
|
|
}
|
2018-08-08 00:40:17 +02:00
|
|
|
|
|
|
|
export default class User {
|
2019-02-26 19:39:50 +01:00
|
|
|
constructor(storage, limits, authConfig) {
|
|
|
|
this.authConfig = authConfig;
|
|
|
|
this.limits = limits;
|
2018-08-08 00:40:17 +02:00
|
|
|
this.storage = storage;
|
2018-09-14 17:00:33 +02:00
|
|
|
this.data = storage.user || {};
|
2018-08-08 00:40:17 +02:00
|
|
|
}
|
|
|
|
|
2018-11-08 22:35:19 +01:00
|
|
|
get info() {
|
|
|
|
return this.data || this.storage.user || {};
|
|
|
|
}
|
|
|
|
|
|
|
|
set info(data) {
|
|
|
|
this.data = data;
|
|
|
|
this.storage.user = data;
|
|
|
|
}
|
|
|
|
|
2019-02-12 20:50:06 +01:00
|
|
|
get firstAction() {
|
|
|
|
return this.storage.get('firstAction');
|
|
|
|
}
|
|
|
|
|
|
|
|
set firstAction(action) {
|
|
|
|
this.storage.set('firstAction', action);
|
|
|
|
}
|
|
|
|
|
2019-04-26 22:30:33 +02:00
|
|
|
get surveyed() {
|
|
|
|
return this.storage.get('surveyed');
|
|
|
|
}
|
|
|
|
|
|
|
|
set surveyed(yes) {
|
|
|
|
this.storage.set('surveyed', yes);
|
|
|
|
}
|
|
|
|
|
2018-08-08 00:40:17 +02:00
|
|
|
get avatar() {
|
|
|
|
const defaultAvatar = assets.get('user.svg');
|
2018-11-08 22:35:19 +01:00
|
|
|
if (this.info.avatarDefault) {
|
2019-02-08 20:58:42 +01:00
|
|
|
return defaultAvatar;
|
2018-08-08 00:40:17 +02:00
|
|
|
}
|
2018-11-08 22:35:19 +01:00
|
|
|
return this.info.avatar || defaultAvatar;
|
2018-08-08 00:40:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get name() {
|
2018-11-08 22:35:19 +01:00
|
|
|
return this.info.displayName;
|
2018-08-08 00:40:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get email() {
|
2018-11-08 22:35:19 +01:00
|
|
|
return this.info.email;
|
2018-08-08 00:40:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get loggedIn() {
|
2018-11-08 22:35:19 +01:00
|
|
|
return !!this.info.access_token;
|
2018-08-08 00:40:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get bearerToken() {
|
2018-11-08 22:35:19 +01:00
|
|
|
return this.info.access_token;
|
2018-08-08 00:40:17 +02:00
|
|
|
}
|
|
|
|
|
2020-07-25 03:11:50 +02:00
|
|
|
get refreshToken() {
|
|
|
|
return this.info.refresh_token;
|
|
|
|
}
|
|
|
|
|
2018-08-08 00:40:17 +02:00
|
|
|
get maxSize() {
|
2021-04-12 15:19:02 +02:00
|
|
|
return this.limits.MAX_FILE_SIZE;
|
2018-08-08 00:40:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get maxExpireSeconds() {
|
2021-04-12 15:19:02 +02:00
|
|
|
return this.limits.MAX_EXPIRE_SECONDS;
|
2018-08-08 00:40:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get maxDownloads() {
|
2021-04-12 15:19:02 +02:00
|
|
|
return this.limits.MAX_DOWNLOADS;
|
2018-08-08 00:40:17 +02:00
|
|
|
}
|
|
|
|
|
2019-02-12 20:50:06 +01:00
|
|
|
async metricId() {
|
|
|
|
return this.loggedIn ? hashId(this.info.uid) : undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
async deviceId() {
|
|
|
|
return this.loggedIn ? hashId(this.storage.id) : hashId(anonId);
|
|
|
|
}
|
|
|
|
|
2019-03-18 23:01:01 +01:00
|
|
|
async startAuthFlow(trigger, utms = {}) {
|
|
|
|
this.utms = utms;
|
|
|
|
this.trigger = trigger;
|
2021-03-15 19:56:51 +01:00
|
|
|
this.flowId = null;
|
|
|
|
this.flowBeginTime = null;
|
2019-03-05 21:58:40 +01:00
|
|
|
}
|
|
|
|
|
2018-09-24 21:01:39 +02:00
|
|
|
async login(email) {
|
2018-09-21 23:16:56 +02:00
|
|
|
const state = arrayToB64(crypto.getRandomValues(new Uint8Array(16)));
|
|
|
|
storage.set('oauthState', state);
|
2018-09-14 17:00:33 +02:00
|
|
|
const keys_jwk = await prepareScopedBundleKey(this.storage);
|
|
|
|
const code_challenge = await preparePkce(this.storage);
|
2018-09-24 21:01:39 +02:00
|
|
|
const options = {
|
2019-03-07 21:08:42 +01:00
|
|
|
action: 'email',
|
2020-07-25 03:11:50 +02:00
|
|
|
access_type: 'offline',
|
2019-02-26 19:39:50 +01:00
|
|
|
client_id: this.authConfig.client_id,
|
2018-09-14 17:00:33 +02:00
|
|
|
code_challenge,
|
|
|
|
code_challenge_method: 'S256',
|
|
|
|
response_type: 'code',
|
2019-02-26 19:39:50 +01:00
|
|
|
scope: `profile ${this.authConfig.key_scope}`,
|
2018-09-21 23:16:56 +02:00
|
|
|
state,
|
2018-09-14 17:00:33 +02:00
|
|
|
keys_jwk
|
2018-09-24 21:01:39 +02:00
|
|
|
};
|
|
|
|
if (email) {
|
|
|
|
options.email = email;
|
|
|
|
}
|
2019-03-05 21:58:40 +01:00
|
|
|
if (this.flowId && this.flowBeginTime) {
|
|
|
|
options.flow_id = this.flowId;
|
|
|
|
options.flow_begin_time = this.flowBeginTime;
|
|
|
|
}
|
2019-03-18 23:01:01 +01:00
|
|
|
if (this.trigger) {
|
2019-03-21 17:30:13 +01:00
|
|
|
options.entrypoint = `send-${this.trigger}`;
|
2019-03-18 23:01:01 +01:00
|
|
|
}
|
2019-03-05 21:58:40 +01:00
|
|
|
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';
|
|
|
|
}
|
2018-09-24 21:01:39 +02:00
|
|
|
const params = new URLSearchParams(options);
|
2018-09-14 17:00:33 +02:00
|
|
|
location.assign(
|
2019-02-26 19:39:50 +01:00
|
|
|
`${this.authConfig.authorization_endpoint}?${params.toString()}`
|
2018-09-14 17:00:33 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-09-21 23:16:56 +02:00
|
|
|
async finishLogin(code, state) {
|
|
|
|
const localState = storage.get('oauthState');
|
|
|
|
storage.remove('oauthState');
|
|
|
|
if (state !== localState) {
|
|
|
|
throw new Error('state mismatch');
|
|
|
|
}
|
2019-02-26 19:39:50 +01:00
|
|
|
const tokenResponse = await fetch(this.authConfig.token_endpoint, {
|
2018-09-14 17:00:33 +02:00
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
code,
|
2019-02-26 19:39:50 +01:00
|
|
|
client_id: this.authConfig.client_id,
|
2018-09-14 17:00:33 +02:00
|
|
|
code_verifier: this.storage.get('pkceVerifier')
|
|
|
|
})
|
|
|
|
});
|
|
|
|
const auth = await tokenResponse.json();
|
2019-02-26 19:39:50 +01:00
|
|
|
const infoResponse = await fetch(this.authConfig.userinfo_endpoint, {
|
2018-09-14 17:00:33 +02:00
|
|
|
method: 'GET',
|
|
|
|
headers: {
|
|
|
|
Authorization: `Bearer ${auth.access_token}`
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const userInfo = await infoResponse.json();
|
|
|
|
userInfo.access_token = auth.access_token;
|
2020-07-25 03:11:50 +02:00
|
|
|
userInfo.refresh_token = auth.refresh_token;
|
2018-09-14 17:00:33 +02:00
|
|
|
userInfo.fileListKey = await getFileListKey(this.storage, auth.keys_jwe);
|
2018-11-08 22:35:19 +01:00
|
|
|
this.info = userInfo;
|
2018-09-14 17:00:33 +02:00
|
|
|
this.storage.remove('pkceVerifier');
|
|
|
|
}
|
2018-08-08 00:40:17 +02:00
|
|
|
|
2020-07-25 03:11:50 +02:00
|
|
|
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
|
|
|
|
})
|
|
|
|
});
|
2020-07-25 07:04:42 +02:00
|
|
|
if (tokenResponse.ok) {
|
|
|
|
const auth = await tokenResponse.json();
|
|
|
|
const info = { ...this.info, access_token: auth.access_token };
|
|
|
|
this.info = info;
|
|
|
|
return true;
|
|
|
|
}
|
2020-07-25 03:11:50 +02:00
|
|
|
} catch (e) {
|
2020-07-25 07:04:42 +02:00
|
|
|
console.error(e);
|
2020-07-25 03:11:50 +02:00
|
|
|
}
|
2020-07-25 07:04:42 +02:00
|
|
|
await this.logout();
|
|
|
|
return false;
|
2020-07-25 03:11:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2018-08-08 00:40:17 +02:00
|
|
|
this.storage.clearLocalFiles();
|
2018-11-08 22:35:19 +01:00
|
|
|
this.info = {};
|
2018-08-08 00:40:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async syncFileList() {
|
|
|
|
let changes = { incoming: false, outgoing: false, downloadCount: false };
|
|
|
|
if (!this.loggedIn) {
|
|
|
|
return this.storage.merge();
|
|
|
|
}
|
|
|
|
let list = [];
|
2019-02-26 22:53:11 +01:00
|
|
|
const key = b64ToArray(this.info.fileListKey);
|
|
|
|
const sha = await crypto.subtle.digest('SHA-256', key);
|
|
|
|
const kid = arrayToB64(new Uint8Array(sha)).substring(0, 16);
|
2020-07-25 07:04:42 +02:00
|
|
|
const retry = async () => {
|
|
|
|
const refreshed = await this.refresh();
|
|
|
|
if (refreshed) {
|
|
|
|
return await this.syncFileList();
|
|
|
|
} else {
|
|
|
|
return { incoming: true };
|
2020-07-25 03:11:50 +02:00
|
|
|
}
|
2020-07-25 07:04:42 +02:00
|
|
|
};
|
2018-08-08 00:40:17 +02:00
|
|
|
try {
|
2020-07-25 07:04:42 +02:00
|
|
|
const encrypted = await getFileList(this.bearerToken, kid);
|
2018-08-08 00:40:17 +02:00
|
|
|
const decrypted = await streamToArrayBuffer(
|
2019-02-26 22:53:11 +01:00
|
|
|
decryptStream(blobStream(encrypted), key)
|
2018-08-08 00:40:17 +02:00
|
|
|
);
|
|
|
|
list = JSON.parse(textDecoder.decode(decrypted));
|
|
|
|
} catch (e) {
|
2020-07-25 07:04:42 +02:00
|
|
|
if (e.message === '401') {
|
|
|
|
return retry(e);
|
|
|
|
}
|
2018-08-08 00:40:17 +02:00
|
|
|
}
|
|
|
|
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(
|
2019-02-26 22:53:11 +01:00
|
|
|
encryptStream(blobStream(blob), key)
|
2018-08-08 00:40:17 +02:00
|
|
|
);
|
2020-07-25 07:04:42 +02:00
|
|
|
await setFileList(this.bearerToken, kid, encrypted);
|
2018-08-08 00:40:17 +02:00
|
|
|
} catch (e) {
|
2020-07-25 07:04:42 +02:00
|
|
|
if (e.message === '401') {
|
|
|
|
return retry(e);
|
|
|
|
}
|
2018-08-08 00:40:17 +02:00
|
|
|
}
|
|
|
|
return changes;
|
|
|
|
}
|
|
|
|
|
|
|
|
toJSON() {
|
2018-11-08 22:35:19 +01:00
|
|
|
return this.info;
|
2018-08-08 00:40:17 +02:00
|
|
|
}
|
|
|
|
}
|