import { AuthorizationConfig } from '@wirewheel/cmp/src/entities/AuthorizationConfig' import axios from 'axios' import { ClientConfig } from '@wirewheel/cmp/src/entities/ClientConfig' import { WireWheelClient } from '@wirewheel/cmp/src/WireWheelClient' import { ValidationError } from '@wirewheel/cmp/src/errors/ValidationError' import { convertToBase64 } from '@wirewheel/cmp/src/utils/convertToBase64' import { PrivacyProtocolsClient } from '@wirewheel/cmp/src/PrivacyProtocolsClient' import { PrivacyProtocolsConfig } from '@wirewheel/cmp/src/entities/PrivacyProtocolsConfig' import { EmbeddedConfig } from '@wirewheel/cmp/src/entities/EmbeddedConfig' import { EmbeddedHandler } from '@wirewheel/cmp/src/EmbeddedHandler' import { isChildWindow } from '@wirewheel/cmp/src/utils/isChildWindow' import { TokenCache } from '@wirewheel/cmp/src/utils/tokenCache' import { ChildEmbeddedConfig } from '@wirewheel/cmp/src/entities/ChildEmbeddedConfig' /* eslint-disable no-unused-vars */ // eslint does not understand this is exported and complains it is unused export enum Scopes { ConsentWrite = 'consent-insert-api', ConsentRead = 'consent-get-api', SubjectProfileManage = 'subject-profile-manage', } /* eslint-enable no-unused-vars */ export class WireWheelSDK { private static tokenCache = new TokenCache() /** * This method retrieves an access token with authorization to execute the functions of the {@link WireWheelClient} * @param config * @param scopes */ static async getToken(config: AuthorizationConfig, scopes: Scopes[]): Promise { if (scopes.length == 0) { throw new ValidationError('Must provide at least one scope type.') } scopes.sort() const token = this.tokenCache.get({ scopes, clientId: config.clientId }) if (token) return token const consentContextToken = convertToBase64(`${config.clientId}:${config.clientSecret}`) const response = await axios({ method: 'post', url: config.issuer, headers: { authorization: `Basic ${consentContextToken}`, 'Content-Type': 'application/x-www-form-urlencoded', }, data: `grant_type=client_credentials&scope=${scopes .map((scope) => `${config.resourceServerName}/${scope}`) .join(' ')}`, }) const { data } = response this.tokenCache.set({ scopes, clientId: config.clientId, expiresIn: data.expires_in }, data.access_token) return data.access_token } /** createClient is a factory method for the instantiation of {@link WireWheelClient} */ static createClient(config: ClientConfig, embeddedHandler?: EmbeddedHandler): WireWheelClient { return WireWheelClient.new(config, embeddedHandler) } static async registerPrivacyProtocols(config: PrivacyProtocolsConfig): Promise { await new PrivacyProtocolsClient(config).registerPrivacyProtocols() } static initEmbeddedParent(config: EmbeddedConfig): EmbeddedHandler { const { targetIframe, cookieConfig } = config if (!targetIframe) { throw new ValidationError('Target iframe invalid.') } return EmbeddedHandler.createFromParent(targetIframe, cookieConfig) } static initEmbeddedChild(config?: ChildEmbeddedConfig): EmbeddedHandler | undefined { if (!isChildWindow()) { return undefined } return EmbeddedHandler.createFromChild(config) } }