|
| 1 | +import axios from 'axios'; |
| 2 | + |
| 3 | +import type { ToolsLogger } from '@sap-ux/logger'; |
| 4 | + |
| 5 | +import { t } from '../i18n'; |
| 6 | +import type { Uaa, BtpDestinationConfig } from '../types'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Obtain an OAuth2 access token using the client credentials grant. |
| 10 | + * |
| 11 | + * @param uaa - UAA service credentials (clientid, clientsecret, url). |
| 12 | + * @param logger - Optional logger. |
| 13 | + * @returns OAuth2 access token. |
| 14 | + */ |
| 15 | +export async function getToken(uaa: Uaa, logger?: ToolsLogger): Promise<string> { |
| 16 | + const auth = Buffer.from(`${uaa.clientid}:${uaa.clientsecret}`); |
| 17 | + const options = { |
| 18 | + headers: { |
| 19 | + 'Content-Type': 'application/x-www-form-urlencoded', |
| 20 | + 'Authorization': 'Basic ' + auth.toString('base64') |
| 21 | + } |
| 22 | + }; |
| 23 | + const uri = `${uaa.url}/oauth/token`; |
| 24 | + logger?.debug(`Requesting OAuth token from ${uri}`); |
| 25 | + try { |
| 26 | + const response = await axios.post(uri, 'grant_type=client_credentials', options); |
| 27 | + logger?.debug('OAuth token obtained successfully'); |
| 28 | + return response.data['access_token']; |
| 29 | + } catch (e) { |
| 30 | + logger?.error(`Failed to obtain OAuth token from ${uri}: ${e.message}`); |
| 31 | + throw new Error(t('error.failedToGetAuthKey', { error: e.message })); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * Get a single destination's configuration from the BTP Destination Configuration API. |
| 37 | + * Note: This calls the BTP Destination Configuration API, not the BAS listDestinations API. |
| 38 | + * |
| 39 | + * @param uri - Destination Configuration API base URI (e.g. https://destination-configuration.cfapps.us20.hana.ondemand.com). |
| 40 | + * @param token - OAuth2 bearer token obtained via {@link getToken}. |
| 41 | + * @param destinationName - Name of the destination to look up. |
| 42 | + * @param logger - Optional logger. |
| 43 | + * @returns The destinationConfiguration object (e.g. Name, ProxyType, URL, Authentication) or undefined on failure. |
| 44 | + */ |
| 45 | +export async function getBtpDestinationConfig( |
| 46 | + uri: string, |
| 47 | + token: string, |
| 48 | + destinationName: string, |
| 49 | + logger?: ToolsLogger |
| 50 | +): Promise<BtpDestinationConfig | undefined> { |
| 51 | + const url = `${uri}/destination-configuration/v1/destinations/${encodeURIComponent(destinationName)}`; |
| 52 | + logger?.debug(`Fetching BTP destination config for "${destinationName}" from ${url}`); |
| 53 | + |
| 54 | + try { |
| 55 | + const response = await axios.get<{ destinationConfiguration?: BtpDestinationConfig }>(url, { |
| 56 | + headers: { 'Authorization': `Bearer ${token}` } |
| 57 | + }); |
| 58 | + const config = response.data?.destinationConfiguration; |
| 59 | + logger?.debug(`Destination "${destinationName}" config: ProxyType=${config?.ProxyType}`); |
| 60 | + return config; |
| 61 | + } catch (e) { |
| 62 | + logger?.error(`Failed to fetch destination config for "${destinationName}": ${e.message}`); |
| 63 | + return undefined; |
| 64 | + } |
| 65 | +} |
0 commit comments