All files / src/shared getOptionsFromEnvironment.ts

81.81% Statements 18/22
29.41% Branches 5/17
100% Functions 4/4
80% Lines 16/20

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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 1621x 1x     1x                                         1x 298x       298x         298x                                   298x   298x                     298x                                                                     1x                                     298x   298x                                                 1x 298x   298x                      
import { KeysService } from '@affinidi/common'
import { resolveUrl, Service } from '@affinidi/url-resolver'
import { Env, SdkOptions } from '../dto/shared.dto'
 
import {
  DEV_COGNITO_CLIENT_ID,
  DEV_COGNITO_USER_POOL_ID,
  DEV_EMAIL_ISSUER_BASE_PATH,
  DEV_PHONE_ISSUER_BASE_PATH,
  PROD_COGNITO_CLIENT_ID,
  PROD_COGNITO_USER_POOL_ID,
  PROD_EMAIL_ISSUER_BASE_PATH,
  PROD_PHONE_ISSUER_BASE_PATH,
  STAGING_COGNITO_CLIENT_ID,
  STAGING_COGNITO_USER_POOL_ID,
  STAGING_EMAIL_ISSUER_BASE_PATH,
  STAGING_PHONE_ISSUER_BASE_PATH,
  DEFAULT_COGNITO_REGION,
} from '../_defaultConfig'
 
type AccessApiKeyOptions = {
  accessApiKey?: string
  apiKey?: string
}
 
export const getAccessApiKeyFromOptions = ({ accessApiKey, apiKey }: AccessApiKeyOptions) => {
  Iif (!accessApiKey && !apiKey) {
    throw new Error('Neither accessApiKey nor apiKey is contained in options')
  }
 
  Iif (apiKey) {
    const apiKeyBuffer = KeysService.sha256(Buffer.from(apiKey))
    return apiKeyBuffer.toString('hex')
  }
 
  return accessApiKey
}
 
type EnvironmentOptions = {
  env: Env
  issuerUrl?: string
  registryUrl?: string
  verifierUrl?: string
  affinidiVaultUrl?: string
  keyStorageUrl?: string
  phoneIssuerBasePath?: string
  emailIssuerBasePath?: string
  revocationUrl?: string
  userPoolId?: string
  clientId?: string
}
 
function getBasicOptionsFromEnvironment(options: EnvironmentOptions) {
  const env = options.env
 
  const urls = {
    issuerUrl: resolveUrl(Service.ISSUER, env, options.issuerUrl),
    registryUrl: resolveUrl(Service.REGISTRY, env, options.registryUrl),
    verifierUrl: resolveUrl(Service.VERIFIER, env, options.verifierUrl),
    affinidiVaultUrl: resolveUrl(Service.VAULT, env, options.affinidiVaultUrl),
    keyStorageUrl: resolveUrl(Service.KEY_STORAGE, env, options.keyStorageUrl),
    revocationUrl: resolveUrl(Service.REVOCATION, env, options.revocationUrl),
    metricsUrl: resolveUrl(Service.METRICS, env),
    migrationUrl: resolveUrl(Service.VAULT_MIGRATION, env),
  }
 
  switch (env) {
    /* istanbul ignore next */
    case 'dev':
      return {
        env: env as Env,
        clientId: options.clientId || DEV_COGNITO_CLIENT_ID,
        userPoolId: options.userPoolId || DEV_COGNITO_USER_POOL_ID,
        phoneIssuerBasePath: options.phoneIssuerBasePath || DEV_PHONE_ISSUER_BASE_PATH,
        emailIssuerBasePath: options.emailIssuerBasePath || DEV_EMAIL_ISSUER_BASE_PATH,
        ...urls,
      }
 
    /* istanbul ignore next */
    case 'prod':
      return {
        env: env as Env,
        clientId: options.clientId || PROD_COGNITO_CLIENT_ID,
        userPoolId: options.userPoolId || PROD_COGNITO_USER_POOL_ID,
        phoneIssuerBasePath: options.phoneIssuerBasePath || PROD_PHONE_ISSUER_BASE_PATH,
        emailIssuerBasePath: options.emailIssuerBasePath || PROD_EMAIL_ISSUER_BASE_PATH,
        ...urls,
      }
 
    case 'staging':
      return {
        env: env as Env,
        clientId: options.clientId || STAGING_COGNITO_CLIENT_ID,
        userPoolId: options.userPoolId || STAGING_COGNITO_USER_POOL_ID,
        phoneIssuerBasePath: options.phoneIssuerBasePath || STAGING_PHONE_ISSUER_BASE_PATH,
        emailIssuerBasePath: options.emailIssuerBasePath || STAGING_EMAIL_ISSUER_BASE_PATH,
        ...urls,
      }
  }
}
 
const splitOptions = <TOptions extends SdkOptions>(options: TOptions) => {
  const {
    accessApiKey,
    apiKey,
    env,
    issuerUrl,
    registryUrl,
    verifierUrl,
    affinidiVaultUrl,
    keyStorageUrl,
    phoneIssuerBasePath,
    emailIssuerBasePath,
    revocationUrl,
    storageRegion,
    clientId,
    userPoolId,
    region,
    origin,
    ...otherOptions
  } = options
 
  return {
    region,
    accessApiKeyOptions: {
      accessApiKey,
      apiKey,
    },
    environmentOptions: {
      env,
      issuerUrl,
      registryUrl,
      verifierUrl,
      affinidiVaultUrl,
      keyStorageUrl,
      phoneIssuerBasePath,
      emailIssuerBasePath,
      revocationUrl,
      clientId,
      userPoolId,
    },
    storageRegion,
    otherOptions,
    origin,
  }
}
 
export const getOptionsFromEnvironment = (options: SdkOptions) => {
  const { region, accessApiKeyOptions, environmentOptions, storageRegion, otherOptions, origin } = splitOptions(options)
 
  return {
    region: region || DEFAULT_COGNITO_REGION,
    basicOptions: getBasicOptionsFromEnvironment(environmentOptions),
    accessApiKey: getAccessApiKeyFromOptions(accessApiKeyOptions),
    storageRegion,
    otherOptions,
    origin,
  }
}
 
export type ParsedOptions = ReturnType<typeof getOptionsFromEnvironment>