UNPKG

@tableau/taco-toolkit

Version:
101 lines (100 loc) 3.36 kB
import { SecretsType } from './types/secrets-type'; /** * Represents an authentication header for making authorized HTTP requests. * * @typedef {Object} AuthHeader * @property {string | undefined} Authorization - The authorization token to include in the request header. * * @example * ```ts * const authHeader: AuthHeader = { * Authorization: 'Bearer <token>', * } * * // Usage in an HTTP request * const requestOptions: RequestOptions = { * headers: authHeader, * // other request options... * } * ``` */ export type AuthHeader = { Authorization?: string; }; declare enum AUTH_TOKENS { accessToken = "access-token" } /** * Represents the oAuth secret that includes only access-token */ type oAuthSecrets = { [AUTH_TOKENS.accessToken]: string | undefined; } | undefined; /** * Extracts OAuth secrets from the provided headers. * * @param headers - The headers containing OAuth secrets inclding access-token. * @returns The extracted OAuth secrets. */ declare function extractOAuthSecretsFromHeader(headers: SecretsType | undefined): oAuthSecrets; /** * Generates an authentication header using the provided token and optional prefix. * * @param token - The token used for authentication. * @param prefix - The prefix to be added before the token (default is 'Bearer'). * @returns The authentication header. * @example * ``` * //Generating a custom prefix token header * const token = 'your-custom-token'; * const customPrefix = 'Custom'; * const header = getAuthHeader(token, customPrefix); * // header will be: { Authorization: 'Custom your-custom-token' } *``` */ declare function getAuthHeader(token: string, prefix?: string): AuthHeader; /** * Generates a Basic authentication header using the provided username and password. * * @param username - The username for authentication. * @param password - The password for authentication. * @returns The Basic authentication header. * * @example * ``` * // Generating a Basic authentication header * const username = 'yourUsername'; * const password = 'yourPassword'; * const basicAuthHeader = getBasicAuthHeader(username, password); * console.log(basicAuthHeader); * // Output will be: { Authorization: 'Basic base64EncodedString' } * ``` */ declare function getBasicAuthHeader(username: string, password: string): AuthHeader; /** * Generates an OAuth authentication header based on the provided secrets. * If no access token is provided in the secrets, an empty object is returned. * * @param headers - The headers containing access-token. * @returns The authentication header. */ declare function getOAuthHeader(headers: SecretsType | undefined): AuthHeader; /** * Represents OAuth credentials. includes only access token */ declare class OAuthCredentials { readonly accessToken: string | undefined; /** * Create a new instance of OAuthCredentials. * * @param headers - The headers containing OAuth secrets. Only `access-token` will be used from the provided headers */ constructor(headers: SecretsType | undefined); /** * Get the authentication header based on the access token. * * @returns The authentication header. */ get authHeader(): AuthHeader; } export { OAuthCredentials, extractOAuthSecretsFromHeader, getAuthHeader, getBasicAuthHeader, getOAuthHeader };