All files / src/tools/auth0/handlers prompts.ts

94.44% Statements 51/54
73.07% Branches 19/26
100% Functions 21/21
100% Lines 45/45

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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 2881x 1x 1x   1x                                                           1x                                                                                                                         1x                               43x         1075x         60200x                                                                                 1x       134x             2x       6x   6x   6x             6x     6x 5x     6x       175x 7x   175x           175x 3x                   6x   175x     3x   3x   3x                     2x   2x   2x   2x 2x     2x   2x 2x             2x   1x     2x   2x   2x 3x 3x               3x                  
import DefaultHandler from './default';
import { Assets, Language, languages } from '../../../types';
import { isEmpty } from 'lodash';
 
const promptTypes = [
  'login',
  'login-id',
  'login-password',
  'login-email-verification',
  'signup',
  'signup-id',
  'signup-password',
  'reset-password',
  'consent',
  'mfa-push',
  'mfa-otp',
  'mfa-voice',
  'mfa-phone',
  'mfa-webauthn',
  'mfa-sms',
  'mfa-email',
  'mfa-recovery-code',
  'mfa',
  'status',
  'device-flow',
  'email-verification',
  'email-otp-challenge',
  'organizations',
  'invitation',
  'common',
] as const;
 
export type PromptTypes = typeof promptTypes[number];
 
const screenTypes = [
  'login',
  'login-id',
  'login-password',
  'login-email-verification',
  'signup',
  'signup-id',
  'signup-password',
  'reset-password-request',
  'reset-password-email',
  'reset-password',
  'reset-password-success',
  'reset-password-error',
  'consent',
  'status',
  'mfa-detect-browser-capabilities',
  'mfa-enroll-result',
  'mfa-login-options',
  'mfa-begin-enroll-options',
  'mfa-otp-enrollment-qr',
  'mfa-otp-enrollment-code',
  'mfa-otp-challenge',
  'mfa-voice-challenge',
  'mfa-sms-challenge',
  'mfa-recovery-code-enrollment',
  'mfa-recovery-code-challenge',
  'mfa-country-codes',
  'mfa-sms-enrollment',
  'mfa-voice-enrollment',
  'mfa-phone-challenge',
  'mfa-phone-enrollment',
  'mfa-webauthn-roaming-enrollment',
  'mfa-webauthn-platform-enrollment',
  'mfa-webauthn-platform-challenge',
  'mfa-webauthn-roaming-challenge',
  'mfa-webauthn-change-key-nickname',
  'mfa-webauthn-enrollment-success',
  'mfa-webauthn-error',
  'mfa-webauthn-not-available-error',
  'mfa-sms-list',
  'mfa-email-challenge',
  'mfa-email-list',
  'mfa-push-welcome',
  'mfa-push-list',
  'mfa-push-enrollment-qr',
  'mfa-push-enrollment-code',
  'mfa-push-success',
  'mfa-push-challenge-push',
  'device-code-activation',
  'device-code-activation-allowed',
  'device-code-activation-denied',
  'device-code-confirmation',
  'email-verification-result',
  'email-otp-challenge',
  'redeem-ticket',
  'organization-selection',
  'accept-invitation',
] as const;
 
export type ScreenTypes = typeof screenTypes[number];
 
export const schema = {
  type: 'object',
  properties: {
    universal_login_experience: {
      type: 'string',
      enum: ['new', 'classic'],
    },
    webauthn_platform_first_factor: {
      type: 'boolean',
    },
    identifier_first: {
      type: 'boolean',
    },
    customText: {
      type: 'object',
      properties: languages.reduce((acc, language) => {
        return {
          ...acc,
          [language]: {
            type: 'object',
            properties: promptTypes.reduce((acc, promptTypes) => {
              return {
                ...acc,
                [promptTypes]: {
                  type: 'object',
                  properties: screenTypes.reduce((acc, screenTypes) => {
                    return {
                      ...acc,
                      [screenTypes]: {
                        type: 'object',
                      },
                    };
                  }, {}),
                },
              };
            }, {}),
          },
        };
      }, {}),
    },
  },
};
 
export type PromptSettings = {
  universal_login_experience?: 'new' | 'classic';
  webauthn_platform_first_factor?: boolean;
  identifier_first?: boolean;
};
 
export type PromptsCustomText = {
  [key in PromptTypes]: Partial<{
    [key in ScreenTypes]: {
      [key: string]: string;
    };
  }>;
};
 
export type Prompts = Partial<
  PromptSettings & {
    customText: AllPromptsByLanguage;
  }
>;
 
export type AllPromptsByLanguage = Partial<{
  [key in Language]: Partial<PromptsCustomText>;
}>;
 
export default class PromptsHandler extends DefaultHandler {
  existing: Prompts;
 
  constructor(options: DefaultHandler) {
    super({
      ...options,
      type: 'prompts',
    });
  }
 
  objString({ customText }: Prompts): string {
    return `Prompts settings${!!customText ? ' and prompts custom text' : ''}`;
  }
 
  async getType(): Promise<Prompts | null> {
    const promptsSettings = await this.client.prompts.getSettings();
 
    const customText = await this.getCustomTextSettings();
 
    return {
      ...promptsSettings,
      customText,
    };
  }
 
  async getCustomTextSettings(): Promise<AllPromptsByLanguage> {
    const supportedLanguages = await this.client.tenant
      .getSettings()
      .then(({ enabled_locales }) => {
        if (enabled_locales === undefined) return []; // In rare cases, private cloud tenants may not have `enabled_locales` defined
        return enabled_locales;
      });
 
    return this.client.pool
      .addEachTask({
        data:
          supportedLanguages
            .map((language) => promptTypes.map((promptType) => ({ promptType, language })))
            .reduce((acc, val) => acc.concat(val), []) || [],
        generator: ({ promptType, language }) =>
          this.client.prompts
            .getCustomTextByLanguage({
              prompt: promptType,
              language,
            })
            .then((customTextData) => {
              if (isEmpty(customTextData)) return null;
              return {
                language,
                [promptType]: {
                  ...customTextData,
                },
              };
            }),
      })
      .promise()
      .then((customTextData) => {
        return customTextData
          .filter((customTextData) => {
            return customTextData !== null;
          })
          .reduce((acc: AllPromptsByLanguage, customTextItem) => {
            Iif (customTextItem?.language === undefined) return acc;
 
            const { language, ...customTextSettings } = customTextItem;
 
            return {
              ...acc,
              [language]: !!acc[language]
                ? { ...acc[language], ...customTextSettings }
                : { ...customTextSettings },
            };
          }, {});
      });
  }
 
  async processChanges(assets: Assets): Promise<void> {
    const { prompts } = assets;
 
    Iif (!prompts) return;
 
    const { customText, ...promptSettings } = prompts;
 
    Eif (!isEmpty(promptSettings)) {
      await this.client.prompts.updateSettings({}, promptSettings);
    }
 
    await this.updateCustomTextSettings(customText);
 
    this.updated += 1;
    this.didUpdate(prompts);
  }
 
  async updateCustomTextSettings(customText: Prompts['customText']): Promise<void> {
    /*
      Note: deletes are not currently supported
    */
    if (!customText) return;
 
    await this.client.pool
      .addEachTask({
        data: Object.keys(customText).flatMap((language: Language) => {
          const languageScreenTypes = customText[language];
 
          Iif (!languageScreenTypes) return [];
 
          return Object.keys(languageScreenTypes).map((prompt: PromptTypes) => {
            const body = languageScreenTypes[prompt] || {};
            return {
              body,
              language,
              prompt,
            };
          });
        }),
        generator: ({ prompt, language, body }) =>
          this.client.prompts.updateCustomTextByLanguage({
            prompt,
            language,
            body,
          }),
      })
      .promise();
  }
}