All files / src WireWheelSDK.ts

80% Statements 28/35
66.66% Branches 4/6
71.42% Functions 5/7
79.41% Lines 27/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83  2x   2x 2x 2x 2x     2x 2x 2x         2x 2x 2x 2x       2x 2x               10x 1x   9x 9x 9x   8x 8x               12x     7x   7x 7x         16x       5x                                      
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<string> {
    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<void> {
    await new PrivacyProtocolsClient(config).registerPrivacyProtocols()
  }
 
  static initEmbeddedParent(config: EmbeddedConfig): EmbeddedHandler {
    const { targetIframe, cookieConfig } = config
    Iif (!targetIframe) {
      throw new ValidationError('Target iframe invalid.')
    }
    return EmbeddedHandler.createFromParent(targetIframe, cookieConfig)
  }
 
  static initEmbeddedChild(config?: ChildEmbeddedConfig): EmbeddedHandler | undefined {
    Iif (!isChildWindow()) {
      return undefined
    }
 
    return EmbeddedHandler.createFromChild(config)
  }
}