MCPcopy Create free account
hub / github.com/arctic-cli/interface / fetch

Function fetch

packages/arctic/src/mcp/oauth-callback.ts:70–136  ·  view source on GitHub ↗
(req)

Source from the content-addressed store, hash-verified

68 server = Bun.serve({
69 port: OAUTH_CALLBACK_PORT,
70 fetch(req) {
71 const url = new URL(req.url)
72
73 if (url.pathname !== OAUTH_CALLBACK_PATH) {
74 return new Response("Not found", { status: 404 })
75 }
76
77 const code = url.searchParams.get("code")
78 const state = url.searchParams.get("state")
79 const error = url.searchParams.get("error")
80 const errorDescription = url.searchParams.get("error_description")
81
82 log.info("received oauth callback", { hasCode: !!code, state, error })
83
84 if (error) {
85 const errorMsg = errorDescription || error
86 if (state && pendingAuths.has(state)) {
87 const pending = pendingAuths.get(state)!
88 clearTimeout(pending.timeout)
89 pendingAuths.delete(state)
90 pending.reject(new Error(errorMsg))
91 }
92 return new Response(HTML_ERROR(errorMsg), {
93 headers: { "Content-Type": "text/html" },
94 })
95 }
96
97 if (!code) {
98 return new Response(HTML_ERROR("No authorization code provided"), {
99 status: 400,
100 headers: { "Content-Type": "text/html" },
101 })
102 }
103
104 // Try to find the pending auth by state parameter, or if no state, use the single pending auth
105 let pending: PendingAuth | undefined
106 let pendingKey: string | undefined
107
108 if (state && pendingAuths.has(state)) {
109 pending = pendingAuths.get(state)!
110 pendingKey = state
111 } else if (!state && pendingAuths.size === 1) {
112 // No state parameter but only one pending auth - use it
113 const [key, value] = pendingAuths.entries().next().value as [string, PendingAuth]
114 pending = value
115 pendingKey = key
116 log.info("no state parameter, using single pending auth", { key })
117 }
118
119 if (!pending || !pendingKey) {
120 const errorMsg = !state
121 ? "No state parameter provided and multiple pending authorizations"
122 : "Unknown or expired authorization request"
123 return new Response(HTML_ERROR(errorMsg), {
124 status: 400,
125 headers: { "Content-Type": "text/html" },
126 })
127 }

Callers

nothing calls this directly

Calls 4

HTML_ERRORFunction · 0.85
getMethod · 0.45
deleteMethod · 0.45
nextMethod · 0.45

Tested by

no test coverage detected