Skip to content

fix(oauth): surface safe token exchange failures - #2698

Merged
benjaminshafii merged 2 commits into
different-ai:devfrom
reachjalil:feature/native-oauth-diagnostics
Jul 13, 2026
Merged

benjaminshafii merged 2 commits into
different-ai:devfrom
reachjalil:feature/native-oauth-diagnostics

Conversation

@reachjalil

@reachjalil reachjalil commented Jul 13, 2026 •

Copy link
Copy Markdown
Member

Important

This is a focused native-provider OAuth fix. It covers Den's Microsoft 365/Google-style OAuth callback and token exchange. It does not implement or claim conformance with Microsoft Work IQ, Microsoft Enterprise MCP, Agent 365, or ServiceNow MCP.

Relationship to the package-first MCP work

The Microsoft 365 quick add is a native OAuth/Microsoft Graph integration. This PR improves and verifies that native path; it does not use @openwork/enterprise-mcp-client, even when DEN_ENABLE_ENTERPRISE_MCP_CLIENT=true is enabled. That flag selects Den's external remote MCP runtime only.

The longer-term direction is to place reusable remote MCP behavior behind package contracts. Migrating this native Microsoft 365 provider is outside the current MCP package scope and must not be inferred from the successful connection below.

Managerial summary

We set out to make an administrator's failed Connect attempt identify the actual source of failure.

During a real Microsoft 365 development connection, Microsoft sign-in and consent succeeded and the browser returned to Den. Den then displayed only:

Token request failed with status 401.

That message proved the HTTP status but hid the actionable provider diagnosis. A controlled replay of the same token request showed:

  • OAuth error: invalid_client;
  • Entra code: AADSTS7000215;
  • first failed stage: OAuth token acquisition/client authentication;
  • root cause: Den held a different client-secret value from the active Azure app registration.

The tenant ID, client ID, application state, web redirect type, exact localhost callback, authorization code return, and PKCE request were all correct. Replacing the stored secret value and starting a fresh authorization completed the connection.

Before and after

Before Added here Administrator outcome
Every non-2xx token response became Token request failed with status <n>. A bounded OAuth error parser retains safe error codes and provider correlation fields. The administrator knows which OAuth sub-step failed and who should act.
The callback reflected an arbitrary thrown error string into HTML. Callback messages come from an allowlisted safe mapping; unknown provider bodies are redacted. Secrets, tokens, codes, and hostile provider text cannot be echoed into the browser.
Native OAuth callback failures were not logged with a stable stage/reference. Structured callback logging includes Den request ID, provider ID, AUTH_TOKEN_ACQUISITION, stable code, status, and safe provider trace/correlation fields. Support can correlate the browser reference with provider and Den evidence.
invalid_client, invalid_grant, invalid scope, denied consent, and provider availability looked alike. Each maps to distinct safe wording and remediation. Replacing credentials is no longer suggested for a stale authorization code or provider outage.

For the verified Microsoft failure, the callback now says:

Microsoft rejected the client secret during OAuth token exchange (AADSTS7000215). An organization administrator should replace the client secret value and try Connect again.

The page also displays the Den diagnostic reference.

Root-cause record

Field Verified result
Plane Den-managed native provider OAuth
Topology Browser → Microsoft Entra authorization → Den callback/token exchange → Microsoft Graph capability routes
Highest passing state Authorization callback returned with valid code/state
First failed state AUTH_TOKEN_ACQUISITION / client authentication
Provider evidence HTTP 401, invalid_client, AADSTS7000215, trace/correlation/timestamp
Fix owner Organization administrator / Azure app registration
Remediation Save the current client-secret value, not Secret ID; start a fresh Connect
Recovery verification Real development connection succeeded after credential correction

Safe diagnostic contract

Retained when valid and bounded:

  • HTTP status;
  • OAuth error identifier;
  • first numeric provider error code;
  • provider trace ID, correlation ID, and timestamp;
  • stable OpenWork error code and OAuth phase.

Never retained or returned:

  • client secret;
  • authorization code;
  • access or refresh token;
  • PKCE verifier;
  • raw provider error description;
  • arbitrary response fields or response body.

Microsoft AADSTS7000215 receives provider-specific wording because it was verified against the development tenant and is a stable Entra error class. A ServiceNow-style invalid_client remains provider-generic unless an approved ServiceNow test instance supplies authoritative wire evidence.

Validation

Passed on Node 24 / pnpm 11.4 / Bun:

pnpm --filter @openwork-ee/den-db build
pnpm --filter @openwork-ee/den-api exec bun test \
  test/generic-oauth-token-response.test.ts \
  test/microsoft-365-provider-scopes.test.ts
pnpm --filter @openwork-ee/den-api exec tsc --noEmit
git diff --check

Result: 10 tests passed / 41 expectations.

Coverage includes:

  • the real token-request path sends client secret, PKCE verifier, and exact localhost callback;
  • Microsoft AADSTS7000215 classification and correlation evidence;
  • generic/ServiceNow-style invalid_client classification without invented provider wording;
  • unknown/malformed provider-body redaction;
  • successful token response regression;
  • tenant-scoped Microsoft authority and delegated read-only scope regression.

Reviewer boundaries

Verified:

  • real Microsoft authorization and callback;
  • real invalid-secret token response;
  • real recovery after correcting the secret;
  • safe parser and request-path regression tests.

Not verified by this PR:

  • live ServiceNow;
  • Microsoft Work IQ, Microsoft Enterprise MCP, or Agent 365;
  • token refresh after long-lived production use;
  • customer proxy, CA, Conditional Access, licensing, or sovereign-cloud behavior;
  • provider operations beyond successful native Microsoft account connection.

Those belong to the enterprise mock/client and approved-tenant verification tracks, not this focused fix.

@vercel

vercel Bot commented Jul 13, 2026 •

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
openwork-landing Ready Ready Preview, Comment, Open in v0 Jul 13, 2026 2:13am

@vercel

vercel Bot commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

@reachjalil is attempting to deploy a commit to the Different AI Team on Vercel.

A member of the Team first needs to authorize it.

@benjaminshafii
benjaminshafii marked this pull request as ready for review July 13, 2026 02:49
@benjaminshafii
benjaminshafii merged commit d145cbf into different-ai:dev Jul 13, 2026
10 checks passed
benjaminshafii pushed a commit that referenced this pull request Jul 13, 2026
* feat(den): add Microsoft 365 permission parity

* feat(mcp): add package-first enterprise mock lab (#2670)

* feat(mcp): add enterprise mock foundation artifacts

* chore(mcp): wire enterprise mock workspaces

* ci(mcp): enforce enterprise mock checks

* fix(mcp): emit a truly invalid schema fault

* feat(mcp): configure exact mock OAuth redirects

* feat(mcp): strengthen mock OAuth verification

* fix(mcp): accept Azure localhost OAuth callbacks

* fix(mcp): close mock sockets across scenario restarts

* fix(mcp): make mock listener restarts explicit

* fix(oauth): surface safe token exchange failures (#2698)

* fix(oauth): surface safe token exchange failures

* test(oauth): use synthetic provider diagnostics

* feat(mcp): add package-first enterprise MCP client (#2694)

* feat(mcp): add enterprise MCP client package

* refactor(mcp): harden enterprise client contracts

* fix(mcp): preserve the first failing request phase

---------

Co-authored-by: Benjamin Shafii <[email protected]>

* feat(den): add Microsoft 365 permission parity

* fix(oauth): use localhost for local Den callbacks

---------

Co-authored-by: Benjamin Shafii <[email protected]>
@reachjalil
reachjalil deleted the feature/native-oauth-diagnostics branch July 17, 2026 07:06

This branch was successfully deployed

1 active deployment
Preview – openwork-landing — 374c0b95 Deployed Jul 13, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants