OAuth callback: mode and provider role decide what a completion mints
The start gate only refused early. The callback ignored the signed state's mode, so a Joystick connect for StreamPixels redeemed into a viewer_access session. callbackMint(profile, provider, mode) now decides at the callback: identity providers keep a viewer session (Bifrost discord connect unchanged); a connect for a managed-connection-only provider stores custody and mints a claim with no facts, no capabilities and no refresh token (the refresh route would recompute viewer_access from the account); anything else is refused. StreamPixels' redeem reads only account, provider, mode, connection and claimSet.identities, so it accepts the capability-free claim unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
6bdaed2a9b
commit
79ea0007d3
7 changed files with 164 additions and 14 deletions
|
|
@ -250,3 +250,22 @@ export function isAllowedReturnOrigin(profile: AppProfile, returnTo: string): bo
|
|||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* What a completed OAuth callback may mint, decided at the callback from the
|
||||
* signed state's mode and the provider's role in the profile (the start gate
|
||||
* is only an early refusal). `viewer_session`: the provider is one of the
|
||||
* profile's `identityProviders`, so the completion signs the account in with
|
||||
* the profile's capabilities. `custody_only`: a `connect` for a provider that
|
||||
* is only a managed connection (Joystick on StreamPixels) stores the creator
|
||||
* credential and mints a claim with no capabilities and no refresh token.
|
||||
* `refused`: any other combination, which the start gate never admits.
|
||||
*/
|
||||
export type CallbackMint = "viewer_session" | "custody_only" | "refused";
|
||||
|
||||
export function callbackMint(profile: AppProfile, provider: Provider, mode: OAuthMode): CallbackMint {
|
||||
if (profile.identityProviders.includes(provider)) {
|
||||
return "viewer_session";
|
||||
}
|
||||
return mode === "connect" && profile.managedConnectionProviders.includes(provider) ? "custody_only" : "refused";
|
||||
}
|
||||
|
|
|
|||
20
src/app.ts
20
src/app.ts
|
|
@ -23,7 +23,7 @@ import {
|
|||
type Provider,
|
||||
} from "./contracts.js";
|
||||
import { issueAccessClaim, type IssueAccessClaimInput } from "./claims.js";
|
||||
import { builtInAppProfiles, getAppProfile, isAllowedReturnOrigin, serializeAppProfile, admitsProviderForMode } from "./app-profiles.js";
|
||||
import { builtInAppProfiles, getAppProfile, isAllowedReturnOrigin, serializeAppProfile, admitsProviderForMode, callbackMint } from "./app-profiles.js";
|
||||
import { renderBrowserHandoffPage } from "./browser-handoff.js";
|
||||
import { type HeimdallConfig, loadConfig } from "./config.js";
|
||||
import { createTokenCustody, type TokenCustody } from "./custody.js";
|
||||
|
|
@ -1008,6 +1008,11 @@ export async function buildApp(options: BuildAppOptions = {}): Promise<FastifyIn
|
|||
const runtime = oauthRuntimes[request.params.provider];
|
||||
|
||||
try {
|
||||
const callbackProfile = getAppProfile(statePayload.app_slug);
|
||||
const mint = callbackProfile ? callbackMint(callbackProfile, request.params.provider, statePayload.mode) : "refused";
|
||||
if (mint === "refused") {
|
||||
throw new Error("The signed state names a mode the provider has no role for in this app.");
|
||||
}
|
||||
const tokenSet = await runtime.exchangeAuthorizationCode({
|
||||
config,
|
||||
code: request.query.code,
|
||||
|
|
@ -1093,6 +1098,7 @@ export async function buildApp(options: BuildAppOptions = {}): Promise<FastifyIn
|
|||
accountId: account.id,
|
||||
linkedIdentities: await store.listLinkedIdentitiesForAccount(account.id),
|
||||
facts: [...entitlementEvaluation.facts, ...buildGrantFacts(grants)],
|
||||
...(mint === "custody_only" ? { withoutCapabilities: true } : {}),
|
||||
};
|
||||
if (account.displayName !== undefined) {
|
||||
issueInput.displayName = account.displayName;
|
||||
|
|
@ -1104,6 +1110,14 @@ export async function buildApp(options: BuildAppOptions = {}): Promise<FastifyIn
|
|||
store,
|
||||
input: issueInput,
|
||||
});
|
||||
// A custody-only completion carries no refresh token: the refresh
|
||||
// route recomputes capabilities from the account's facts, so one
|
||||
// would turn the capability-free session into a viewer session.
|
||||
const { refreshToken: issuedRefreshToken, refresh: issuedRefresh, ...issuedWithoutRefresh } = issued;
|
||||
const delivered =
|
||||
mint === "custody_only"
|
||||
? issuedWithoutRefresh
|
||||
: { ...issuedWithoutRefresh, refreshToken: issuedRefreshToken, refresh: issuedRefresh };
|
||||
|
||||
await store.createAuditEvent({
|
||||
accountId: account.id,
|
||||
|
|
@ -1143,7 +1157,7 @@ export async function buildApp(options: BuildAppOptions = {}): Promise<FastifyIn
|
|||
entitlements: entitlementEvaluation,
|
||||
returnTo: statePayload.return_to,
|
||||
connection: statePayload.connection,
|
||||
...issued,
|
||||
...delivered,
|
||||
};
|
||||
if (handoff.kind === "backend_callback") {
|
||||
await maybeDeliverBackendHandoff(handoff, {
|
||||
|
|
@ -1159,7 +1173,7 @@ export async function buildApp(options: BuildAppOptions = {}): Promise<FastifyIn
|
|||
connection: statePayload.connection,
|
||||
account: accountSummary,
|
||||
entitlements: entitlementEvaluation,
|
||||
...issued,
|
||||
...delivered,
|
||||
});
|
||||
|
||||
await store.createAuditEvent({
|
||||
|
|
|
|||
|
|
@ -19,7 +19,13 @@ interface BackendHandoffBase {
|
|||
connection: OAuthConnectionBinding | null;
|
||||
}
|
||||
|
||||
export interface BackendHandoffSuccessPayload extends BackendHandoffBase, IssuedAccessClaimResult {
|
||||
/** A custody-only completion (callbackMint) carries no refresh token. */
|
||||
type RefreshFields = "refreshToken" | "refresh";
|
||||
|
||||
export interface BackendHandoffSuccessPayload
|
||||
extends BackendHandoffBase,
|
||||
Omit<IssuedAccessClaimResult, RefreshFields>,
|
||||
Partial<Pick<IssuedAccessClaimResult, RefreshFields>> {
|
||||
status: "success";
|
||||
mode: OAuthMode;
|
||||
account: AccountSummary;
|
||||
|
|
|
|||
|
|
@ -39,6 +39,12 @@ export interface IssueAccessClaimInput {
|
|||
displayName?: string;
|
||||
linkedIdentities: LinkedIdentityInput[];
|
||||
facts: string[];
|
||||
/**
|
||||
* The claim names the account and its identities and carries no facts and
|
||||
* no capabilities (a managed-credential connect for a provider that is not
|
||||
* an identity provider). The facts are not even `identity.authenticated`.
|
||||
*/
|
||||
withoutCapabilities?: boolean;
|
||||
sessionId?: string;
|
||||
accessRevision?: number;
|
||||
ttlSeconds?: number;
|
||||
|
|
@ -80,13 +86,17 @@ export async function issueAccessClaim(options: {
|
|||
// would name an app no verifier can resolve a profile for.
|
||||
throw new Error(`Cannot issue an access claim for unknown app '${options.input.appSlug}'.`);
|
||||
}
|
||||
const facts = normalizeFacts(options.input.accountId, options.input.linkedIdentities, options.input.facts);
|
||||
const factSet = new Set(facts);
|
||||
const sharedCapabilities = evaluateSharedCapabilities(profile, {
|
||||
accountId: options.input.accountId,
|
||||
facts: factSet,
|
||||
identities: options.input.linkedIdentities,
|
||||
});
|
||||
const withoutCapabilities = options.input.withoutCapabilities === true;
|
||||
const facts = withoutCapabilities
|
||||
? []
|
||||
: normalizeFacts(options.input.accountId, options.input.linkedIdentities, options.input.facts);
|
||||
const sharedCapabilities = withoutCapabilities
|
||||
? []
|
||||
: evaluateSharedCapabilities(profile, {
|
||||
accountId: options.input.accountId,
|
||||
facts: new Set(facts),
|
||||
identities: options.input.linkedIdentities,
|
||||
});
|
||||
|
||||
const issuedAt = nowEpochSeconds();
|
||||
const ttlSeconds = clampTtlSeconds(
|
||||
|
|
@ -176,6 +186,8 @@ export async function issueAccessClaim(options: {
|
|||
kid: options.keys.kid,
|
||||
},
|
||||
sharedCapabilities,
|
||||
hybridCapabilities: profile.capabilities.filter((capability) => capability.mode === "hybrid"),
|
||||
hybridCapabilities: withoutCapabilities
|
||||
? []
|
||||
: profile.capabilities.filter((capability) => capability.mode === "hybrid"),
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue