All files / lib/icloud/mfa mfa-method.ts

100% Statements 262/262
87.5% Branches 56/64
100% Functions 14/14
100% Lines 262/262

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 2621x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 1x 1x 202x 202x 202x 202x 202x 202x 202x 202x 202x 202x 202x 202x 202x 202x 202x 202x 202x 202x 209x 209x 14x 14x 14x 209x 14x 14x 14x 209x 209x 181x 181x 181x 209x 209x 202x 202x 202x 202x 202x 202x 1x 1x 202x 202x 202x 202x 202x 202x 3x 3x 202x 202x 202x 202x 202x 202x 2x 2x 202x 202x 202x 202x 202x 202x 44x 44x 16x 44x 16x 44x 44x 12x 44x 44x 202x 202x 202x 202x 202x 202x 9x 9x 9x 6x 9x 9x 3x 9x 9x 202x 202x 202x 202x 202x 202x 9x 9x 3x 3x 3x 3x 3x 3x 9x 3x 3x 3x 3x 3x 3x 9x 9x 3x 9x 9x 202x 202x 202x 202x 202x 202x 202x 6x 6x 6x 6x 4x 6x 6x 2x 6x 6x 202x 202x 202x 202x 202x 202x 202x 22x 9x 9x 9x 13x 16x 3x 3x 3x 10x 16x 7x 3x 3x 3x 4x 7x 4x 4x 4x 4x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4x 7x 3x 3x 3x 3x 3x 202x 202x 202x 202x 202x 202x 9x 9x 3x 3x 3x 3x 3x 3x 3x 3x 3x 9x 3x 3x 3x 3x 3x 3x 3x 3x 3x 9x 9x 3x 3x 3x 3x 3x 9x 9x 202x 202x 202x 202x 202x 202x 9x 9x 9x 6x 9x 9x 3x 9x 9x 202x 202x 202x 202x 202x 202x 202x 6x 6x 6x 6x 4x 6x 6x 2x 6x 6x 202x
import {AxiosResponse} from 'axios';
import {MFA_ERR} from '../../../app/error/error-codes.js';
import {iCPSError} from '../../../app/error/error.js';
import * as ICLOUD from '../constants.js';
 
/**
 * Indicating, which MFA method should be used
 */
export enum MFAMethodType {
    DEVICE = 1,
    SMS = 2,
    VOICE = 3
}
 
export class MFAMethod {
    type: MFAMethodType;
    numberId: number;
 
    /**
     * Creates a new MFAMethod object to hold status information
     * @param mfaMethod - The method to be used. Defaults to `device`
     * @param numberId - The number id used for sending sms or voice codes. Defaults to 1
     */
    constructor(mfaMethod: `device` | `voice` | `sms` = `device`, numberId: number = 1) {
        this.update(mfaMethod, numberId);
    }
 
    /**
     * Updates this object to the given method and number id
     * @param mfaMethod - The method to be used. Defaults to `device`
     * @param numberId - The number id used for sending sms or voice codes. Defaults to 1
     */
    update(mfaMethod: `device` | `voice` | `sms` | string = `device`, numberId: number = 1) {
        switch (mfaMethod) {
        case `sms`:
            this.type = MFAMethodType.SMS;
            this.numberId = numberId;
            break;
        case `voice`:
            this.type = MFAMethodType.VOICE;
            this.numberId = numberId;
            break;
        default:
        case `device`:
            this.type = MFAMethodType.DEVICE;
            this.numberId = undefined;
            break;
        }
    }
 
    /**
     *
     * @returns True, if the 'device' method is active
     */
    isDevice(): boolean {
        return this.type === MFAMethodType.DEVICE;
    }
 
    /**
     *
     * @returns True, if the 'sms' method is active
     */
    isSMS(): boolean {
        return this.type === MFAMethodType.SMS;
    }
 
    /**
     *
     * @returns True, if the 'voice' method is active
     */
    isVoice(): boolean {
        return this.type === MFAMethodType.VOICE;
    }
 
    /**
     *
     * @returns A string representation of this object
     */
    toString(): string {
        switch (this.type) {
        case MFAMethodType.SMS:
            return `'SMS' (Number ID: ${this.numberId})`;
        case MFAMethodType.VOICE:
            return `'Voice' (Number ID: ${this.numberId})`;
        default:
        case MFAMethodType.DEVICE:
            return `'Device'`;
        }
    }
 
    /**
     *
     * @returns The appropriate URL endpoint for resending the code, given the currently selected MFA Method
     */
    getResendURL(): string {
        switch (this.type) {
        case MFAMethodType.VOICE:
        case MFAMethodType.SMS:
            return ICLOUD.URL.MFA_PHONE;
        default:
        case MFAMethodType.DEVICE:
            return ICLOUD.URL.MFA_DEVICE;
        }
    }
 
    /**
     *
     * @returns The appropriate data for resending the code, given the currently selected MFA Method
     */
    getResendPayload(): any {
        switch (this.type) {
        case MFAMethodType.VOICE:
            return {
                "phoneNumber": {
                    "id": this.numberId,
                },
                "mode": `voice`,
            };
        case MFAMethodType.SMS:
            return {
                "phoneNumber": {
                    "id": this.numberId,
                },
                "mode": `sms`,
            };
        default:
        case MFAMethodType.DEVICE:
            return undefined;
        }
    }
 
    /**
     *
     * @param res - The response received from the backend
     * @returns True, if the response was successful, based on the currently selected MFA Method
     */
    resendSuccessful(res: AxiosResponse<any, any>) {
        const {status} = res;
        switch (this.type) {
        case MFAMethodType.VOICE:
        case MFAMethodType.SMS:
            return status === 200;
        default:
        case MFAMethodType.DEVICE:
            return status === 202;
        }
    }
 
    /**
     * Will take the Axios Error returned from the backend, and provide a user-readable string
     * @param err - The error returned
     * @returns An iCPSError indicating the underlying issue
     */
    processResendError(err: any): iCPSError {
        if (err.name !== `AxiosError` || !err.response) {
            return new iCPSError(MFA_ERR.NO_RESPONSE)
                .addCause(err);
        }
 
        if (err.response.status === 403) {
            return new iCPSError(MFA_ERR.TIMEOUT)
                .addCause(err);
        }
 
        if (err.response.status === 412) {
            if (!err.response.data) {
                return new iCPSError(MFA_ERR.PRECONDITION_FAILED)
                    .addCause(err);
            }
 
            if (this.type === MFAMethodType.SMS || this.type === MFAMethodType.VOICE) {
                const trustedPhones = (err.response.data as any).trustedPhoneNumbers;
                if (!trustedPhones
                    || !Array.isArray(trustedPhones)
                    || trustedPhones.length === 0) {
                    return new iCPSError(MFA_ERR.NO_TRUSTED_NUMBERS)
                        .addContext(`response.data`, err.response.data)
                        .addCause(err);
                }
 
                if (!trustedPhones.some(number => number.id === this.numberId)) {
                    return new iCPSError(MFA_ERR.TRUSTED_NUMBER_NOT_AVAILABLE)
                        .addMessage(`available numbers:\n${trustedPhones.map(number => `- ${number.id}: ${number.numberWithDialCode}`).join(`\n`)}`)
                        .addContext(`response.data`, err.response.data)
                        .addCause(err);
                }
            }
        }
 
        return new iCPSError(MFA_ERR.UNKNOWN_RESEND_ERROR)
            .addMessage(`method ${this}`)
            .addCause(err);
    }
 
    /**
     * @param mfa - The MFA code, that should be send for validation
     * @returns The appropriate payload for entering the code, given the currently selected MFA Method
     */
    getEnterPayload(mfa: string): any {
        switch (this.type) {
        case MFAMethodType.VOICE:
            return {
                "securityCode": {
                    "code": `${mfa}`,
                },
                "phoneNumber": {
                    "id": this.numberId,
                },
                "mode": `voice`,
            };
        case MFAMethodType.SMS:
            return {
                "securityCode": {
                    "code": `${mfa}`,
                },
                "phoneNumber": {
                    "id": this.numberId,
                },
                "mode": `sms`,
            };
        default:
        case MFAMethodType.DEVICE:
            return {
                "securityCode": {
                    "code": `${mfa}`,
                },
            };
        }
    }
 
    /**
     *
     * @returns The appropriate URL endpoint for entering the code, given the currently selected MFA Method
     */
    getEnterURL(): string {
        switch (this.type) {
        case MFAMethodType.VOICE:
        case MFAMethodType.SMS:
            return ICLOUD.URL.MFA_PHONE_ENTER;
        default:
        case MFAMethodType.DEVICE:
            return ICLOUD.URL.MFA_DEVICE_ENTER;
        }
    }
 
    /**
     *
     * @param res - The response received from the backend
     * @returns True, if the response was successful, based on the currently selected MFA Method
     */
    enterSuccessful(res: AxiosResponse<any, any>) {
        const {status} = res;
        switch (this.type) {
        case MFAMethodType.VOICE:
        case MFAMethodType.SMS:
            return status === 200;
        default:
        case MFAMethodType.DEVICE:
            return status === 204;
        }
    }
}