({
client_id,
client_secret,
redirect_uri,
code_verifier,
code,
}: {
client_id: string
client_secret: string
redirect_uri: string
code_verifier: string
code: string
})
| 163 | * @returns {Promise<[string, null] | [null, Response]>} A promise that resolves to an array containing the access token or an error response. |
| 164 | */ |
| 165 | export async function getAuthToken({ |
| 166 | client_id, |
| 167 | client_secret, |
| 168 | redirect_uri, |
| 169 | code_verifier, |
| 170 | code, |
| 171 | }: { |
| 172 | client_id: string |
| 173 | client_secret: string |
| 174 | redirect_uri: string |
| 175 | code_verifier: string |
| 176 | code: string |
| 177 | }): Promise<AuthorizationToken> { |
| 178 | if (!code) { |
| 179 | throw new McpError('Missing code', 400) |
| 180 | } |
| 181 | |
| 182 | const params = new URLSearchParams({ |
| 183 | grant_type: 'authorization_code', |
| 184 | client_id, |
| 185 | redirect_uri, |
| 186 | code, |
| 187 | code_verifier, |
| 188 | }).toString() |
| 189 | const resp = await fetch('https://dash.cloudflare.com/oauth2/token', { |
| 190 | method: 'POST', |
| 191 | headers: { |
| 192 | Authorization: `Basic ${btoa(`${client_id}:${client_secret}`)}`, |
| 193 | 'Content-Type': 'application/x-www-form-urlencoded', |
| 194 | }, |
| 195 | body: params, |
| 196 | }) |
| 197 | |
| 198 | if (!resp.ok) { |
| 199 | throwUpstreamTokenError(resp.status, await resp.text(), 'Token exchange failed') |
| 200 | } |
| 201 | |
| 202 | return AuthorizationToken.parse(await resp.json()) |
| 203 | } |
| 204 | |
| 205 | export async function refreshAuthToken({ |
| 206 | client_id, |
no test coverage detected