| 1 | import { type Database, schema } from '@ghost/db'; |
| 2 | import { and, eq |
| 3 | |
| 4 | import { OauthAppRestrictedError } |
| 5 | |
| 6 | /** Whether the organization's OAuth app restrictions keep the app out of its repositories: they are on, and the app is neither built into the instance, nor the organization's own, nor approved by it. A null `clientId` is an app that cannot be identified, which restrictions always keep out. */ |
| 7 | export async function oauthAppBlocked |
| 8 | db: Pick |
| 9 | { |
| 10 | clientId, |
| 11 | organizationId, |
| 12 | }: { clientId |
| 13 | ) { |
| 14 | const [settings] |
| 15 | .select({ |
| 16 | blocked: sql<boolean |
| 17 | }) |
| 18 | .from(schema.organizationSettings |
| 19 | .where( |
| 20 | and( |
| 21 | eq(schema.organizationSettings |
| 22 | eq(schema.organizationSettings |
| 23 | ), |
| 24 | ); |
| 25 | return settings?.blocked |
| 26 | } |
| 27 | |
| 28 | /** Refuses a write into the organization behind `slug`, such as creating a repository there, by an OAuth app its restrictions keep out. */ |
| 29 | export async function requireOauthAppAllowed |
| 30 | db: Pick |
| 31 | clientId: string, |
| 32 | slug: string, |
| 33 | ) { |
| 34 | const [organization] |
| 35 | .select({ id: |
| 36 | .from(schema.organization |
| 37 | .where(eq(schema |
| 38 | if ( |
| 39 | organization && |
| 40 | (await oauthAppBlocked(db |
| 41 | ) |
| 42 | throw new OauthAppRestrictedError( |
| 43 | } |