/ services / oauth / getOauthProfile.ts
getOauthProfile.ts
 1  import axios from 'axios'
 2  import { getOauthConfig, OAUTH_BETA_HEADER } from 'src/constants/oauth.js'
 3  import type { OAuthProfileResponse } from 'src/services/oauth/types.js'
 4  import { getAnthropicApiKey } from 'src/utils/auth.js'
 5  import { getGlobalConfig } from 'src/utils/config.js'
 6  import { logError } from 'src/utils/log.js'
 7  export async function getOauthProfileFromApiKey(): Promise<
 8    OAuthProfileResponse | undefined
 9  > {
10    // Assumes interactive session
11    const config = getGlobalConfig()
12    const accountUuid = config.oauthAccount?.accountUuid
13    const apiKey = getAnthropicApiKey()
14  
15    // Need both account UUID and API key to check
16    if (!accountUuid || !apiKey) {
17      return
18    }
19    const endpoint = `${getOauthConfig().BASE_API_URL}/api/claude_cli_profile`
20    try {
21      const response = await axios.get<OAuthProfileResponse>(endpoint, {
22        headers: {
23          'x-api-key': apiKey,
24          'anthropic-beta': OAUTH_BETA_HEADER,
25        },
26        params: {
27          account_uuid: accountUuid,
28        },
29        timeout: 10000,
30      })
31      return response.data
32    } catch (error) {
33      logError(error as Error)
34    }
35  }
36  
37  export async function getOauthProfileFromOauthToken(
38    accessToken: string,
39  ): Promise<OAuthProfileResponse | undefined> {
40    const endpoint = `${getOauthConfig().BASE_API_URL}/api/oauth/profile`
41    try {
42      const response = await axios.get<OAuthProfileResponse>(endpoint, {
43        headers: {
44          Authorization: `Bearer ${accessToken}`,
45          'Content-Type': 'application/json',
46        },
47        timeout: 10000,
48      })
49      return response.data
50    } catch (error) {
51      logError(error as Error)
52    }
53  }