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, 03:37 UTC
| 1 | import { EnvironmentId, ProviderInstanceId } from "@t3tools/contracts"; |
| 2 | import * as Schema from "effect/Schema"; |
| 3 | import { oauthCallbackUrl } from "./oauthCallback.ts"; |
| 4 | import { providerAuthReturnUrl } from "./providerAuthReturnUrl.ts"; |
| 5 | |
| 6 | export const CodexAuthHandoff = Schema.Struct({ |
| 7 | authorizationUrl: Schema.String.check(Schema.isMaxLength(16_384)), |
| 8 | returnUrl: Schema.String.check(Schema.isMaxLength(4_096)), |
| 9 | environmentId: EnvironmentId, |
| 10 | instanceId: ProviderInstanceId, |
| 11 | flowId: Schema.NonEmptyString.check(Schema.isMaxLength(128)), |
| 12 | }); |
| 13 | export type CodexAuthHandoff = typeof CodexAuthHandoff.Type; |
| 14 | const Delivery = Schema.Struct({ |
| 15 | environmentId: EnvironmentId, |
| 16 | instanceId: ProviderInstanceId, |
| 17 | flowId: Schema.NonEmptyString.check(Schema.isMaxLength(128)), |
| 18 | callbackUrl: Schema.String.check(Schema.isMaxLength(16_384)), |
| 19 | returnHash: Schema.String.check(Schema.isMaxLength(128)), |
| 20 | }); |
| 21 | |
| 22 | const encodeHandoff = Schema.encodeSync(Schema.fromJsonString(CodexAuthHandoff)); |
| 23 | const decodeHandoff = Schema.decodeUnknownSync(Schema.fromJsonString(CodexAuthHandoff)); |
| 24 | const encodeDelivery = Schema.encodeSync(Schema.fromJsonString(Delivery)); |
| 25 | const decodeDelivery = Schema.decodeUnknownSync(Schema.fromJsonString(Delivery)); |
| 26 | |
| 27 | export function codexAuthorizationRequest(value: string) { |
| 28 | const url = new URL(value); |
| 29 | if ( |
| 30 | value.length > 16_384 || |
| 31 | url.origin !== "https://auth.openai.com" || |
| 32 | url.pathname !== "/api/accounts/authorize" || |
| 33 | url.username || |
| 34 | url.password || |
| 35 | url.hash |
| 36 | ) |
| 37 | throw new Error("Invalid ChatGPT sign-in request."); |
| 38 | const single = (key: string) => { |
| 39 | const values = url.searchParams.getAll(key); |
| 40 | if (values.length !== 1 || !values[0]) throw new Error("Invalid ChatGPT sign-in request."); |
| 41 | return values[0]; |
| 42 | }; |
| 43 | const redirectUri = single("redirect_uri"); |
| 44 | const redirect = new URL(redirectUri); |
| 45 | const state = single("state"); |
| 46 | if ( |
| 47 | !/^http:\/\/127\.0\.0\.1:[1-9]\d{0,4}\/auth\/callback$/u.test(redirectUri) || |
| 48 | Number(redirect.port) > 65_535 || |
| 49 | !/^[\w-]{16,128}$/u.test(state) || |
| 50 | single("response_type") !== "code" || |
| 51 | single("code_challenge_method") !== "S256" || |
| 52 | !/^[\w-]{43}$/u.test(single("code_challenge")) || |
| 53 | !/^(dynamic_agent_client|oaiapp_[\w-]+)$/u.test(single("client_id")) |
| 54 | ) |
| 55 | throw new Error("Invalid ChatGPT sign-in request."); |
| 56 | return { authorizationUrl: url.toString(), redirectUri, state }; |
| 57 | } |
| 58 | |
| 59 | export function codexCallbackUrl(value: string, redirectUri: string, state: string) { |
| 60 | const callback = oauthCallbackUrl(value, redirectUri, state); |
| 61 | const clients = callback.searchParams.getAll("client_id"); |
| 62 | if (clients.length > 1 || (clients.length === 1 && !/^oaiapp_[\w-]+$/u.test(clients[0]!))) |
| 63 | throw new Error("This redirect URL does not belong to the current sign-in."); |
| 64 | return callback; |
| 65 | } |
| 66 | |
| 67 | export function codexAuthHandoffUrl(input: CodexAuthHandoff, development = false) { |
| 68 | const url = new URL(`${development ? "t3code-dev" : "t3code"}://auth/codex`); |
| 69 | url.searchParams.set("request", encodeHandoff(input)); |
| 70 | return url.toString(); |
| 71 | } |
| 72 | |
| 73 | export function readCodexAuthHandoff(value: string, development: boolean) { |
| 74 | try { |
| 75 | const url = new URL(value); |
| 76 | if ( |
| 77 | value.length > 32_768 || |
| 78 | url.protocol !== (development ? "t3code-dev:" : "t3code:") || |
| 79 | url.host !== "auth" || |
| 80 | url.pathname !== "/codex" || |
| 81 | url.username || |
| 82 | url.password || |
| 83 | url.hash || |
| 84 | url.searchParams.getAll("request").length !== 1 |
| 85 | ) |
| 86 | return undefined; |
| 87 | const input = decodeHandoff(url.searchParams.get("request")); |
| 88 | codexAuthorizationRequest(input.authorizationUrl); |
| 89 | if (!providerAuthReturnUrl(input.returnUrl)) return undefined; |
| 90 | return input; |
| 91 | } catch { |
| 92 | return undefined; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /** Codes travel in a fragment, never in hosted web requests or a token store on the helper. */ |
| 97 | export function codexAuthDeliveryUrl(input: CodexAuthHandoff, callbackUrl: string) { |
| 98 | const request = codexAuthorizationRequest(input.authorizationUrl); |
| 99 | codexCallbackUrl(callbackUrl, request.redirectUri, request.state); |
| 100 | const destination = providerAuthReturnUrl(input.returnUrl); |
| 101 | if (!destination) throw new Error("Invalid T3 Code return address."); |
| 102 | const url = new URL(destination); |
| 103 | const delivery = { |
| 104 | environmentId: input.environmentId, |
| 105 | instanceId: input.instanceId, |
| 106 | flowId: input.flowId, |
| 107 | callbackUrl, |
| 108 | returnHash: url.hash, |
| 109 | }; |
| 110 | url.hash = `codex-auth=${encodeURIComponent(encodeDelivery(delivery))}`; |
| 111 | return url.toString(); |
| 112 | } |
| 113 | |
| 114 | export function readCodexAuthDelivery(value: string) { |
| 115 | try { |
| 116 | const url = new URL(value); |
| 117 | if (!url.hash.startsWith("#codex-auth=") || url.hash.length > 32_768) return undefined; |
| 118 | const input = decodeDelivery(decodeURIComponent(url.hash.slice("#codex-auth=".length))); |
| 119 | const destination = providerAuthReturnUrl(value); |
| 120 | if (!destination) return undefined; |
| 121 | const returnUrl = new URL(destination); |
| 122 | returnUrl.hash = input.returnHash; |
| 123 | const sanitized = providerAuthReturnUrl(returnUrl.toString()); |
| 124 | if (!sanitized) return undefined; |
| 125 | return { ...input, returnUrl: sanitized }; |
| 126 | } catch { |
| 127 | return undefined; |
| 128 | } |
| 129 | } |