OpenViking icon indicating copy to clipboard operation
OpenViking copied to clipboard

[Feature]: Vika email Connector: OAuth2 sign-in for Microsoft 365 / Outlook.com mailboxes

Open xuhaobin217-alt opened this issue 1 month ago • 1 comments

Issue Origin

Observed or reproduced in a real environment

Problem Statement

Vika's email Connector supports exactly one authentication mechanism: IMAP LOGIN with a username and password, and SMTP AUTH PLAIN / AUTH LOGIN.

  • vika/packages/daemon/src/email/imap.mjs:220 sends LOGIN
  • vika/packages/daemon/src/email/smtp.mjs:117 offers only AUTH PLAIN / AUTH LOGIN
  • the account file requires password (vika/packages/daemon/src/email/client.mjs:47)
  • vika/README.md:166 states that OAuth-only mailboxes are unsupported
  • vika/packages/cli/src/email.mjs:40 hard-rejects the consumer Outlook domains

Exchange Online permanently disabled Basic authentication for IMAP, so every such mailbox fails regardless of what is entered. Measured against outlook.office365.com:993 on 2026-11-03:

* CAPABILITY IMAP4 IMAP4rev1 AUTH=XOAUTH2 LOGINDISABLED SASL-IR UIDPLUS MOVE ID UNSELECT CHILDREN IDLE NAMESPACE LITERAL+
a1 LOGIN "[email protected]" "***"
a1 NO [AUTHENTICATIONFAILED] Basic authentication is disabled.

SMTP still advertises AUTH LOGIN XOAUTH2 after STARTTLS, so SASL XOAUTH2 is the correct target in both directions.

Because no OAuth path exists today, such a mailbox can only be attached through an external gateway (an extra daemon plus custom TLS trust, since the Connector only dials verified TLS) or a side script that bypasses Vika's "the owner approves every reply" model. Neither is acceptable.

Proposed Solution

Add SASL XOAUTH2 to the existing IMAP/SMTP transport and a Microsoft device-code flow, keeping the account file as the single credential owner (0600) with an optional oauth2 block:

{
  "address": "[email protected]",
  "imap": { "host": "outlook.office365.com", "port": 993 },
  "smtp": { "host": "smtp.office365.com", "port": 587 },
  "oauth2": { "tenant": "common", "clientId": "<entra app id>", "refreshToken": "<opaque>" }
}
  • IMAP: AUTHENTICATE XOAUTH2 <base64> using SASL-IR when advertised, otherwise a continuation; a + <base64 error> challenge is finished with an empty line before reading the tagged NO.
  • SMTP: prefer AUTH XOAUTH2 <base64> when the account has a token; password accounts keep the existing PLAIN → LOGIN order.
  • CLI (vika setup / vika login email): device-code flow against login.microsoftonline.com/{tenant}/oauth2/v2.0/devicecode with scopes offline_access https://outlook.office.com/IMAP.AccessAsUser.All https://outlook.office.com/SMTP.Send, then verify IMAP and SMTP before saving, exactly like the password path.
  • daemon: refresh access tokens from the refresh token, write a rotated refresh token back atomically with mode 0600, and map invalid_grant to "sign in again with vika login email" instead of a password error.
  • Tokens never appear in errors or logs; no new npm dependencies (fetch and AbortSignal.timeout only).
  • Password accounts, event shapes, reply approval and the OpenViking archive path stay unchanged.

Alternatives Considered

  1. Microsoft Graph connector — more future-proof, but needs a new Connector type plus config/daemon plumbing and re-implements the MIME, threading, attachment and archive logic in client.mjs; better as a separate change.
  2. Local OAuth bridge (e.g. DavMail) — no code, but adds a Java daemon and requires IMAPS/SMTPS certificates in the trust store, because the Connector only connects with verified TLS (imap.mjs:115).
  3. EWS — being retired in Exchange Online (disablement starts October 2026, fully disabled April 2027), so not viable.
  4. Keep asking for a password — Exchange Online disabled IMAP Basic authentication permanently, and app passwords are not available for these protocols.

Feature Area

Other (Vika email Connector: vika/packages/daemon/src/email, vika/packages/cli/src/email.mjs)

Use Case

Attach a corporate Microsoft 365 mailbox to a Vika instance: read the inbox read-only, tell the owner about new mail, and reply only after the owner approves the draft. The refresh token outlives the one-hour access token, so the daemon keeps working unattended; when the grant is revoked, vika login email re-authorizes.

Example API (Optional)

{
  "address": "[email protected]",
  "imap": { "host": "outlook.office365.com", "port": 993 },
  "smtp": { "host": "smtp.office365.com", "port": 587 },
  "oauth2": { "tenant": "common", "clientId": "00000000-0000-0000-0000-000000000000", "refreshToken": "<opaque>" }
}

Additional Context

  • Requires a user-supplied Entra app registration: allow public client flows, add the delegated "Office 365 Exchange Online" permissions IMAP.AccessAsUser.All and SMTP.Send, and grant admin consent where the tenant requires it. I do not plan to ship a first-party client id.
  • The mailbox must have IMAP and SMTP AUTH enabled (Get-CASMailbox -Identity ... | fl ImapEnabled,SmtpClientAuthenticationDisabled). If a tenant disables SMTP AUTH entirely, sending would need a Graph follow-up.
  • Observed on Ubuntu 22.04 with Node 24 against the live service; no new dependencies.
  • Out of scope: Graph/EWS/POP3, app-only authentication (IMAP.AccessAsApp), and the separate bot/vikingbot email channel.

Contribution

  • [x] I am willing to contribute to implementing this feature

xuhaobin217-alt avatar Nov 04 '26 02:11 xuhaobin217-alt

Thanks for the detailed proposal! The direction looks reasonable: add OAuth2/XOAUTH2 to the existing IMAP/SMTP connector while keeping password-based accounts and the reply approval flow intact. Please feel free to open a PR if you're interested in implementing it.

For the setup UX, please preserve the current email-address-first flow rather than adding a provider selection step for everyone:

  • Recognize known Microsoft domains such as outlook.com, hotmail.com, live.com, and msn.com and route them to Microsoft OAuth.
  • For an unrecognized/custom domain, offer a Microsoft 365 vs. other mail provider choice, since the address suffix alone cannot identify Microsoft-hosted corporate mailboxes. Other providers can continue with manual server/password configuration.

Please also cover token refresh across startup, IMAP reconnects, and SMTP authentication; avoid concurrent refresh/re-login writes overwriting newer credentials, and keep authentication retries separate from uncertain mail delivery outcomes. Document the user-provided Entra app and tenant-policy prerequisites, and validate Microsoft 365 and personal Outlook.com separately if both are included in the PR.

Regression tests for password accounts, OAuth error/refresh handling, and credential redaction would be appreciated.

krsish avatar Nov 04 '26 07:11 krsish