Skip to content

Let management assert a member's app identity to connectors - #246

Merged
tnkuehne merged 1 commit into
feat/user-connection-entrypointfrom
feat/user-connection-identity
Nov 20, 2026
Merged

tnkuehne merged 1 commit into
feat/user-connection-entrypointfrom
feat/user-connection-identity

Conversation

@tnkuehne

@tnkuehne tnkuehne commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Second layer of the personal connections stack. Management names a member's app identity from what it already knows, and the runtime accepts UserConnection calls only from management.

Background

Connectors store each user's account under the app identity that app sign-in issues: issuer plus subject. My connections runs in management, so management must name that identity for the signed-in member.

Organization App identity What management already has
No required company SSO (PLATFORM_URL/api/auth, user ID): apps sign in through Widefleet the user
Required company SSO, Google or OIDC (issuer, sub) the SSO account row; management and app sign-in use the same registration, so the sub is the same
Required company SSO, Entra (issuer, oid) the SSO account row, but keyed by the registration's pairwise sub

Only Entra differed. Entra's sub is pairwise per app registration; app sign-in uses the stable object ID, as Better Auth's own Microsoft provider does since 1.7.0 (#10204). @better-auth/sso still keys every OIDC account by sub, and 1.7.0 removed its mapping.id.

What changes

Management keys Entra accounts by object ID

  • patches/@[email protected] adds an accountSubject option, like Generic OAuth's: the account identifier of a verified OIDC sign-in, defaulting to sub.
  • Management returns the oid for Entra providers and rejects tokens from another tenant. Other providers keep sub.
  • Migration 0034 rekeys existing Entra SSO accounts from the ID token saved at their last sign-in. An ID token that is not a readable JWT leaves its account unchanged.
  • The MCP CLI login finds the account by the app identity's subject. The separate account subject in MCP tokens is gone.

createAppUserIdentity returns (issuer, subject, name):

  • Without required SSO: the Widefleet account, only for members with a verified email, as app sign-in requires.
  • With required SSO: the issuer app sign-in uses, and the member's SSO account. A session without one, such as a recovery session, is refused with 403.

Runtime endpoint /.well-known/widefleet/user-connections, reachable only through authenticated runtime management:

  • Refuses an identity whose issuer is not the one app sign-in publishes (409).
  • Finds the organization's app exporting UserConnection (404 otherwise). App access groups do not apply: a member manages only their own account, while using the app still requires them.
  • Passes the connector a caller proof valid for five minutes. Allows only status, prepare and disconnect, checks their results, keeps launch links on the connector's origin and does not relay connector errors.

Tests

  • app-user-identity.integration.test.ts, real SSO callback against an Entra fixture whose oid differs from sub:
    • the account row holds the object ID
    • the identity's issuer matches the published app sign-in
    • a repeated sign-in finds the same account
    • another tenant is rejected without creating an account
    • other providers keep sub
    • Widefleet mode, including the refusal for an unverified email
    • a member without a company account is refused
    • the migration rekeys an Entra account, skips unreadable ID tokens and social accounts, and the person still signs in afterwards
  • Without the patch's Entra rule, the object-ID and tenant cases fail.
  • managed-oauth-runtime.test.ts, real loader: unauthenticated and malformed requests, a foreign issuer, a foreign app, access groups, isolation between users, and disconnection.
  • mcp-cli-login.integration.test.ts and the Entra login edge test now expect the object ID.

RetriggerView in GreptileConfidence Score: 4/5 Tier: plus

Fix the unreadable-token migration path before merging; one affected row can prevent management from starting.

Findings

  1. P1 Unreadable tokens stop upgrades ▶

Summary

Management can name a member's app identity and call personal connector controls through authenticated runtime management.

  • Management can manage a member's connector account through the runtime.
  • Management now uses the identity that app sign-in gives each member.

Diagram

sequenceDiagram
  participant M as Management
  participant D as Member and account records
  participant R as Runtime management
  participant C as Connector UserConnection
  M->>D: Resolve member's issuer and subject
  D-->>M: App identity
  M->>R: Target app, identity and allowed action
  R->>R: Check published issuer and app
  R->>C: Action with five-minute caller proof
  C-->>R: Status, launch link or disconnect result
  R->>R: Check result and launch origin
  R-->>M: Checked result or fixed error
Loading

Reviews (6) · Last reviewed commit: "Let management assert a member's app ide..." · Reviewed by Greptile

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
widefleet-docs bc81f07 Nov 20 2026, 06:20 PM

@tnkuehne
tnkuehne force-pushed the feat/user-connection-identity branch 2 times, most recently from 2100d48 to 06fd28b Compare November 20, 2026 17:43
tnkuehne added a commit that referenced this pull request Nov 20, 2026
Better Auth stores OAuth access and refresh tokens in plain text unless
`account.encryptOAuthTokens` is set; its option documentation calls
plain storage "a security risk if your database is compromised".
Management saves them for company SSO and social sign-in but never reads
them, so a database copy would carry usable grants to people's company
and social accounts.

## What changes

- Management sets `account.encryptOAuthTokens: true`. Better Auth
encrypts access and refresh tokens with the authentication secret
(XChaCha20-Poly1305) and decrypts them where it reads them.
- Tokens saved before stay readable (Better Auth recognizes plain
values) and are replaced with encrypted ones at the next sign-in.
- Better Auth keeps ID tokens unencrypted. The Entra migration in #246
reads them, so this PR does not depend on the merge order.

## Tests

- The Entra login edge test checks that the saved access token differs
from the issued one and decrypts to it with the authentication secret.
- All Vitest suites pass, as do the edge, installation and Entra suites.


<!-- greptile_comment -->

<!-- greptile_summary -->

<h2><a
href="https://app.greptile.com/api/retrigger?id=114375365&amp;sig=ca646197c96ad091136eb9197185750bd193c6790d8caff5dd10834820a44341"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://greptile-static-assets.s3.amazonaws.com/badges/RetriggerDark.svg?v=2"><source
media="(prefers-color-scheme: light)"
srcset="https://greptile-static-assets.s3.amazonaws.com/badges/Retrigger.svg?v=2"><img
alt="Retrigger"
src="https://greptile-static-assets.s3.amazonaws.com/badges/Retrigger.svg?v=2"
align="right"></picture></a><a
href="https://app.greptile.com/-/switch-tenant?tenant=mendra&amp;next=%2Fwidefleet%2Fwidefleet%2Fpull%2F252&amp;utm_source=greptile_review&amp;utm_medium=github&amp;utm_campaign=view_in_greptile"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptileDark.svg?v=2"><source
media="(prefers-color-scheme: light)"
srcset="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptile.svg?v=2"><img
alt="View in Greptile"
src="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptile.svg?v=2"
align="right"></picture></a>Confidence Score: 5/5</h2>

<!-- greptile-impact-verdict --> The PR appears safe to merge; no
actionable issues were found.

<details open><summary><h3>Summary</h3></summary>

Management enables Better Auth’s encryption for saved provider access
and refresh tokens.
- Company and social sign-ins save provider access and refresh tokens
encrypted.
</details>

<!-- greptile_confidence_score:5 -->

<sub>Reviews (1) · Last reviewed commit: ["Encrypt provider tokens that
management
..."](44196e3)
· [Reviewed by
Greptile](https://www.greptile.com/?utm_source=greptile_review&utm_medium=github&utm_campaign=review_public&utm_content=widefleet/widefleet)</sub>

<!-- /greptile_comment -->
Connectors store each user's account under the app identity that app
sign-in issues: the company issuer and subject, or the Widefleet account
when apps sign in through Widefleet. My connections needs management to
name that identity for the signed-in member.

Management already knows it. With required company SSO the member's SSO
account links them to their company identity, and both sign-ins use the
same registration. Only Entra differed: app sign-in identifies users by
object ID, while Better Auth's SSO plugin keys accounts by the
registration's pairwise `sub`.

- A patch adds an `accountSubject` option to `@better-auth/sso`, as Better
  Auth's own Microsoft provider moved to `oid` in 1.7.0. Management sets it
  for Entra providers, checking the tenant; other providers keep `sub`.
- Migration 0034 rekeys existing Entra accounts from the ID token saved at
  their last sign-in.
- The MCP CLI login finds the account by the app identity's subject; the
  separate account subject is gone.

The runtime accepts `UserConnection` calls only through authenticated
management. It checks that the identity's issuer is the one app sign-in
publishes, finds the organization's app and passes the connector a caller
proof valid for five minutes. App access groups do not apply: a member
manages only their own account, while using the app still requires them.
It allows only `status`, `prepare` and `disconnect`, checks their results,
keeps launch links on the connector's origin and does not relay connector
errors.
@tnkuehne
tnkuehne force-pushed the feat/user-connection-identity branch from 06fd28b to bc81f07 Compare November 20, 2026 18:19
@tnkuehne
tnkuehne merged commit ecd4d55 into main Nov 20, 2026
14 of 15 checks passed
@tnkuehne
tnkuehne deleted the feat/user-connection-identity branch November 20, 2026 18:19
Comment on lines +19 to +23
)::jsonb ->> 'oid' AS oid
FROM account
JOIN sso_provider ON sso_provider.provider_id = account.provider_id
WHERE sso_provider.app_identity_type = 'entra'
AND account.id_token ~ '^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.'

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Unreadable tokens stop upgrades

If a saved Entra id_token contains a dotted but unreadable value such as e30.eA.signature, it passes this filter but decodes to x. The ::jsonb cast then throws instead of skipping the row. Migration 0034 fails, preventing management from starting.

Catch decoding and parsing failures per row, and add a dotted unreadable token to the migration test.

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.

1 participant