OAuth start: the profile's role lists decide which provider a mode may use

sign_in and link now take only identityProviders; connect takes only
managedConnectionProviders. supportsProvider(), which unioned the role lists
with entitlementSources and never looked at the mode, is deleted: a Joystick
sign_in for streampixels no longer starts, so a Deru sign-in cannot rewrite
his managed credential row. No built-in profile had a provider reachable
only through the union.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-10-19 12:31:59 +02:00
commit c554c00697
5 changed files with 72 additions and 12 deletions

View file

@ -20,7 +20,7 @@
> still reports `providers=2/6`, which counts it.
>
> **Why it was removed anyway:** `/v1/oauth/spotify/start` is gated by
> `supportsProvider(profile, provider)`, so a provider with no app behind it is
> `admitsProviderForMode(profile, provider, mode)`, so a provider with no app behind it is
> unreachable. While the profile existed, that path was publicly reachable and
> would have run a real OAuth handshake against a real client secret, taking
> custody of a user's Spotify refresh token on behalf of a consumer that could

View file

@ -42,7 +42,7 @@ In the service-first Heimdall version, the most likely reusable slice is:
- provider OAuth authority
- identity-linking authority
- managed Twitch/YouTube/Joystick credential custody for creator connections (Joystick is a managed connection only, not a viewer sign-in provider)
- managed Twitch/YouTube/Joystick credential custody for creator connections (Joystick is a managed connection only, not a viewer sign-in provider: `startOAuthFlow` admits `sign_in` and `link` only for `identityProviders` and `connect` only for `managedConnectionProviders`, and refuses anything else before any redirect)
- entitlement refresh
- shared claim semantics

View file

@ -3,7 +3,7 @@ import {
type CapabilityDefinition,
type CapabilityMode,
} from "./capability-rules.js";
import { type AppSlug, type LinkedIdentityInput, type OAuthEntitlementPolicy, type Provider } from "./contracts.js";
import { type AppSlug, type LinkedIdentityInput, type OAuthEntitlementPolicy, type OAuthMode, type Provider } from "./contracts.js";
import { entitlementFacts, grantFacts, identityFacts } from "./facts.js";
export type { CapabilityDefinition, CapabilityMode };
@ -222,12 +222,17 @@ export function serializeAppProfile(profile: AppProfile): Record<string, unknown
};
}
export function supportsProvider(profile: AppProfile, provider: Provider): boolean {
return (
profile.identityProviders.includes(provider) ||
profile.managedConnectionProviders.includes(provider) ||
profile.entitlementSources.includes(provider)
);
/**
* Whether the profile admits this provider for this mode. The role lists
* decide: `sign_in` and `link` mint viewer identity, so they take only
* `identityProviders`; `connect` takes custody of a creator credential, so it
* takes only `managedConnectionProviders`. A provider in neither list for the
* mode is refused before any redirect.
*/
export function admitsProviderForMode(profile: AppProfile, provider: Provider, mode: OAuthMode): boolean {
return mode === "connect"
? profile.managedConnectionProviders.includes(provider)
: profile.identityProviders.includes(provider);
}
/**

View file

@ -23,7 +23,7 @@ import {
type Provider,
} from "./contracts.js";
import { issueAccessClaim, type IssueAccessClaimInput } from "./claims.js";
import { builtInAppProfiles, getAppProfile, isAllowedReturnOrigin, serializeAppProfile, supportsProvider } from "./app-profiles.js";
import { builtInAppProfiles, getAppProfile, isAllowedReturnOrigin, serializeAppProfile, admitsProviderForMode } from "./app-profiles.js";
import { renderBrowserHandoffPage } from "./browser-handoff.js";
import { type HeimdallConfig, loadConfig } from "./config.js";
import { createTokenCustody, type TokenCustody } from "./custody.js";
@ -494,10 +494,10 @@ export async function startOAuthFlow(
return { statusCode: 404, body: { error: "unknown_app", detail: "No app is registered under that slug." } };
}
if (!supportsProvider(profile, provider)) {
if (!admitsProviderForMode(profile, provider, input.mode)) {
return {
statusCode: 400,
body: { error: "provider_not_supported_for_app", provider, appSlug: profile.slug },
body: { error: "provider_not_supported_for_app", provider, mode: input.mode, appSlug: profile.slug },
};
}

View file

@ -2,6 +2,7 @@ import { inspect } from "node:util";
import { afterEach, describe, expect, it } from "vitest";
import { type FastifyInstance } from "fastify";
import { buildApp, errorIdentity } from "../src/app.js";
import { builtInAppProfiles } from "../src/app-profiles.js";
import { deliverBackendHandoff, type BackendHandoffPayload } from "../src/backend-handoff.js";
import { type HeimdallConfig, loadConfig } from "../src/config.js";
import { entitlementFacts, identityFacts } from "../src/facts.js";
@ -1373,6 +1374,33 @@ describe("Heimdall service", () => {
expect(url.searchParams.get("redirect_uri")).toBe("https://heimdall.gamecult.org/v1/oauth/joystick/callback");
});
it.each(["sign_in", "link"] as const)(
"refuses %s for joystick on streampixels before any redirect, and Deru's managed row is untouched",
async (mode) => {
const store = new InMemoryStore();
const app = await buildApp({ config: createTestConfig(), store });
apps.push(app);
stubJoystick({ channel_id: "deru-chan", username: "deru" });
const stateToken = (await connect(app)).json().stateToken as string;
const connected = await app.inject({
method: "GET",
url: `/v1/oauth/joystick/callback?code=a&state=${encodeURIComponent(stateToken)}`,
});
const accountId = connected.json().account.id as string;
const before = await store.findStoredLinkedIdentityForAccount(accountId, "joystick");
const start = await app.inject({
method: "POST",
url: "/v1/oauth/joystick/start",
payload: { appSlug: "streampixels", mode, returnTo: "https://streampixels.gamecult.org/auth/connect" },
});
expect(start.statusCode).toBe(400);
expect(start.json()).toMatchObject({ error: "provider_not_supported_for_app", provider: "joystick", mode });
expect(start.json().authorizationUrl).toBeUndefined();
expect(await store.findStoredLinkedIdentityForAccount(accountId, "joystick")).toEqual(before);
}
);
function stubJoystick(identity: Record<string, unknown>) {
const calls: Array<{ url: string; headers: Record<string, string>; body?: URLSearchParams | undefined }> = [];
globalThis.fetch = (async (input: string | URL, init?: RequestInit) => {
@ -1990,3 +2018,30 @@ describe("what may be recorded about a caught error", () => {
expect(errorIdentity(error)).toEqual(expected);
});
});
describe("OAuth start admits a provider by the profile's role for the mode", () => {
const allProviders = ["discord", "patreon", "github", "twitch", "youtube", "spotify", "joystick"] as const;
const modes = ["sign_in", "link", "connect"] as const;
it("sign_in and link take identityProviders only; connect takes managedConnectionProviders only, for every profile", async () => {
const app = await buildApp({ config: createTestConfig() });
try {
for (const profile of Object.values(builtInAppProfiles)) {
for (const provider of allProviders) {
for (const mode of modes) {
const start = await app.inject({
method: "POST",
url: `/v1/oauth/${provider}/start`,
payload: { appSlug: profile.slug, mode, returnTo: `${profile.allowedReturnOrigins[0]}/x` },
});
const roleList = mode === "connect" ? profile.managedConnectionProviders : profile.identityProviders;
const refused = start.json().error === "provider_not_supported_for_app";
expect([profile.slug, provider, mode, refused]).toEqual([profile.slug, provider, mode, !roleList.includes(provider)]);
}
}
}
} finally {
await app.close();
}
});
});