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

100% Statements 220/220
82.6% Branches 38/46
100% Functions 13/13
100% Lines 220/220

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 2201x 1x 1x 1x 1x 5x 5x 5x 5x 5x 1x 1x 1x 1x 1x 367x 367x 367x 367x 367x 367x 367x 367x 367x 367x 367x 367x 367x 367x 367x 367x 367x 367x 367x 367x 367x 367x 367x 367x 374x 374x 22x 22x 22x 374x 22x 22x 22x 374x 374x 330x 330x 330x 374x 374x 367x 367x 367x 367x 367x 367x 4x 4x 367x 367x 367x 367x 367x 367x 12x 12x 367x 367x 367x 367x 367x 367x 8x 8x 367x 367x 367x 367x 367x 367x 32x 32x 12x 32x 12x 32x 32x 8x 32x 32x 367x 367x 367x 367x 367x 367x 18x 18x 18x 12x 18x 18x 6x 18x 18x 367x 367x 367x 367x 367x 367x 18x 18x 6x 6x 6x 6x 6x 6x 18x 6x 6x 6x 6x 6x 6x 18x 18x 6x 18x 18x 367x 367x 367x 367x 367x 367x 367x 18x 18x 18x 12x 18x 18x 6x 18x 18x 367x 367x 367x 367x 367x 367x 30x 30x 10x 10x 10x 10x 10x 10x 10x 10x 10x 30x 10x 10x 10x 10x 10x 10x 10x 10x 10x 30x 30x 10x 10x 10x 10x 10x 30x 30x 367x 367x 367x 367x 367x 367x 30x 30x 30x 20x 30x 30x 10x 30x 30x 367x 367x 367x 367x 367x 367x 367x 30x 30x 30x 20x 30x 30x 10x 30x 30x 367x
import {ENDPOINTS} from '../../resources/network-types.js';
 
/**
 * Indicating, which MFA method should be used
 */
enum MFAMethodType {
    DEVICE = 1,
    SMS = 2,
    VOICE = 3
}
 
/**
 * 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 device used
     */
    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
     */
    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: {
                    id: this.numberId,
                },
                mode: `voice`,
            };
        case MFAMethodType.SMS:
            return {
                phoneNumber: {
                    id: this.numberId,
                },
                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) {
        switch (this.type) {
        case MFAMethodType.VOICE:
        case MFAMethodType.SMS:
            return status === 200;
        default:
        case MFAMethodType.DEVICE:
            return status === 202;
        }
    }
 
    /**
     * @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 ENDPOINTS.AUTH.BASE + ENDPOINTS.AUTH.PATH.MFA.PHONE_ENTER;
        default:
        case MFAMethodType.DEVICE:
            return ENDPOINTS.AUTH.BASE + ENDPOINTS.AUTH.PATH.MFA.DEVICE_ENTER;
        }
    }
 
    /**
     * Checks if the status code matches our expectation for a successful enter
     * @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
     */
    enterSuccessful(status: number): boolean {
        switch (this.type) {
        case MFAMethodType.VOICE:
        case MFAMethodType.SMS:
            return status === 200;
        default:
        case MFAMethodType.DEVICE:
            return status === 204;
        }
    }
}