The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import * as Neon from "@distilled.cloud/neon"; |
| 2 | import { OwnedBySomeoneElse, Unowned } from "alchemy/AdoptPolicy"; |
| 3 | import { isResolved } from "alchemy/Diff"; |
| 4 | import type { Input } from "alchemy/Input"; |
| 5 | import * as Provider from "alchemy/Provider"; |
| 6 | import { Resource } from "alchemy/Resource"; |
| 7 | import * as Data from "effect/Data"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Redacted from "effect/Redacted"; |
| 10 | import { authPlanScope, authRequest, InvalidManagedAuth, removedAuthSettings } from "./Auth.ts"; |
| 11 | import type { Providers } from "./Providers.ts"; |
| 12 | |
| 13 | export class AuthOAuthProviderUnexpectedResponse extends Data.TaggedError( |
| 14 | "AuthOAuthProviderUnexpectedResponse", |
| 15 | )<{ |
| 16 | readonly message: string; |
| 17 | readonly cause?: unknown; |
| 18 | }> {} |
| 19 | |
| 20 | export interface AuthOAuthProviderProps { |
| 21 | /** Managed Auth integration; its scope is inherited without owning the parent. */ |
| 22 | auth: { projectId: string; branchId: string }; |
| 23 | /** Supported OAuth provider identity. Changing it replaces this child. */ |
| 24 | provider: Neon.NeonAuthOauthProviderId; |
| 25 | /** OAuth client ID. Omit for Neon-managed development credentials. */ |
| 26 | clientId?: string; |
| 27 | /** OAuth secret, never included in resource outputs. */ |
| 28 | clientSecret?: Redacted.Redacted<string>; |
| 29 | /** Microsoft tenant ID; write-only in the current read schema. */ |
| 30 | microsoftTenantId?: string; |
| 31 | } |
| 32 | |
| 33 | export interface AuthOAuthProviderAttributes { |
| 34 | /** Neon project identity. */ |
| 35 | projectId: string; |
| 36 | /** Owning branch identity. */ |
| 37 | branchId: string; |
| 38 | /** OAuth provider identity. */ |
| 39 | provider: Neon.NeonAuthOauthProviderId; |
| 40 | /** Whether Neon-managed development or application credentials are active. */ |
| 41 | type: Neon.NeonAuthOauthProviderType; |
| 42 | /** Public OAuth client identifier, when configured. */ |
| 43 | clientId: string | undefined; |
| 44 | } |
| 45 | |
| 46 | export interface AuthOAuthProvider extends Resource< |
| 47 | "Neon.AuthOAuthProvider", |
| 48 | AuthOAuthProviderProps, |
| 49 | AuthOAuthProviderAttributes, |
| 50 | never, |
| 51 | Providers |
| 52 | > {} |
| 53 | |
| 54 | /** |
| 55 | * Independently own one managed Auth OAuth provider. Shared credentials are for |
| 56 | * development only. Existing providers, including inherited providers, require |
| 57 | * explicit adoption. Removing managed credential fields is rejected; replace the |
| 58 | * child explicitly to switch back to development credentials. |
| 59 | * |
| 60 | * ### Configure an OAuth application |
| 61 | * **Example:** GitHub sign-in |
| 62 | * ```typescript |
| 63 | * yield* Neon.AuthOAuthProvider("GitHub", { |
| 64 | * auth, provider: "github", clientId: "application-id", |
| 65 | * clientSecret: yield* Config.Redacted("GITHUB_CLIENT_SECRET"), |
| 66 | * }); |
| 67 | * ``` |
| 68 | * |
| 69 | * @resource |
| 70 | * @product Auth |
| 71 | */ |
| 72 | export const AuthOAuthProvider = Resource<AuthOAuthProvider>("Neon.AuthOAuthProvider"); |
| 73 | |
| 74 | const observe = ( |
| 75 | scope: { projectId: string; branchId: string }, |
| 76 | provider: Neon.NeonAuthOauthProviderId, |
| 77 | ) => |
| 78 | Neon.listBranchNeonAuthOauthProviders(authRequest(scope)).pipe( |
| 79 | Effect.map((response) => response.providers.find((item) => item.id === provider)), |
| 80 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 81 | ); |
| 82 | |
| 83 | const attributes = ( |
| 84 | scope: { projectId: string; branchId: string }, |
| 85 | value: Neon.NeonAuthOauthProvider, |
| 86 | ): AuthOAuthProviderAttributes => ({ |
| 87 | projectId: scope.projectId, |
| 88 | branchId: scope.branchId, |
| 89 | provider: value.id, |
| 90 | type: value.type, |
| 91 | clientId: value.client_id, |
| 92 | }); |
| 93 | |
| 94 | const validateOAuthRemoval = ( |
| 95 | olds: AuthOAuthProviderProps | undefined, |
| 96 | news: Input<AuthOAuthProviderProps>, |
| 97 | ) => { |
| 98 | const removed = removedAuthSettings(olds, news, [ |
| 99 | "clientId", |
| 100 | "clientSecret", |
| 101 | "microsoftTenantId", |
| 102 | ]); |
| 103 | return removed.length === 0 |
| 104 | ? Effect.void |
| 105 | : Effect.fail( |
| 106 | new InvalidManagedAuth({ |
| 107 | message: `Cannot remove managed OAuth settings: ${removed.join(", ")}; replace the OAuth child explicitly`, |
| 108 | }), |
| 109 | ); |
| 110 | }; |
| 111 | |
| 112 | export const AuthOAuthProviderProvider = () => |
| 113 | Provider.succeed(AuthOAuthProvider, { |
| 114 | stables: ["projectId", "branchId", "provider"], |
| 115 | diff: Effect.fn(function* ({ olds, news, output }) { |
| 116 | yield* validateOAuthRemoval(olds, news); |
| 117 | const scope = "auth" in news ? yield* authPlanScope({ branch: news.auth }) : undefined; |
| 118 | if ( |
| 119 | output && |
| 120 | (!scope || |
| 121 | scope.projectId !== output.projectId || |
| 122 | scope.branchId !== output.branchId || |
| 123 | !("provider" in news) || |
| 124 | news.provider !== output.provider) |
| 125 | ) |
| 126 | return { action: "replace" } as const; |
| 127 | if (!isResolved(news)) return; |
| 128 | if ( |
| 129 | news.auth.projectId !== olds.auth.projectId || |
| 130 | news.auth.branchId !== olds.auth.branchId || |
| 131 | news.provider !== olds.provider |
| 132 | ) { |
| 133 | return { action: "replace" } as const; |
| 134 | } |
| 135 | if (output) { |
| 136 | const current = yield* observe(news.auth, news.provider); |
| 137 | if (!current || (news.clientId !== undefined && current.client_id !== news.clientId)) |
| 138 | return { action: "update" } as const; |
| 139 | } |
| 140 | }), |
| 141 | read: Effect.fn(function* ({ olds, output }) { |
| 142 | const scope = output ?? olds.auth; |
| 143 | const current = yield* observe(scope, output?.provider ?? olds.provider); |
| 144 | if (!current) return undefined; |
| 145 | const attrs = attributes(scope, current); |
| 146 | return output ? attrs : Unowned(attrs); |
| 147 | }), |
| 148 | reconcile: Effect.fn(function* ({ news, output, olds }) { |
| 149 | yield* validateOAuthRemoval(olds, news); |
| 150 | if ( |
| 151 | output && |
| 152 | (news.auth.projectId !== output.projectId || |
| 153 | news.auth.branchId !== output.branchId || |
| 154 | news.provider !== output.provider) |
| 155 | ) |
| 156 | return yield* new InvalidManagedAuth({ |
| 157 | message: "OAuth identity changed without replacement; refusing to mutate either provider", |
| 158 | }); |
| 159 | const request = authRequest(news.auth); |
| 160 | let current = yield* observe(news.auth, news.provider); |
| 161 | if (current && !output) |
| 162 | return yield* new OwnedBySomeoneElse({ |
| 163 | message: "Existing OAuth provider requires explicit adoption", |
| 164 | resourceType: "Neon.AuthOAuthProvider", |
| 165 | }); |
| 166 | if (!current) { |
| 167 | yield* Neon.addBranchNeonAuthOauthProvider({ |
| 168 | ...request, |
| 169 | id: news.provider, |
| 170 | client_id: news.clientId, |
| 171 | client_secret: news.clientSecret, |
| 172 | microsoft_tenant_id: news.microsoftTenantId, |
| 173 | }).pipe( |
| 174 | Effect.catchTag("Conflict", () => |
| 175 | Effect.fail( |
| 176 | new OwnedBySomeoneElse({ |
| 177 | message: "OAuth provider appeared during creation; explicit adoption is required", |
| 178 | resourceType: "Neon.AuthOAuthProvider", |
| 179 | }), |
| 180 | ), |
| 181 | ), |
| 182 | ); |
| 183 | current = yield* observe(news.auth, news.provider); |
| 184 | } |
| 185 | const secretMatches = |
| 186 | news.clientSecret === undefined || |
| 187 | (current?.client_secret !== undefined && |
| 188 | (Redacted.isRedacted(current.client_secret) |
| 189 | ? Redacted.value(current.client_secret) |
| 190 | : current.client_secret) === Redacted.value(news.clientSecret)); |
| 191 | // The API may mask secrets and does not expose the Microsoft tenant on GET. |
| 192 | if ( |
| 193 | (news.clientId !== undefined && news.clientId !== current?.client_id) || |
| 194 | !secretMatches || |
| 195 | news.microsoftTenantId !== undefined |
| 196 | ) { |
| 197 | yield* Neon.updateBranchNeonAuthOauthProvider({ |
| 198 | ...request, |
| 199 | oauth_provider_id: news.provider, |
| 200 | client_id: news.clientId, |
| 201 | client_secret: news.clientSecret, |
| 202 | microsoft_tenant_id: news.microsoftTenantId, |
| 203 | }); |
| 204 | } |
| 205 | const result = yield* Neon.listBranchNeonAuthOauthProviders(request); |
| 206 | const provider = result.providers.find((item) => item.id === news.provider); |
| 207 | if (!provider) |
| 208 | return yield* new AuthOAuthProviderUnexpectedResponse({ |
| 209 | message: "OAuth provider was not visible after reconciliation", |
| 210 | }); |
| 211 | return attributes(news.auth, provider); |
| 212 | }), |
| 213 | delete: Effect.fn(function* ({ output }) { |
| 214 | if (yield* observe(output, output.provider)) { |
| 215 | yield* Neon.deleteBranchNeonAuthOauthProvider({ |
| 216 | ...authRequest(output), |
| 217 | oauth_provider_id: output.provider, |
| 218 | }).pipe(Effect.catchTag("NotFound", () => Effect.void)); |
| 219 | } |
| 220 | }), |
| 221 | }); |