Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

6.6 KB
Raw Blame
const { net } = require("electron");
const { runOAuthLoopbackFlow, OAuthFlowError } = require("./oauthLoopbackFlow");

// The "common" tenant covers both work/school (M365) and personal
// (outlook.com) accounts. Desktop apps are public clients: PKCE, no secret.
const MICROSOFT_AUTH_URL = "https://login.microsoftonline.com/common/oauth2/v2.0/authorize";
const MICROSOFT_TOKEN_URL = "https://login.microsoftonline.com/common/oauth2/v2.0/token";
// "profile" puts preferred_username in the id_token and User.Read allows the
// GET /me fallback — work accounts often omit the "email" claim.
const CALENDAR_SCOPE =
  "openid profile email offline_access https://graph.microsoft.com/User.Read https://graph.microsoft.com/Calendars.Read";

function decodeIdToken(idToken) {
  if (!idToken) return null;
  try {
    return JSON.parse(Buffer.from(idToken.split(".")[1], "base64url").toString());
  } catch {
    return null;
  }
}

// The Entra tenant tells a personal Microsoft account from a work or school
// one, whatever the address's domain.
function tenantIdOf(tokenData) {
  return decodeIdToken(tokenData.id_token)?.tid ?? null;
}

class MicrosoftCalendarOAuth {
  constructor(databaseManager) {
    this.databaseManager = databaseManager;
    this._refreshInFlight = new Map();
  }

  getClientId() {
    return process.env.MICROSOFT_CALENDAR_CLIENT_ID;
  }

  startOAuthFlow() {
    if (!this.getClientId()) {
      // Fail fast instead of opening the browser on a client_id=undefined URL
      // and hanging until the loopback flow times out.
      throw new Error("MICROSOFT_CALENDAR_CLIENT_ID is not configured");
    }
    return runOAuthLoopbackFlow({
      errorParam: "mcal_error",
      buildAuthUrl: (redirectUri, state, codeChallenge) => {
        const params = new URLSearchParams({
          client_id: this.getClientId(),
          redirect_uri: redirectUri,
          response_type: "code",
          scope: CALENDAR_SCOPE,
          // Without this, SSO silently re-links the signed-in account when
          // the user tries to add another one.
          prompt: "select_account",
          state,
          code_challenge: codeChallenge,
          code_challenge_method: "S256",
        });
        return `${MICROSOFT_AUTH_URL}?${params.toString()}`;
      },
      handleCallback: async (code, redirectUri, codeVerifier) => {
        const tokenData = await this.exchangeCodeForTokens(code, redirectUri, codeVerifier);

        if (tokenData.error) {
          throw new OAuthFlowError(
            "token_exchange_failed",
            `Token exchange failed: ${tokenData.error_description || tokenData.error}`
          );
        }

        const email = await this._resolveEmail(tokenData);
        if (!email) {
          throw new OAuthFlowError(
            "no_email",
            "Could not extract email from Microsoft OAuth response"
          );
        }

        this._saveTokens(email, tokenData);
        return { success: true, email, tenantId: tenantIdOf(tokenData) };
      },
    });
  }

  async exchangeCodeForTokens(code, redirectUri, codeVerifier) {
    const body = new URLSearchParams({
      code,
      client_id: this.getClientId(),
      redirect_uri: redirectUri,
      grant_type: "authorization_code",
      code_verifier: codeVerifier,
    }).toString();

    return this._httpsPost(MICROSOFT_TOKEN_URL, body);
  }

  async refreshAccessToken(refreshToken) {
    const body = new URLSearchParams({
      client_id: this.getClientId(),
      refresh_token: refreshToken,
      grant_type: "refresh_token",
      scope: CALENDAR_SCOPE,
    }).toString();

    return this._httpsPost(MICROSOFT_TOKEN_URL, body);
  }

  async getValidAccessToken(accountEmail) {
    const tokens = this.databaseManager.getMicrosoftTokensByEmail(accountEmail);
    if (!tokens) throw new Error(`No Microsoft tokens found for ${accountEmail}`);

    const fiveMinutes = 5 * 60 * 1000;
    if (tokens.expires_at - fiveMinutes >= Date.now()) {
      return tokens.access_token;
    }

    // Microsoft rotates refresh tokens on every refresh, so concurrent
    // refreshes (focus sync racing the interval sync) would persist competing
    // rotations; share one in-flight refresh per account instead.
    let refreshPromise = this._refreshInFlight.get(accountEmail);
    if (!refreshPromise) {
      refreshPromise = this._refreshAndSaveTokens(tokens).finally(() => {
        this._refreshInFlight.delete(accountEmail);
      });
      this._refreshInFlight.set(accountEmail, refreshPromise);
    }
    return refreshPromise;
  }

  async _refreshAndSaveTokens(tokens) {
    const refreshed = await this.refreshAccessToken(tokens.refresh_token);
    if (refreshed.error) {
      throw new Error(`Token refresh failed: ${refreshed.error_description || refreshed.error}`);
    }

    // Persist the rotated refresh token or the old one stops working within 24h.
    this._saveTokens(tokens.microsoft_email, {
      ...refreshed,
      refresh_token: refreshed.refresh_token || tokens.refresh_token,
      scope: refreshed.scope || tokens.scope,
    });

    return refreshed.access_token;
  }

  _saveTokens(email, tokenData) {
    this.databaseManager.saveMicrosoftTokens({
      microsoft_email: email,
      access_token: tokenData.access_token,
      refresh_token: tokenData.refresh_token,
      expires_at: Date.now() + tokenData.expires_in * 1000,
      scope: tokenData.scope || CALENDAR_SCOPE,
      tenant_id: tenantIdOf(tokenData),
    });
  }

  // Work accounts often omit the id_token "email" claim; fall back to
  // preferred_username, then Graph /me.
  async _resolveEmail(tokenData) {
    const payload = decodeIdToken(tokenData.id_token);
    if (payload?.email) return payload.email;
    if (payload?.preferred_username?.includes("@")) return payload.preferred_username;

    try {
      const response = await net.fetch("https://graph.microsoft.com/v1.0/me", {
        headers: { Authorization: `Bearer ${tokenData.access_token}` },
        signal: AbortSignal.timeout(10000),
        useSessionCookies: false,
      });
      const me = await response.json();
      return me.mail || me.userPrincipalName || null;
    } catch {
      return null;
    }
  }

  async _httpsPost(urlString, body) {
    const response = await net.fetch(urlString, {
      method: "POST",
      headers: { "Content-Type": "application/x-www-form-urlencoded" },
      body,
      signal: AbortSignal.timeout(10000),
      useSessionCookies: false,
    });
    const text = await response.text();
    try {
      return JSON.parse(text);
    } catch {
      throw new Error(`Invalid JSON response: ${text.slice(0, 200)}`);
    }
  }
}

module.exports = MicrosoftCalendarOAuth;