Skip to content

Commit

Permalink
core: fix 2fa codes not being sent
Browse files Browse the repository at this point in the history
  • Loading branch information
thecodrr committed May 11, 2024
1 parent 5028bb1 commit 400aa57
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
5 changes: 4 additions & 1 deletion packages/core/src/api/mfa-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ class MFAManager {
}

async sendCode(method: "sms" | "email") {
const token = await this.tokenManager.getAccessToken();
const token = await this.tokenManager.getAccessToken([
"IdentityServerApi",
"auth:grant_types:mfa"
]);
if (!token) throw new Error("Unauthorized.");

return await http.post(
Expand Down
17 changes: 15 additions & 2 deletions packages/core/src/api/token-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ export type Token = {
refresh_token: string;
};

type Scope = (typeof SCOPES)[number];

const SCOPES = [
"notesnook.sync",
"offline_access",
"IdentityServerApi",
"auth:grant_types:mfa",
"auth:grant_types:mfa_password"
] as const;
const ENDPOINTS = {
token: "/connect/token",
revoke: "/connect/revocation",
Expand Down Expand Up @@ -79,10 +88,14 @@ class TokenManager {
return scopes.includes("offline_access") && Boolean(refresh_token);
}

async getAccessToken(forceRenew = false) {
async getAccessToken(
scopes: Scope[] = ["notesnook.sync", "IdentityServerApi"],
forceRenew = false
) {
return await getSafeToken(async () => {
const token = await this.getToken(true, forceRenew);
if (!token || token.scope.includes("auth:grant_types")) return;
if (!token) return;
if (!scopes.some((s) => token.scope.includes(s))) return;
return token.access_token;
}, "Error getting access token:");
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/api/user-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ class UserManager {
username: email,
password: hashedPassword,
grant_type: code ? "mfa" : "password",
scope: "notesnook.sync offline_access openid IdentityServerApi",
scope: "notesnook.sync offline_access IdentityServerApi",
client_id: "notesnook",
"mfa:code": code,
"mfa:method": method
Expand Down

0 comments on commit 400aa57

Please sign in to comment.