(
owner: Owner,
slug: OAuthClientSlug,
)
| 1610 | // Load an oauth_client row by (owner, slug). |
| 1611 | // ----------------------------------------------------------------------- |
| 1612 | const loadClient = ( |
| 1613 | owner: Owner, |
| 1614 | slug: OAuthClientSlug, |
| 1615 | ): Effect.Effect<LoadedOAuthClient | null, StorageFailure> => { |
| 1616 | // First-party apps resolve from config, never storage. Owner is irrelevant: |
| 1617 | // the app belongs to the DEPLOYMENT, and visibility policy has nothing to |
| 1618 | // narrow — only the minted connection (and its tokens) is owner-scoped. |
| 1619 | if (isFirstPartyOAuthClientSlug(String(slug))) { |
| 1620 | const config = firstPartyBySlug.get(String(slug)); |
| 1621 | return Effect.succeed(config ? loadedFirstPartyClient(config) : null); |
| 1622 | } |
| 1623 | return deps.fuma |
| 1624 | .use("oauth_client.findFirst", (db) => |
| 1625 | looseDb(db).findFirst("oauth_client", { |
| 1626 | where: (b: any) => b.and(b("owner", "=", owner), b("slug", "=", String(slug))), |
| 1627 | }), |
| 1628 | ) |
| 1629 | .pipe( |
| 1630 | Effect.flatMap((row) => { |
| 1631 | if (!row) return Effect.succeed(null); |
| 1632 | const grant = parseGrant(row.grant); |
| 1633 | // EXPLICIT — this row drives the token exchange. An unknown grant is a |
| 1634 | // corrupt row; fail loudly rather than guessing authorization_code and |
| 1635 | // running the wrong flow. |
| 1636 | if (grant === null) { |
| 1637 | return Effect.fail( |
| 1638 | new StorageError({ |
| 1639 | message: `oauth_client ${String(slug)} has an unknown grant: ${String(row.grant)}`, |
| 1640 | cause: undefined, |
| 1641 | }), |
| 1642 | ); |
| 1643 | } |
| 1644 | const tokenEndpointAuthMethod = parseStoredTokenEndpointAuthMethod( |
| 1645 | row.token_endpoint_auth_method, |
| 1646 | ); |
| 1647 | if (tokenEndpointAuthMethod === null) { |
| 1648 | return Effect.fail( |
| 1649 | new StorageError({ |
| 1650 | message: `oauth_client ${String(slug)} has an unknown token endpoint auth method: ${String(row.token_endpoint_auth_method)}`, |
| 1651 | cause: undefined, |
| 1652 | }), |
| 1653 | ); |
| 1654 | } |
| 1655 | // `client_secret_item_id` is null for DCR-minted / public PKCE clients; |
| 1656 | // the token exchange treats a missing secret as "public client, omit |
| 1657 | // client_secret" (see pickClientAuth). A confidential client persisted |
| 1658 | // its secret to the provider in createClient; resolve it back here. |
| 1659 | return Effect.gen(function* () { |
| 1660 | let clientSecret = ""; |
| 1661 | if (row.client_secret_item_id != null) { |
| 1662 | const provider = deps.defaultWritableProvider(); |
| 1663 | if (provider) { |
| 1664 | const itemId = String(row.client_secret_item_id); |
| 1665 | const resolved = yield* provider.get(ProviderItemId.make(itemId)); |
| 1666 | if ( |
| 1667 | resolved === null && |
| 1668 | parseCredentialWriteAttempt(row.credential_write) !== null |
| 1669 | ) { |
no test coverage detected