import { AAD_OAUTH, ADFS_OAUTH, AuthFlowType, CDF_OAUTH, CUSTOM, HttpError, OIDC_AUTHORIZATION_CODE_FLOW, } from "@cognite/sdk-core"; import { CogniteClient } from "@cognite/sdk"; import { rethrowAsHttpError } from "./errorHandlingUtils"; export interface ClientLoginOptions { appId: string; baseUrl?: string; } export interface ClientOptions { appId: string; baseUrl?: string; } export declare type WellsAuthFlowType = | AAD_OAUTH | CDF_OAUTH | ADFS_OAUTH | OIDC_AUTHORIZATION_CODE_FLOW | CUSTOM; export declare type RefreshMethod = (args?: any) => Promise; export interface CogniteProject { /** * Cognite project to login into */ project: string; //cluster: Cluster; } export interface ApiKeyLogin extends CogniteProject { /** * A Cognite issued api-key */ apiKey: string; } export interface TokenLogin extends CogniteProject { /** * authentication alternative 1. * Provide optional cached access token to skip the authentication flow (client.authenticate will still override this). */ accessToken?: string; /** * authentication alternative 2. * When the token expires, this custom method will be called to provide a new token */ tokenRefreshCallback?: RefreshMethod; /** * authentication alternative 3. * When the token expires, this custom method will be called to provide a new token */ authenticationFlow?: WellsAuthFlowType; } /** @hidden */ export interface Tokens { accessToken: string; idToken: string; } export type WithTokens = (tokens: Tokens) => void; /** @hidden */ export function bearerTokenString(token: string) { return `Bearer ${token}`; } /** @hidden */ export function isUsingBrowser() { return ( typeof window !== "undefined" && typeof window.document !== "undefined" ); } /** @hidden */ export function isUsingSSL() { return isUsingBrowser() && location.protocol.toLowerCase() === "https:"; } export function accessWellsApi(api: T | undefined): T { if (api === undefined) { throw Error( "Need to login with either loginWithApiKey or loginWithToken before you can use the Cognite Wells SDK" ); } return api; } export const getTokenWithCogniteClient = async ( flow: WellsAuthFlowType ): Promise => { const client = new CogniteClient({ appId: "wells client token" }); await client.loginWithOAuth(flow as AuthFlowType); const token: string | null = await client .getCDFToken() .then((response) => response) .catch((err) => { throw rethrowAsHttpError(err); }); if (token == null) throw new HttpError(401, "Regenerated token was 'null'", {}); return token; };