Files
notesnook/packages/core/api/mfa-manager.js

143 lines
3.3 KiB
JavaScript
Raw Normal View History

/*
This file is part of the Notesnook project (https://notesnook.com/)
Copyright (C) 2022 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2022-08-30 16:13:11 +05:00
2022-03-11 22:49:24 +05:00
import http from "../utils/http";
import constants from "../utils/constants";
import TokenManager from "./token-manager";
const ENDPOINTS = {
2022-03-21 10:23:35 +05:00
setup: "/mfa",
enable: "/mfa",
disable: "/mfa",
recoveryCodes: "/mfa/codes",
send: "/mfa/send"
2022-03-11 22:49:24 +05:00
};
class MFAManager {
/**
*
* @param {import("../database/storage").default} storage
* @param {import("../api/index").default} db
*/
constructor(storage, db) {
this._storage = storage;
this._db = db;
this.tokenManager = new TokenManager(storage);
}
/**
*
* @param {"app" | "sms" | "email"} type
* @param {string} phoneNumber
* @returns
*/
async setup(type, phoneNumber = undefined) {
const token = await this.tokenManager.getAccessToken();
if (!token) return;
return await http.post(
`${constants.AUTH_HOST}${ENDPOINTS.setup}`,
{
type,
phoneNumber
2022-03-11 22:49:24 +05:00
},
token
);
}
/**
*
* @param {"app" | "sms" | "email"} type
* @param {string} code
* @returns
*/
async enable(type, code) {
return this._enable(type, code, false);
}
/**
*
* @param {"app" | "sms" | "email"} type
* @param {string} code
* @returns
*/
async enableFallback(type, code) {
2022-03-21 10:23:35 +05:00
return this._enable(type, code, true);
}
/**
*
* @param {"app" | "sms" | "email"} type
* @param {string} code
* @param {boolean} isFallback
* @private
* @returns
*/
async _enable(type, code, isFallback) {
2022-03-11 22:49:24 +05:00
const token = await this.tokenManager.getAccessToken();
if (!token) return;
2022-03-21 10:23:35 +05:00
return await http.patch(
2022-03-11 22:49:24 +05:00
`${constants.AUTH_HOST}${ENDPOINTS.enable}`,
{ type, code, isFallback },
2022-03-11 22:49:24 +05:00
token
);
}
async disable() {
const token = await this.tokenManager.getAccessToken();
if (!token) return;
2022-03-21 10:23:35 +05:00
return await http.delete(
`${constants.AUTH_HOST}${ENDPOINTS.disable}`,
token
);
}
/**
* Generate new 2FA recovery codes or get count of valid recovery codes.
* @param {boolean} generate
* @returns
*/
2022-03-17 22:56:33 +05:00
async codes() {
const token = await this.tokenManager.getAccessToken();
if (!token) return;
return await http.get(
2022-03-17 22:56:33 +05:00
`${constants.AUTH_HOST}${ENDPOINTS.recoveryCodes}`,
token
);
}
2022-03-21 10:23:35 +05:00
/**
* @param {"sms" | "email"} method
* @param {string} phoneNumber
* @returns
*/
async sendCode(method, token) {
if (!token) throw new Error("Token is required to make this request.");
return await http.post(
`${constants.AUTH_HOST}${ENDPOINTS.send}`,
{
type: method
2022-03-21 10:23:35 +05:00
},
token
);
}
2022-03-11 22:49:24 +05:00
}
export default MFAManager;