2020-12-16 12:06:25 +05:00
|
|
|
import http from "../utils/http";
|
|
|
|
|
import constants from "../utils/constants";
|
2021-08-08 12:20:07 +05:00
|
|
|
import { EV, EVENTS } from "../common";
|
2020-12-16 12:06:25 +05:00
|
|
|
|
|
|
|
|
const ENDPOINTS = {
|
|
|
|
|
token: "/connect/token",
|
|
|
|
|
revoke: "/connect/revocation",
|
2020-12-22 09:47:01 +05:00
|
|
|
temporaryToken: "/account/token",
|
2021-01-04 11:22:24 +05:00
|
|
|
logout: "/account/logout",
|
2020-12-16 12:06:25 +05:00
|
|
|
};
|
|
|
|
|
|
2021-08-08 12:20:07 +05:00
|
|
|
var isRefreshingToken = false;
|
2020-12-16 12:06:25 +05:00
|
|
|
class TokenManager {
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param {import("./index").default} db
|
|
|
|
|
*/
|
|
|
|
|
constructor(db) {
|
|
|
|
|
this._db = db;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-25 16:19:58 +05:00
|
|
|
async getToken(renew = true, forceRenew = false) {
|
2021-05-28 23:10:10 +05:00
|
|
|
let token = await this._db.context.read("token");
|
2020-12-16 12:06:25 +05:00
|
|
|
if (!token) return;
|
2021-05-25 16:19:58 +05:00
|
|
|
if (forceRenew || (renew && this._isTokenExpired(token))) {
|
2020-12-16 12:06:25 +05:00
|
|
|
await this._refreshToken(token);
|
|
|
|
|
return await this.getToken();
|
|
|
|
|
}
|
|
|
|
|
return token;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_isTokenExpired(token) {
|
|
|
|
|
const { t, expires_in } = token;
|
2021-08-08 12:20:07 +05:00
|
|
|
const expiryMs = t + expires_in * 1000;
|
2020-12-16 12:06:25 +05:00
|
|
|
return Date.now() >= expiryMs;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-25 16:19:58 +05:00
|
|
|
async getAccessToken(forceRenew = false) {
|
|
|
|
|
try {
|
|
|
|
|
const token = await this.getToken(true, forceRenew);
|
|
|
|
|
if (!token) return;
|
|
|
|
|
return token.access_token;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error("Error getting access token:", e);
|
|
|
|
|
if (e.message === "invalid_grant" || e.message === "invalid_client") {
|
2021-05-26 12:40:15 +05:00
|
|
|
EV.publish(EVENTS.userSessionExpired);
|
2021-05-25 16:19:58 +05:00
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2020-12-16 12:06:25 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async _refreshToken(token) {
|
|
|
|
|
const { refresh_token, scope } = token;
|
2021-03-11 14:39:48 +05:00
|
|
|
if (!refresh_token || !scope) return;
|
2021-08-08 12:20:07 +05:00
|
|
|
|
|
|
|
|
if (isRefreshingToken) {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
2021-08-31 12:11:46 +05:00
|
|
|
EV.subscribe(EVENTS.tokenRefreshed, onTokenRefreshed, true);
|
2021-08-08 12:20:07 +05:00
|
|
|
|
|
|
|
|
function onTokenRefreshed({ success, error }) {
|
|
|
|
|
clearTimeout(timeout);
|
|
|
|
|
if (success) return resolve();
|
|
|
|
|
else return reject(error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const timeout = setTimeout(() => {
|
2021-08-31 12:11:46 +05:00
|
|
|
EV.unsubscribe(EVENTS.tokenRefreshed, onTokenRefreshed);
|
2021-08-08 12:20:07 +05:00
|
|
|
reject("Timeout while refreshing token.");
|
|
|
|
|
}, 15000);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isRefreshingToken = true;
|
|
|
|
|
try {
|
|
|
|
|
await this.saveToken(
|
|
|
|
|
await http.post(`${constants.AUTH_HOST}${ENDPOINTS.token}`, {
|
|
|
|
|
refresh_token,
|
|
|
|
|
grant_type: "refresh_token",
|
|
|
|
|
scope: scope,
|
|
|
|
|
client_id: "notesnook",
|
|
|
|
|
})
|
|
|
|
|
);
|
2021-08-31 12:11:46 +05:00
|
|
|
EV.publish(EVENTS.tokenRefreshed, { success: true });
|
2021-08-08 12:20:07 +05:00
|
|
|
} catch (e) {
|
2021-08-31 12:11:46 +05:00
|
|
|
EV.publish(EVENTS.tokenRefreshed, {
|
2021-08-08 12:20:07 +05:00
|
|
|
success: false,
|
|
|
|
|
error: e,
|
|
|
|
|
});
|
|
|
|
|
console.error("Failed to refresh token:", e);
|
|
|
|
|
throw e;
|
|
|
|
|
} finally {
|
|
|
|
|
isRefreshingToken = false;
|
|
|
|
|
}
|
2020-12-16 12:06:25 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async revokeToken() {
|
|
|
|
|
const token = await this.getToken();
|
|
|
|
|
if (!token) return;
|
2021-01-04 11:22:24 +05:00
|
|
|
const { access_token } = token;
|
2020-12-16 12:06:25 +05:00
|
|
|
|
2021-01-04 11:22:24 +05:00
|
|
|
await http.post(
|
|
|
|
|
`${constants.AUTH_HOST}${ENDPOINTS.logout}`,
|
|
|
|
|
null,
|
|
|
|
|
access_token
|
|
|
|
|
);
|
2020-12-16 12:06:25 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
saveToken(tokenResponse) {
|
2021-05-28 23:10:10 +05:00
|
|
|
let token = { ...tokenResponse, t: Date.now() };
|
|
|
|
|
return this._db.context.write("token", token);
|
2021-04-05 11:32:40 +05:00
|
|
|
}
|
|
|
|
|
|
2020-12-22 09:47:01 +05:00
|
|
|
async getAccessTokenFromAuthorizationCode(userId, authCode) {
|
|
|
|
|
return await this.saveToken(
|
|
|
|
|
await http.post(`${constants.AUTH_HOST}${ENDPOINTS.temporaryToken}`, {
|
|
|
|
|
authorization_code: authCode,
|
|
|
|
|
user_id: userId,
|
|
|
|
|
client_id: "notesnook",
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
}
|
2020-12-16 12:06:25 +05:00
|
|
|
}
|
|
|
|
|
export default TokenManager;
|