2018-09-14 17:00:33 +02:00
|
|
|
/* global LIMITS AUTH_CONFIG */
|
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();
|
|
|
|
|
|
|
|
export default class User {
|
2018-09-14 17:00:33 +02:00
|
|
|
constructor(storage) {
|
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;
|
|
|
|
}
|
|
|
|
|
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) {
|
2018-09-25 22:54:47 +02:00
|
|
|
return assets.get('firefox_logo-only.svg');
|
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
|
|
|
}
|
|
|
|
|
|
|
|
get maxSize() {
|
|
|
|
return this.loggedIn ? LIMITS.MAX_FILE_SIZE : LIMITS.ANON.MAX_FILE_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
get maxExpireSeconds() {
|
|
|
|
return this.loggedIn
|
|
|
|
? LIMITS.MAX_EXPIRE_SECONDS
|
|
|
|
: LIMITS.ANON.MAX_EXPIRE_SECONDS;
|
|
|
|
}
|
|
|
|
|
|
|
|
get maxDownloads() {
|
|
|
|
return this.loggedIn ? LIMITS.MAX_DOWNLOADS : LIMITS.ANON.MAX_DOWNLOADS;
|
|
|
|
}
|
|
|
|
|
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 = {
|
2018-09-14 17:00:33 +02:00
|
|
|
client_id: AUTH_CONFIG.client_id,
|
|
|
|
code_challenge,
|
|
|
|
code_challenge_method: 'S256',
|
|
|
|
response_type: 'code',
|
|
|
|
scope: 'profile https://identity.mozilla.com/apps/send', //TODO param
|
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;
|
|
|
|
}
|
|
|
|
const params = new URLSearchParams(options);
|
2018-09-14 17:00:33 +02:00
|
|
|
location.assign(
|
|
|
|
`${AUTH_CONFIG.authorization_endpoint}?${params.toString()}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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');
|
|
|
|
}
|
2018-09-14 17:00:33 +02:00
|
|
|
const tokenResponse = await fetch(AUTH_CONFIG.token_endpoint, {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
code,
|
|
|
|
client_id: AUTH_CONFIG.client_id,
|
|
|
|
code_verifier: this.storage.get('pkceVerifier')
|
|
|
|
})
|
|
|
|
});
|
|
|
|
const auth = await tokenResponse.json();
|
|
|
|
const infoResponse = await fetch(AUTH_CONFIG.userinfo_endpoint, {
|
|
|
|
method: 'GET',
|
|
|
|
headers: {
|
|
|
|
Authorization: `Bearer ${auth.access_token}`
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const userInfo = await infoResponse.json();
|
|
|
|
userInfo.access_token = auth.access_token;
|
|
|
|
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
|
|
|
|
|
|
|
logout() {
|
|
|
|
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 = [];
|
|
|
|
try {
|
|
|
|
const encrypted = await getFileList(this.bearerToken);
|
|
|
|
const decrypted = await streamToArrayBuffer(
|
2018-11-08 22:35:19 +01:00
|
|
|
decryptStream(encrypted, b64ToArray(this.info.fileListKey))
|
2018-08-08 00:40:17 +02:00
|
|
|
);
|
|
|
|
list = JSON.parse(textDecoder.decode(decrypted));
|
|
|
|
} catch (e) {
|
2018-11-06 00:56:59 +01:00
|
|
|
if (e.message === '401') {
|
2018-11-06 20:36:20 +01:00
|
|
|
this.logout();
|
|
|
|
return { incoming: true };
|
2018-11-06 00:56:59 +01:00
|
|
|
}
|
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(
|
2018-11-08 22:35:19 +01:00
|
|
|
encryptStream(blobStream(blob), b64ToArray(this.info.fileListKey))
|
2018-08-08 00:40:17 +02:00
|
|
|
);
|
|
|
|
await setFileList(this.bearerToken, encrypted);
|
|
|
|
} catch (e) {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
return changes;
|
|
|
|
}
|
|
|
|
|
|
|
|
toJSON() {
|
2018-11-08 22:35:19 +01:00
|
|
|
return this.info;
|
2018-08-08 00:40:17 +02:00
|
|
|
}
|
|
|
|
}
|