import type { AxiosRequestConfig, CatalogService, ODataService, ProviderConfiguration, ServiceInfo, ServiceProvider } from '@sap-ux/axios-extension'; import { ODataVersion } from '@sap-ux/axios-extension'; import { type Destination } from '@sap-ux/btp-utils'; import { ERROR_TYPE } from '@sap-ux/inquirer-common'; import type { ConnectedSystem } from '../types.js'; import type { ValidationResult } from './types.js'; /** * Structure to store validity information about url to be validated. */ interface Validity { urlFormat?: boolean; reachable?: boolean; authRequired?: boolean; authenticated?: boolean; canSkipCertError?: boolean; } export type SystemAuthType = 'serviceKey' | 'reentranceTicket' | 'basic' | 'unknown'; export type ConnectionType = 'abap_catalog' | 'odata_path'; /** * Class that can be used to determine the connectivity using a service url, system url, or service info (UAA Key details) or reentrance ticket. * This will determine if if the service/catalog is reachable, authentication is required and generates ting messages to guide the user. * Certain types of certificate errors can be ignored if required. However, the end-user should be warned about the risks using a prompt message. * Catalog requests may be made multiple times for the same url, but the underlying connectivity module (@sap-ux/axios-extension) will cache the results to avoid repeated network requests. * The class also stores the current connection state, including the service provider, odata service, and catalog services. */ export declare class ConnectionValidator { readonly validity: Validity; private _validatedUrl; private _destinationUrl; private _destination; private _validatedClient; private _odataService; private _serviceProvider; private _axiosConfig; private _catalogV2; private _catalogV4; private _systemAuthType; private _serviceInfo; private _connectedUserName; private _connectedSystemName; private _refreshToken; private _ignoreCertError; private _connectType; /** * Get the ignoreCertError value. * * @returns the ignoreCertError value, true indicates that cert errors may have been ignored */ get ignoreCertError(): boolean | undefined; /** * Getter for the axios configuration. * * @returns the axios configuration */ get axiosConfig(): AxiosRequestConfig & ProviderConfiguration; /** * Get the odata service instance. * * @returns the odata service instance */ get odataService(): ODataService | undefined; /** * Get the catalogs for the odata versions. Note that one of these may not be defined where a specific odata version is required. * * @returns the catalog services for each the odata versions */ get catalogs(): Record; /** * * @returns the current connections service provider */ get serviceProvider(): ServiceProvider | undefined; /** * The auth type used to create an authenticated connection to the system. * * @returns the system auth type */ get systemAuthType(): SystemAuthType | undefined; /** * The auth type used to create an authenticated connection to the system. * * @param value the system auth type */ set systemAuthType(value: SystemAuthType); /** * Get the validated url. This is the url that has been successfully validated (not necessarily connected). Use validity to check if the url is reachable. * * @returns the validated url */ get validatedUrl(): string | undefined; /** * Get the validated client code. This is the client code that has been successfully validated by a request. * * @returns the validated client code */ get validatedClient(): string | undefined; /** * Get the service info used to connect to the system. * * @returns the service info */ get serviceInfo(): ServiceInfo | undefined; /** * Set the service info used to connect to the system. * * @param serviceInfo the service info */ set serviceInfo(serviceInfo: ServiceInfo); /** * Get the connected user name. * * @returns the connected user name */ get connectedUserName(): string | undefined; /** * Get the refresh token. * * @returns the refresh token */ get refreshToken(): string | undefined; /** * Get the full expanded destination url as defined in the destination configuration. This should not be used to connect from App Studio (use the .dest form). * * @returns the connected destination 'URL' attribute value */ get destinationUrl(): string | undefined; /** * Get the destination. * * @returns the connected destination 'URL' attribute value */ get destination(): Destination | undefined; /** * Get the connection type. * * @returns the connection type, either 'abap_catalog' or 'odata_path' */ get connectType(): ConnectionType; /** * Get the connected system name. If previously set this will be used, otherwise the name is determined * by the system auth type, or the validated url. * * @returns the connected system name */ get connectedSystemName(): string | undefined; /** * */ set connectedSystemName(value: string | undefined); /** * Setting an existing connected system will prevent re-authentication. * This should be used where the user has previously authenticated to prevent re-authentication (e.g. via browser) and the previous * connected system is still valid. Only Abap connected backend systems are currently supported for cached connection use. * For non-Abap backend systems, or basic auth connections, the user will be prompted to re-authenticate. * * @param connectedSystem A connected system object containing the service provider and backend system information. Not relevant for destination connections. * @param connectedSystem.serviceProvider * @param connectedSystem.backendSystem */ setConnectedSystem({ serviceProvider, backendSystem }: ConnectedSystem): void; /** * Calls a given service or system url to test its reachability and authentication requirements. * If the url is a system url, it will attempt to use the catalog service to get the service info. * * @param url a service url (:/) or a system url (:) * @param username optional username * @param password optional password * @param options options for the connection validation * @param options.ignoreCertError ignore some certificate errors * @param options.isSystem if true, the url will be treated as a system url rather than a service url * @param options.odataVersion if specified will restrict catalog requests to only the specified odata version * @param options.connectType if specified will determine how a system is authenticated, catalog or metadata request * @returns the status code or error returned by the connection attempt * @throws an error if the connection attempt fails and the error is a 500 on App Studio or a non-axios error is caught */ private checkUrl; /** * Create the axios configuration object for the service or system connection. * * @param url the service or system url * @param ignoreCertError if true the config will be set to ignore cert errors * @param username provided for basic authentication * @param password provided for basic authentication * @returns the axios configuration object */ private createAxiosConfig; /** * Create the connection for a service url. The base url should be provided with the axios config property. * * @param axiosConfig the axios request configuration * @param servicePath the service path without the origin */ private createOdataServiceConnection; /** * Resets any connection state and validity information. * * @param resetValidity if true, the validity information will be reset also */ resetConnectionState(resetValidity?: boolean): void; /** * When the env has set `NODE_TLS_REJECT_UNAUTHORIZED=0` we make an intial request to check for cert errors. * We log these errors to the console so the user is informed of the risks. * * @param axiosConfig the axios request configuration for Abap on prem connection * @param odataVersion the odata version to restrict the catalog requests if only a specific version is required * @returns true if the cert error should be ignored based on the environment setting `NODE_TLS_REJECT_UNAUTHORIZED=0` */ private shouldIgnoreCertError; /** * Create the connection for a system url, the specified axios config or the specified service info. * * @param connectConfig the connection configuration * @param connectConfig.axiosConfig the axios request configuration * @param connectConfig.url the system url * @param connectConfig.serviceInfo the service info * @param connectConfig.odataVersion the odata version to restrict the catalog requests if only a specific version is required * @param connectConfig.destination the destination to connect with * @param connectConfig.connectType Override the default connection approach if specified * @throws an error if the connection attempt fails, callers should handle the error */ private createSystemConnection; /** * Determine if a v4 catalog request should be made based on the specified error and if the error originated from a v2 or v4 catalog request. * If the error originated from a v2 catalog request and it is of type 401/403/404 then we try the v4 catalog. * If the v4 catalog request fails, for any reason, and the v2 catalog request failed with 401/403 we will throw that (401/403) error, * ultimately resulting in a basic auth prompt for the end-user. * Otherwise the v4 catalog request error is thrown, which may also be a 401/403, again resulting in a basic auth prompt and re-validation. * If both catalogs have returned 404 nothing else can be done, the error is thrown and reported. * * @param error - an error returned from either a v2 or v4 catalog listServices request. * @param v4Requested - has the v4 catalog been requested */ private handleCatalogError; /** * Callback for when the Abap cloud system refresh token changes. * * @param refreshToken the new refresh token */ private refreshTokenChangedCb; /** * Get the service provider for the Abap Cloud environment. * * @param url the system url * @param serviceInfo the service info * @param refreshToken * @returns the service provider */ private getAbapOnCloudServiceProvider; /** * Validate the system connectivity with the specified service info (containing UAA details). * This will create a connection to the system, updating the service provider reference. * The connected user name will be cached for later use. * * @param serviceInfo the service info containing the UAA details * @param odataVersion the odata version to restrict the catalog requests if only a specific version is required * @returns true if the system is reachable and authenticated, if required, false if not, or an error message string */ validateServiceInfo(serviceInfo: ServiceInfo, odataVersion?: ODataVersion): Promise; /** * Validate the specified destination connectivity, determining if authentication is required or if the destination is misconfigured. * * @param destination the destination to validate * @param odataVersion the odata version to restrict the catalog requests if only a specific version is required * @param servicePath the service path to validate, if specified will be appended to the destination URL for validation, if not specified the destination url will be used * @returns true if the system is reachable and authenticated, if required, false if not, or an error message string */ validateDestination(destination: Destination, odataVersion?: ODataVersion, servicePath?: string): Promise<{ valResult: ValidationResult; errorType?: ERROR_TYPE; }>; /** * Validate the connectivity to a destination odata service (for generic odata service destinations), determining, for example, if authentication is required. * * @param destination a destination that is a generic odata service destination * @param servicePath additional service path to append to the destination URL * @param requiredOdataVersion the required odata version, if specified will validate the odata service version satisfies the specified version * @returns validation result and error type if validation fails */ private validateOdataServiceDestination; /** * Validates a url format as well as its reachability. Note if this function returns true, this is only an indication that the url or host is reachable * not that a connection has been established. The connection may require separate authentication or other steps (it may be reachable but a cert error was returned). * * @param serviceUrl the url to validate, may be a system or service url. * @param options options for the connection validation * @param options.ignoreCertError ignore some certificate errors * @param options.forceReValidation force re-validation of the url * @param options.isSystem if true, the url will be treated as a system url rather than a service url, this value is retained for subsequent calls * @param options.odataVersion if specified will restrict catalog requests to only the specified odata version * @param options.systemAuthType the system auth type used to create system connections, if not specified or `isSystem` is false or undefined, `basic` is assumed * @param options.connectType if specified will be used to determine the endpoint used for authentication * @returns true if the url is reachable, false if not, or an error message string */ validateUrl(serviceUrl: string, { ignoreCertError, forceReValidation, isSystem, odataVersion, systemAuthType, connectType }?: { ignoreCertError?: boolean; forceReValidation?: boolean; isSystem?: boolean; odataVersion?: ODataVersion; systemAuthType?: SystemAuthType; connectType?: ConnectionType; }): Promise; /** * Sets the instance validity state based on the status code and returns true if the status code indicates that the URL was reachable. * * @param status a http request status code used to determine the validation result * @returns true, if the status code indicates the url is reachable, false if not, or an error message string */ private getValidationResultFromStatusCode; /** * Is a string nil or whitespace only. * * @param url the string to test * @returns true if the string is nil or whitespace */ private isEmptyString; /** * Tests if the url has already been validated. * * @param url the full url to test for previous validation * @returns true if the url has already been validated */ private isUrlValidated; /** * Checks if the current connection requires basic authentication * * @returns true if basic auth is required, false if not */ isAuthRequired(): Promise; /** * Test the connectivity with the specified service url using the provided credentials (basic authentication). * * @param url the url to validate * @param username user name * @param password password * @param options options for the connection authentication validation * @param options.ignoreCertError ignore some certificate errors * @param options.isSystem if true, the url will be treated as a system url rather than a service url * @param options.sapClient the sap client to use for the connection * @param options.odataVersion if specified will restrict catalog requests to only the specified odata version * @param options.connectType if specified this will determine how a system connection is authenticated, catalog or service metadata, overiding the default approach * @returns true if the authentication is successful, false if not, or an error message string */ validateAuth(url: string, username?: string, password?: string, { ignoreCertError, sapClient, odataVersion, isSystem, connectType }?: { ignoreCertError?: boolean; odataVersion?: ODataVersion; sapClient?: string; isSystem?: boolean; connectType?: ConnectionType; }): Promise<{ valResult: ValidationResult; errorType?: ERROR_TYPE; }>; /** * Reset the validity state. */ private resetValidity; /** * Get the specific authorization reason user message. This may be catalog version specific. * * @param httpStatusCode the http error code returned from a request * @param odataVersion optional, the odata version specific request, for example for specific catalog requests * @returns the text message indicting the authentication or authorization failure speific to an odata catalog request */ private getAuthFailureReasonText; } export {}; //# sourceMappingURL=connectionValidator.d.ts.map