espennilsen/pi / extensions/pi-a2a
pi-a2a
Flaggedauth-selector.ts
View on GitHub/** Pure policy for selecting peer authentication without handling credentials. */
import type { AuthMode, AuthSelection, AuthSelectionInput } from "./auth-types.ts";
const MODE_STRENGTH: readonly AuthMode[] = ["oauth2+mtls", "oauth2", "legacy-api-key"];
const MODERN_MODES = new Set<AuthMode>(["oauth2", "oauth2+mtls"]);
const VALID_MODES = new Set<AuthMode>(MODE_STRENGTH);
/**
* Select the strongest mutually usable authentication mode for a peer.
* The result is denied rather than falling back when no permitted mode exists.
*/
export function selectPeerAuth(input: AuthSelectionInput): AuthSelection {
const { peer, local = {} } = input;
const modernOnly = input.modernOnly ?? (input.skillId !== undefined && local.modernOnlySkills?.includes(input.skillId));
const localModes = local.supportedAuthModes ?? ["legacy-api-key"];
const peerModes = peer.supportedAuthModes;
const oauthTransportUsable = isUsableOAuthTransport(peer.endpoint, peer.transport?.tls, local.transport?.tls);
const mutuallySupported = MODE_STRENGTH.filter(
(mode) =>
VALID_MODES.has(mode) &&
localModes.includes(mode) &&
peerModes.includes(mode) &&
(mode !== "oauth2" || oauthTransportUsable) &&
// Legacy credentials are allowed only when a trusted Hub or explicit
// static-directory configuration declares that capability.
(mode !== "legacy-api-key" || peer.source !== "agent-card") &&
// mTLS is intentionally unavailable until the SDK client can install a
// certificate-bearing HTTPS transport for the actual request.
mode !== "oauth2+mtls",
);
const permittedModes = mutuallySupported;
if (modernOnly) {
const modernMode = permittedModes.find((mode) => MODERN_MODES.has(mode));
if (modernMode) return selected(modernMode, peer.source);
return denied(peer.source, "modern-auth-required");
}
if (local.preferModern === false && permittedModes.includes("legacy-api-key")) {
return selected("legacy-api-key", peer.source);
}
const mode = permittedModes[0];
return mode ? selected(mode, peer.source) : denied(peer.source, "no-mutual-auth-mode");
}
function isUsableOAuthTransport(endpoint: string, peerTls: boolean | undefined, localTls: boolean | undefined): boolean {
try {
return new URL(endpoint).protocol === "https:" && peerTls !== false && localTls !== false;
} catch {
return false;
}
}
function selected(mode: AuthMode, source: AuthSelection["source"]): AuthSelection {
return { selectedAuthMode: mode, source, denial: null };
}
function denied(source: AuthSelection["source"], reason: "no-mutual-auth-mode" | "modern-auth-required"): AuthSelection {
return { selectedAuthMode: null, source, denial: { reason } };
}
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack." | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |