2020-12-16 12:06:25 +05:00
|
|
|
import http from "../utils/http";
|
|
|
|
|
import constants from "../utils/constants";
|
|
|
|
|
import TokenManager from "./token-manager";
|
2021-03-06 09:44:57 +05:00
|
|
|
import { EV, EVENTS } from "../common";
|
2020-12-16 12:06:25 +05:00
|
|
|
|
|
|
|
|
const ENDPOINTS = {
|
|
|
|
|
signup: "/users",
|
|
|
|
|
token: "/connect/token",
|
|
|
|
|
user: "/users",
|
|
|
|
|
deleteUser: "/users/delete",
|
|
|
|
|
patchUser: "/account",
|
2020-12-16 14:56:09 +05:00
|
|
|
verifyUser: "/account/verify",
|
2020-12-16 12:06:25 +05:00
|
|
|
revoke: "/connect/revocation",
|
2020-12-22 09:36:35 +05:00
|
|
|
recoverAccount: "/account/recover",
|
2020-12-16 12:06:25 +05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class UserManager {
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param {import("./index").default} db
|
|
|
|
|
*/
|
|
|
|
|
constructor(db) {
|
|
|
|
|
this._db = db;
|
|
|
|
|
this.tokenManager = new TokenManager(db);
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-22 16:13:01 +05:00
|
|
|
async init() {
|
|
|
|
|
const user = await this.getUser();
|
|
|
|
|
if (!user) return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-16 12:06:25 +05:00
|
|
|
async signup(email, password) {
|
2021-01-18 23:29:49 +05:00
|
|
|
const hashedPassword = await this._db.context.hash(password, email);
|
2020-12-16 12:06:25 +05:00
|
|
|
await http.post(`${constants.API_HOST}${ENDPOINTS.signup}`, {
|
|
|
|
|
email,
|
2021-01-18 23:29:49 +05:00
|
|
|
password: hashedPassword,
|
2020-12-22 09:55:31 +05:00
|
|
|
client_id: "notesnook",
|
2020-12-16 12:06:25 +05:00
|
|
|
});
|
2021-02-26 17:33:46 +05:00
|
|
|
EV.publish(EVENTS.userSignedUp);
|
2021-05-25 16:19:58 +05:00
|
|
|
return await this.login(email, password, hashedPassword);
|
2021-01-18 23:29:49 +05:00
|
|
|
}
|
|
|
|
|
|
2021-05-25 16:19:58 +05:00
|
|
|
async login(email, password, hashedPassword) {
|
2021-04-09 12:55:32 +05:00
|
|
|
if (!hashedPassword) {
|
|
|
|
|
hashedPassword = await this._db.context.hash(password, email);
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-16 12:06:25 +05:00
|
|
|
await this.tokenManager.saveToken(
|
|
|
|
|
await http.post(`${constants.AUTH_HOST}${ENDPOINTS.token}`, {
|
|
|
|
|
username: email,
|
2021-01-19 09:48:42 +05:00
|
|
|
password: hashedPassword,
|
2020-12-16 12:06:25 +05:00
|
|
|
grant_type: "password",
|
|
|
|
|
scope: "notesnook.sync offline_access openid IdentityServerApi",
|
|
|
|
|
client_id: "notesnook",
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
|
2021-05-25 16:19:58 +05:00
|
|
|
const user = await this.fetchUser();
|
2020-12-16 12:06:25 +05:00
|
|
|
await this._db.context.deriveCryptoKey(`_uk_@${user.email}`, {
|
|
|
|
|
password,
|
|
|
|
|
salt: user.salt,
|
|
|
|
|
});
|
|
|
|
|
|
2021-01-23 10:48:21 +05:00
|
|
|
EV.publish(EVENTS.userLoggedIn, user);
|
2020-12-16 12:06:25 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getSessions() {
|
|
|
|
|
const token = await this.tokenManager.getAccessToken();
|
|
|
|
|
if (!token) return;
|
|
|
|
|
await http.get(`${constants.AUTH_HOST}/account/sessions`, token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async logout(revoke = true, reason) {
|
2020-12-22 16:13:01 +05:00
|
|
|
try {
|
|
|
|
|
if (revoke) await this.tokenManager.revokeToken();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e);
|
|
|
|
|
} finally {
|
|
|
|
|
await this._db.context.clear();
|
2021-01-23 10:48:21 +05:00
|
|
|
EV.publish(EVENTS.userLoggedOut, reason);
|
2020-12-22 16:13:01 +05:00
|
|
|
}
|
2020-12-16 12:06:25 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setUser(user) {
|
|
|
|
|
if (!user) return;
|
|
|
|
|
return this._db.context.write("user", user);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getUser() {
|
|
|
|
|
return this._db.context.read("user");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async deleteUser(password) {
|
|
|
|
|
let token = await this.tokenManager.getAccessToken();
|
|
|
|
|
if (!token) return;
|
2021-01-19 09:48:42 +05:00
|
|
|
const user = await this.getUser();
|
2020-12-16 12:06:25 +05:00
|
|
|
await http.post(
|
|
|
|
|
`${constants.API_HOST}${ENDPOINTS.deleteUser}`,
|
2021-01-19 09:48:42 +05:00
|
|
|
{ password: await this._db.context.hash(password, user.email) },
|
2020-12-16 12:06:25 +05:00
|
|
|
token
|
|
|
|
|
);
|
|
|
|
|
await this.logout(false, "Account deleted.");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-25 16:19:58 +05:00
|
|
|
async fetchUser() {
|
2020-12-16 12:06:25 +05:00
|
|
|
try {
|
|
|
|
|
let token = await this.tokenManager.getAccessToken();
|
|
|
|
|
if (!token) return;
|
|
|
|
|
const user = await http.get(
|
|
|
|
|
`${constants.API_HOST}${ENDPOINTS.user}`,
|
|
|
|
|
token
|
|
|
|
|
);
|
|
|
|
|
if (user) {
|
2021-01-08 01:56:41 +05:00
|
|
|
const oldUser = await this.getUser();
|
|
|
|
|
if (!!oldUser && !oldUser.isEmailConfirmed && user.isEmailConfirmed) {
|
2021-01-05 13:07:37 +05:00
|
|
|
// generate new token
|
|
|
|
|
const token = await this.tokenManager.getToken(false);
|
|
|
|
|
await this.tokenManager._refreshToken(token);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-25 16:19:58 +05:00
|
|
|
await this.setUser(user);
|
2021-01-23 10:48:21 +05:00
|
|
|
EV.publish(EVENTS.userFetched, user);
|
2020-12-16 12:06:25 +05:00
|
|
|
return user;
|
|
|
|
|
} else {
|
2020-12-16 14:41:28 +05:00
|
|
|
return await this.getUser();
|
2020-12-16 12:06:25 +05:00
|
|
|
}
|
2021-01-22 22:08:56 +05:00
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e);
|
|
|
|
|
return await this.getUser();
|
2020-12-16 12:06:25 +05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-22 13:13:18 +05:00
|
|
|
changePassword(oldPassword, newPassword) {
|
|
|
|
|
return this._updatePassword("change_password", {
|
|
|
|
|
old_password: oldPassword,
|
|
|
|
|
new_password: newPassword,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resetPassword(newPassword) {
|
|
|
|
|
return this._updatePassword("reset_password", {
|
|
|
|
|
new_password: newPassword,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-16 12:06:25 +05:00
|
|
|
async getEncryptionKey() {
|
|
|
|
|
const user = await this.getUser();
|
|
|
|
|
if (!user) return;
|
|
|
|
|
const key = await this._db.context.getCryptoKey(`_uk_@${user.email}`);
|
|
|
|
|
return { key, salt: user.salt };
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-22 13:13:18 +05:00
|
|
|
async sendVerificationEmail() {
|
|
|
|
|
let token = await this.tokenManager.getAccessToken();
|
|
|
|
|
if (!token) return;
|
|
|
|
|
await http.post(
|
|
|
|
|
`${constants.AUTH_HOST}${ENDPOINTS.verifyUser}`,
|
|
|
|
|
null,
|
|
|
|
|
token
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
recoverAccount(email) {
|
|
|
|
|
return http.post(`${constants.AUTH_HOST}${ENDPOINTS.recoverAccount}`, {
|
|
|
|
|
email,
|
|
|
|
|
client_id: "notesnook",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async _updatePassword(type, data) {
|
2020-12-16 12:06:25 +05:00
|
|
|
let token = await this.tokenManager.getAccessToken();
|
|
|
|
|
if (!token) return;
|
2021-01-19 09:48:42 +05:00
|
|
|
|
|
|
|
|
// we hash the passwords beforehand
|
2021-02-26 17:33:46 +05:00
|
|
|
const { email, salt } = await this.getUser();
|
2021-01-19 09:48:42 +05:00
|
|
|
var hashedData = {};
|
|
|
|
|
if (data.old_password)
|
|
|
|
|
hashedData.old_password = await this._db.context.hash(
|
|
|
|
|
data.old_password,
|
|
|
|
|
email
|
|
|
|
|
);
|
|
|
|
|
if (data.new_password)
|
|
|
|
|
hashedData.new_password = await this._db.context.hash(
|
|
|
|
|
data.new_password,
|
|
|
|
|
email
|
|
|
|
|
);
|
|
|
|
|
|
2020-12-16 12:06:25 +05:00
|
|
|
await http.patch(
|
2020-12-22 14:18:53 +05:00
|
|
|
`${constants.AUTH_HOST}${ENDPOINTS.patchUser}`,
|
2020-12-16 12:06:25 +05:00
|
|
|
{
|
2020-12-22 13:13:18 +05:00
|
|
|
type,
|
2021-01-19 09:48:42 +05:00
|
|
|
...hashedData,
|
2020-12-16 12:06:25 +05:00
|
|
|
},
|
|
|
|
|
token
|
|
|
|
|
);
|
2020-12-22 14:18:53 +05:00
|
|
|
await this._db.outbox.add(
|
|
|
|
|
type,
|
2020-12-22 14:25:01 +05:00
|
|
|
{ newPassword: data.new_password },
|
2020-12-22 14:18:53 +05:00
|
|
|
async () => {
|
2021-02-26 17:33:46 +05:00
|
|
|
await this._db.sync(true);
|
|
|
|
|
|
2020-12-22 14:18:53 +05:00
|
|
|
await this._db.context.deriveCryptoKey(`_uk_@${email}`, {
|
2020-12-22 14:25:01 +05:00
|
|
|
password: data.new_password,
|
2021-02-26 17:33:46 +05:00
|
|
|
salt,
|
2020-12-22 14:18:53 +05:00
|
|
|
});
|
2021-02-26 17:33:46 +05:00
|
|
|
|
|
|
|
|
await this._db.sync(false, true);
|
2020-12-22 14:18:53 +05:00
|
|
|
}
|
|
|
|
|
);
|
2020-12-16 12:06:25 +05:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default UserManager;
|