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

100% Statements 240/240
84.31% Branches 43/51
100% Functions 14/14
100% Lines 240/240

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 2401x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 272x 272x 272x 272x 272x 272x 272x 272x 1x 1x 1x 1x 1x 1x 1x 1x 272x 272x 1x 1x 1x 1x 1x 1x 1x 1x 286x 286x 38x 38x 38x 38x 286x 34x 34x 34x 34x 286x 286x 214x 214x 214x 214x 286x 286x 1x 1x 1x 1x 1x 1x 8x 8x 1x 1x 1x 1x 1x 1x 22x 22x 1x 1x 1x 1x 1x 1x 14x 14x 1x 1x 1x 1x 1x 1x 44x 44x 18x 44x 15x 44x 44x 11x 44x 44x 1x 1x 1x 1x 1x 1x 22x 22x 22x 16x 22x 22x 6x 22x 22x 1x 1x 1x 1x 1x 1x 23x 23x 8x 8x 8x 8x 23x 9x 9x 9x 9x 23x 23x 6x 23x 23x 1x 1x 1x 1x 1x 1x 1x 22x 22x 22x 16x 22x 22x 6x 22x 22x 1x 1x 1x 1x 1x 1x 75x 75x 22x 22x 22x 22x 22x 22x 22x 75x 23x 23x 23x 23x 23x 23x 23x 75x 75x 30x 30x 30x 30x 30x 75x 75x 1x 1x 1x 1x 1x 1x 74x 74x 74x 44x 74x 74x 30x 74x 74x 1x 1x 1x 1x 1x 62x 56x 6x 62x 1x 1x 1x 1x 1x 1x 1x 1x 74x 42x 42x 32x 32x 74x 74x 20x 74x 74x 12x 74x 74x 1x
import {ENDPOINTS} from '../../resources/network-types.js';
 
/**
 * Indicating, which MFA method should be used
 */
const MFAMethodType = {
    DEVICE: 1,
    SMS: 2,
    VOICE: 3,
} as const;
 
/**
 * The values of {@link MFAMethodType}
 */
type MFAMethodType = typeof MFAMethodType[keyof typeof MFAMethodType];
 
/**
 * A class to hold the status of the currently executed MFA method
 */
export class MFAMethod {
    /**
     * The type of MFA method used
     */
    type!: MFAMethodType;
    /**
     * The id of the phone number used - only set for sms and voice methods
     */
    numberId?: number;
    /**
     * The backend's 'nonFTEU' flag of the phone number used - only set for sms and voice methods, if provided by the backend
     */
    nonFTEU?: boolean;
 
    /**
     * 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
     * @param nonFTEU - The backend's 'nonFTEU' flag of the phone number used for sending sms or voice codes
     */
    constructor(mfaMethod: `device` | `voice` | `sms` = `device`, numberId: number = 1, nonFTEU?: boolean) {
        this.update(mfaMethod, numberId, nonFTEU);
    }
 
    /**
     * Updates this object to the given method and number id
     * @param mfaMethod - The method to be used: `device`, `voice` or `sms` (anything else falls back to `device`). Defaults to `device`
     * @param numberId - The number id used for sending sms or voice codes. Defaults to 1
     * @param nonFTEU - The backend's 'nonFTEU' flag of the phone number used for sending sms or voice codes
     */
    update(mfaMethod: string = `device`, numberId: number = 1, nonFTEU?: boolean) {
        switch (mfaMethod) {
        case `sms`:
            this.type = MFAMethodType.SMS;
            this.numberId = numberId;
            this.nonFTEU = nonFTEU;
            break;
        case `voice`:
            this.type = MFAMethodType.VOICE;
            this.numberId = numberId;
            this.nonFTEU = nonFTEU;
            break;
        default:
        case `device`:
            this.type = MFAMethodType.DEVICE;
            this.numberId = undefined;
            this.nonFTEU = undefined;
            break;
        }
    }
 
    /**
     *
     * @returns True, if the 'device' method is active
     */
    get isDevice(): boolean {
        return this.type === MFAMethodType.DEVICE;
    }
 
    /**
     *
     * @returns True, if the 'sms' method is active
     */
    get isSMS(): boolean {
        return this.type === MFAMethodType.SMS;
    }
 
    /**
     *
     * @returns True, if the 'voice' method is active
     */
    get 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 ENDPOINTS.AUTH.BASE + ENDPOINTS.AUTH.PATH.MFA.PHONE_RESEND;
        default:
        case MFAMethodType.DEVICE:
            return ENDPOINTS.AUTH.BASE + ENDPOINTS.AUTH.PATH.MFA.DEVICE_RESEND;
        }
    }
 
    /**
     *
     * @returns The appropriate payload for resending the code, given the currently selected MFA Method
     */
    getResendPayload(): any {
        switch (this.type) {
        case MFAMethodType.VOICE:
            return {
                phoneNumber: this.phoneNumberPayload,
                mode: `voice`,
            };
        case MFAMethodType.SMS:
            return {
                phoneNumber: this.phoneNumberPayload,
                mode: `sms`,
            };
        default:
        case MFAMethodType.DEVICE:
            return undefined;
        }
    }
 
    /**
     * Checks if the status code matches our expectation for a successful resend
     * @param status - The status code for the response received from the backend
     * @returns True, if the response was successful, based on the currently selected MFA Method
     */
    resendSuccessful(status: number): boolean {
        switch (this.type) {
        case MFAMethodType.VOICE:
        case MFAMethodType.SMS:
            return status === 200;
        default:
        case MFAMethodType.DEVICE:
            return status === 202 || status === 200;
        }
    }
 
    /**
     * @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: this.phoneNumberPayload,
                mode: `voice`,
            };
        case MFAMethodType.SMS:
            return {
                securityCode: {
                    code: `${mfa}`,
                },
                phoneNumber: this.phoneNumberPayload,
                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 ENDPOINTS.AUTH.BASE + ENDPOINTS.AUTH.PATH.MFA.PHONE_ENTER;
        default:
        case MFAMethodType.DEVICE:
            return ENDPOINTS.AUTH.BASE + ENDPOINTS.AUTH.PATH.MFA.DEVICE_ENTER;
        }
    }
 
    /**
     * The phone number object used in the payload when requesting or entering a code via sms or voice
     */
    private get phoneNumberPayload(): {id?: number, nonFTEU?: boolean} {
        return this.nonFTEU === undefined
            ? {id: this.numberId}
            : {id: this.numberId, nonFTEU: this.nonFTEU};
    }
 
    /**
     * Checks if the status code matches our expectation for a successful enter
     * Since iOS 26.4 the backend acknowledges a valid code with status 409 - the response body needs to be checked by the caller in this case
     * @param status - The status code for the response received from the backend
     * @returns True, if the response was successful (or potentially successful), based on the currently selected MFA Method
     */
    enterSuccessful(status: number): boolean {
        if (status === 409) {
            return true;
        }
 
        switch (this.type) {
        case MFAMethodType.VOICE:
        case MFAMethodType.SMS:
            return status === 200;
        default:
        case MFAMethodType.DEVICE:
            return status === 204;
        }
    }
}