( authorizationCode: string, state: string, codeVerifier: string, port: number, useManualRedirect: boolean = false, expiresIn?: number, )
| 105 | } |
| 106 | |
| 107 | export async function exchangeCodeForTokens( |
| 108 | authorizationCode: string, |
| 109 | state: string, |
| 110 | codeVerifier: string, |
| 111 | port: number, |
| 112 | useManualRedirect: boolean = false, |
| 113 | expiresIn?: number, |
| 114 | ): Promise<OAuthTokenExchangeResponse> { |
| 115 | const requestBody: Record<string, string | number> = { |
| 116 | grant_type: 'authorization_code', |
| 117 | code: authorizationCode, |
| 118 | redirect_uri: useManualRedirect |
| 119 | ? getOauthConfig().MANUAL_REDIRECT_URL |
| 120 | : `http://localhost:${port}/callback`, |
| 121 | client_id: getOauthConfig().CLIENT_ID, |
| 122 | code_verifier: codeVerifier, |
| 123 | state, |
| 124 | } |
| 125 | |
| 126 | if (expiresIn !== undefined) { |
| 127 | requestBody.expires_in = expiresIn |
| 128 | } |
| 129 | |
| 130 | const response = await axios.post(getOauthConfig().TOKEN_URL, requestBody, { |
| 131 | headers: { 'Content-Type': 'application/json' }, |
| 132 | timeout: 15000, |
| 133 | }) |
| 134 | |
| 135 | if (response.status !== 200) { |
| 136 | throw new Error( |
| 137 | response.status === 401 |
| 138 | ? 'Authentication failed: Invalid authorization code' |
| 139 | : `Token exchange failed (${response.status}): ${response.statusText}`, |
| 140 | ) |
| 141 | } |
| 142 | logEvent('tengu_oauth_token_exchange_success', {}) |
| 143 | return response.data |
| 144 | } |
| 145 | |
| 146 | export async function refreshOAuthToken( |
| 147 | refreshToken: string, |
nothing calls this directly
no test coverage detected