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

102 lines
2.7 KiB
JavaScript
Raw Normal View History

2020-12-16 12:06:25 +05:00
import http from "../utils/http";
import constants from "../utils/constants";
import { EV, EVENTS } from "../common";
import { withTimeout, Mutex } from "async-mutex";
2020-12-16 12:06:25 +05:00
const ENDPOINTS = {
token: "/connect/token",
revoke: "/connect/revocation",
temporaryToken: "/account/token",
logout: "/account/logout",
2020-12-16 12:06:25 +05:00
};
class TokenManager {
/**
*
* @param {import("../database/storage").default} storage
2020-12-16 12:06:25 +05:00
*/
constructor(storage) {
this._storage = storage;
this._refreshTokenMutex = withTimeout(new Mutex(), 10 * 1000);
2020-12-16 12:06:25 +05:00
}
2021-05-25 16:19:58 +05:00
async getToken(renew = true, forceRenew = false) {
let token = await this._storage.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))) {
await this._refreshToken(forceRenew);
2020-12-16 12:06:25 +05:00
return await this.getToken();
}
return token;
}
_isTokenExpired(token) {
const { t, expires_in } = token;
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") {
EV.publish(EVENTS.userSessionExpired);
2021-05-25 16:19:58 +05:00
}
throw e;
2021-05-25 16:19:58 +05:00
}
2020-12-16 12:06:25 +05:00
}
async _refreshToken(forceRenew = false) {
await this._refreshTokenMutex.runExclusive(async () => {
const token = await this.getToken(false, false);
if (!forceRenew && !this._isTokenExpired(token)) {
return;
}
const { refresh_token, scope } = token;
if (!refresh_token || !scope) return;
await this.saveToken(
await http.post(`${constants.AUTH_HOST}${ENDPOINTS.token}`, {
refresh_token,
grant_type: "refresh_token",
scope: scope,
client_id: "notesnook",
})
);
EV.publish(EVENTS.tokenRefreshed);
});
2020-12-16 12:06:25 +05:00
}
async revokeToken() {
const token = await this.getToken();
if (!token) return;
const { access_token } = token;
2020-12-16 12:06:25 +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._storage.write("token", token);
2021-04-05 11:32:40 +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;