All files / lib/icloud auth.ts

99.6% Statements 500/502
100% Branches 107/107
94.73% Functions 18/19
99.6% Lines 500/502

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 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 5021x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 233x 233x 233x 233x 233x 233x 233x 233x 233x 233x 233x 233x 233x 233x 233x 233x 233x 233x 233x 233x 233x 233x 233x 233x 233x 233x 233x 233x 233x 233x 233x 233x 233x 233x 233x 233x 233x 233x 233x 233x 233x 233x 233x 233x 233x 233x 233x 233x 233x 233x 233x 26x 233x 207x 207x 233x 233x 233x 233x 233x 233x 26x 26x 26x 26x 26x 26x 26x 25x 25x 26x 233x 233x 233x 233x 233x 3x 3x 3x 3x 2x 3x 2x 2x 2x 3x 233x 233x 233x 233x 233x 233x 233x 4x 4x 4x 4x 4x 4x 4x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 233x 233x 233x 233x 233x 233x 21x 21x 21x 21x 21x 21x 21x 233x 233x 233x 233x 233x 233x 233x 2x 2x 2x 2x 2x 1x 233x 233x 233x 233x 233x 233x 6x 6x 6x 6x 6x 233x 233x 233x 233x 233x 233x 233x 233x 5x 5x 5x 1x 1x 1x 3x 3x 3x 3x 51x 51x 51x 48x 48x 48x 3x 3x 5x 1x 1x 1x 2x 2x 2x 2x 2x 233x 233x 233x 233x 233x 233x 233x 2x 2x 2x 2x 233x 233x 233x 233x 233x 233x 233x 2x 2x 2x 34x 2x 34x 32x 32x 2x 2x 2x 2x 233x 233x 233x 233x 233x 233x 233x 4x 4x 4x 1x 1x 3x 4x 1x 1x 2x 2x 2x 2x 2x 2x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 2x 4x 4x 233x 233x 233x 233x 233x 233x 10x 1x 1x 1x 9x 9x 9x 9x 9x 9x 10x 3x 3x 3x 50x 3x 3x 3x 3x 3x 10x 233x 233x 233x 233x 233x 233x 233x 4x 4x 4x 1x 1x 1x 1x 3x 4x 2x 4x 1x 1x 1x 4x 233x 233x 233x 233x 233x 10x 5x 1x 1x 1x 1x 4x 5x 1x 1x 1x 1x 3x 5x 1x 1x 1x 1x 2x 5x 1x 1x 1x 1x 5x 6x 10x 5x 1x 1x 1x 1x 4x 5x 1x 1x 1x 1x 3x 5x 1x 1x 1x 1x 2x 5x 1x 1x 1x 1x 5x 10x 233x 233x 233x 233x 233x 233x 13x 1x 1x 1x 12x 13x 1x 1x 1x 13x 233x 233x 233x 233x 233x 233x 30x 1x 1x 1x 1x 29x 30x 1x 1x 1x 1x 28x 30x 1x 1x 1x 1x 30x 233x 233x 233x 233x 233x 233x 11x 2x 2x 2x 9x 11x 1x 1x 1x 11x 233x 233x 233x 233x 233x 233x     233x
import {AxiosResponse} from "axios";
import * as fs from 'fs/promises';
import {readFileSync} from 'fs';
import * as path from 'path';
import {Cookie} from "tough-cookie";
import {iCPSError} from "../../app/error/error.js";
import {getLogger} from "../logger.js";
 
import * as ICLOUD from './constants.js';
import {AUTH_ERR} from "../../app/error/error-codes.js";
import {Zones} from "./icloud-photos/query-builder.js";
 
/**
 * Secrets, required to track authentication request across the MFA process
 */
export interface AuthSecrets {
    /**
     * X-Apple-ID-Session-Id / X-Apple-Session-Token
     */
    sessionId?: string,
    /**
     * Apple provided header to identify requests during authentication
     */
    scnt?: string,
    /**
     * Apple session cookie during authentication
     */
    aasp?: string
}
 
/**
 * Account secrets
 */
export interface AccountSecrets {
    /**
     * Apple ID username
     */
    username?: string,
    /**
     * Apple ID Password
     */
    password?: string,
}
 
/**
 * Account tokens required to setup iCloud
 */
export interface AccountTokens {
    /**
     * Session Token
     */
    sessionToken?: string,
    /**
     * 2FA trust token
     */
    trustToken?: string
}
 
export interface PhotosAccountZone {
    zoneName?: string,
    zoneType?: string,
    ownerName?: string,
}
 
/**
 * Authentication information required to interact with the iCloud Photos backend
 */
export interface PhotosAccount {
    photosDomain?: string,
    primary?: PhotosAccountZone,
    shared?: PhotosAccountZone
}
 
/**
 * This class holds all iCloud related authentication information
 */
export class iCloudAuth {
    /**
     * Default logger for this class
     */
    private logger = getLogger(this);
 
    /**
     * Cookies required to access iCloud services
     */
    iCloudCookies: Cookie[] = [];
 
    /**
     * Secrets required during authentication process
     */
    iCloudAuthSecrets: AuthSecrets = {};
 
    /**
     * Account tokens
     */
    iCloudAccountTokens: AccountTokens = {};
 
    /**
     * General Account secrets
     */
    iCloudAccountSecrets: AccountSecrets = {};
 
    /**
     * Relevant account information for iCloud Photos Service
     */
    iCloudPhotosAccount: PhotosAccount = {};
 
    /**
     * File path to the location, where the trust token is persisted on disk to circumvent future MFA requests
     */
    trustTokenFile: string;
 
    /**
     *
     * @param username - The AppleID username
     * @param password - The AppleID password
     * @param trustToken - The trust token in string format. Will take precedence over any stored file
     * @param appDataDir - The directory to store authentication tokens for future re-authentication without MFA
     * @param sharedLibrary - Specifies if the shared iCloud Photos Library should be used
     */
    constructor(username: string, password: string, trustToken: string, appDataDir: string) {
        this.iCloudAccountSecrets.username = username;
        this.iCloudAccountSecrets.password = password;
        this.trustTokenFile = path.format({
            "dir": appDataDir,
            "base": ICLOUD.TRUST_TOKEN_FILE_NAME,
        });
        if (!trustToken) {
            this.loadTrustToken();
        } else {
            this.iCloudAccountTokens.trustToken = trustToken;
        }
    }
 
    /**
     * Tries loading the trust token from disk. Loading is done synchronously in order to avoid race conditions, where authentication is attempted before token is loaded
     */
    loadTrustToken() {
        this.logger.debug(`Trying to load trust token from disk`);
        try {
            const trustToken = readFileSync(this.trustTokenFile, {"encoding": ICLOUD.TRUST_TOKEN_FILE_ENCODING});
            this.logger.debug(`Acquired trust token from file`);
            this.logger.trace(`  - token: ${trustToken}`);
            this.iCloudAccountTokens.trustToken = trustToken;
        } catch (err) {
            this.logger.debug(`Unable to acquire trust token from file: ${err.message}`);
        }
    }
 
    /**
     * Tries to write the trust token to disk
     */
    async storeTrustToken() {
        this.logger.debug(`Trying to persist trust token to disk`);
        const trustTokenPath = path.dirname(this.trustTokenFile);
        try {
            await fs.mkdir(trustTokenPath, {"recursive": true});
            await fs.writeFile(this.trustTokenFile, this.iCloudAccountTokens.trustToken, {"encoding": ICLOUD.TRUST_TOKEN_FILE_ENCODING});
        } catch (err) {
            throw new iCPSError(AUTH_ERR.STORE_TRUST_TOKEN)
                .addCause(err);
        }
    }
 
    /**
     * Processing the response from the initial authentication request and populating the AuthSecret object
     * Trying to extract X-Apple-Session-Token, scnt Header and aasp Cookie
     * @param response - The response from the initial authentication request
     */
    processAuthSecrets(response: AxiosResponse) {
        this.logger.debug(`Processing iCloud authentication response`);
        this.iCloudAuthSecrets.sessionId = response.headers[ICLOUD.AUTH_RESPONSE_HEADER.SESSION_TOKEN.toLowerCase()];
        this.iCloudAccountTokens.sessionToken = this.iCloudAuthSecrets.sessionId;
        this.iCloudAuthSecrets.scnt = response.headers[ICLOUD.AUTH_RESPONSE_HEADER.SCNT.toLowerCase()];
 
        const cookieHeaders = response.headers[`set-cookie`];
        if (!cookieHeaders || !Array.isArray(cookieHeaders) || cookieHeaders.length === 0) {
            throw new iCPSError(AUTH_ERR.COOKIES)
                .addContext(`responseHeaders`, response.headers);
        }
 
        const extractedHeader: string = cookieHeaders.find(el => el.startsWith(`${ICLOUD.AUTH_RESPONSE_HEADER.AASP_COOKIE}=`));
        const removedKey: string = extractedHeader.substring(ICLOUD.AUTH_RESPONSE_HEADER.AASP_COOKIE.length + 1);
        const removedMetadata: string = removedKey.split(`;`)[0];
        this.iCloudAuthSecrets.aasp = removedMetadata;
 
        this.validateAuthSecrets();
        this.logger.debug(`Authentication processed, auth secrets populated`);
        this.logger.trace(`  - auth secrets: ${JSON.stringify(this.iCloudAuthSecrets)}`);
    }
 
    /**
     * Headers required for MFA authentication flow (Enter 2-FA + get tokens)
     * @returns The header object for the request
     */
    getMFAHeaders(): any {
        this.validateAuthSecrets();
        return {...ICLOUD.DEFAULT_AUTH_HEADER,
            "scnt": this.iCloudAuthSecrets.scnt,
            'X-Apple-ID-Session-Id': this.iCloudAuthSecrets.sessionId,
            "Cookie": `aasp=${this.iCloudAuthSecrets.aasp}`,
        };
    }
 
    /**
     * Processing the response from acquiring the necessary trust tokens. This method is automatically storing the trust token on disk
     * @param response - The response from the trust token endpoint
     * @throws A fatal error in case it failed
     */
    async processAccountTokens(response: AxiosResponse) {
        this.logger.debug(`Processing trust token response`);
        this.iCloudAccountTokens.sessionToken = response?.headers[ICLOUD.AUTH_RESPONSE_HEADER.SESSION_TOKEN.toLowerCase()];
        this.iCloudAccountTokens.trustToken = response?.headers[ICLOUD.AUTH_RESPONSE_HEADER.TRUST_TOKEN.toLowerCase()];
        this.validateAccountTokens();
        await this.storeTrustToken();
    }
 
    /**
     *
     * @returns Data required for iCloud setup request
     */
    getSetupData(): any {
        return {
            "dsWebAuthToken": this.iCloudAccountTokens.sessionToken,
            "trustToken": this.iCloudAccountTokens.trustToken,
        };
    }
 
    /**
     * Parses the iCloud setup response in order to acquire necessary secrets for further requests
     * @param response - The successful response during iCloud Setup
     * @returns True if successful processing, false otherwise
     * @throws An iCloudAuthError in case the response cannot be processed
     */
    processCloudSetupResponse(response: AxiosResponse) {
        this.logger.debug(`Processing iCloud setup response`);
        const cookieHeaders = response.headers[`set-cookie`];
        if (!cookieHeaders || !Array.isArray(cookieHeaders) || cookieHeaders.length === 0) {
            throw new iCPSError(AUTH_ERR.COOKIES)
                .addContext(`responseHeaders`, response.headers);
        }
 
        this.iCloudCookies = [];
 
        cookieHeaders.forEach(cookieString => {
            const cookie = Cookie.parse(cookieString);
            // Filtering empty cookies
            if (cookie.value) {
                this.logger.trace(`Adding cookie: ${cookie}`);
                this.iCloudCookies.push(cookie);
            }
        });
 
        if (!response?.data?.webservices?.ckdatabasews?.url) {
            throw new iCPSError(AUTH_ERR.NO_PHOTOS_DOMAIN)
                .addContext(`responseData`, response.data);
        }
 
        this.iCloudPhotosAccount.photosDomain = response.data.webservices.ckdatabasews.url;
 
        this.validateCloudCookies();
    }
 
    /**
     *
     * @returns A fully authenticated header, to be used with the iCloud Photos Service
     * @throws An iCloudAuthError in case the returned headers would be expired
     */
    getPhotosHeader(): any {
        return {...ICLOUD.DEFAULT_HEADER,
            "Cookie": this.getCookiesHeaderString(),
        };
    }
 
    /**
     * Builds the cookie header string for future requests
     * @returns The cookie header string or an empty string (if cookies are not available)
     * @throws An iCloudAuthError in case the returned headers would be expired
     */
    getCookiesHeaderString(): string {
        let cookieString: string = ``;
        this.validateCloudCookies();
        this.iCloudCookies.forEach(cookie => {
            if (cookieString.length === 0) {
                cookieString = cookie.cookieString();
            } else {
                cookieString = `${cookieString}; ${cookie.cookieString()}`;
            }
        });
        this.logger.trace(`Build cookie string: ${cookieString}`);
        return cookieString;
    }
 
    /**
     * Processing setup response to acquire PhotosAccount
     * @param response - The Photos Setup response
     * @throws An iCloudAuthError in case the setup response cannot be processed
     */
    processPhotosSetupResponse(response: AxiosResponse) {
        this.logger.debug(`Processing Photos setup request`);
 
        if (response.data?.moreComing) {
            throw new iCPSError(AUTH_ERR.TOO_MANY_ZONES);
        }
 
        if (!Array.isArray(response.data?.zones)) {
            throw new iCPSError(AUTH_ERR.ZONE_RESPONSE_INVALID);
        }
 
        const availableZones = response.data.zones as any[];
        this.logger.info(`Found ${availableZones.length} available zones`);
 
        const primaryZone = availableZones.find(zone => zone.zoneID.zoneName === `PrimarySync`);
        this.iCloudPhotosAccount.primary = {
            "ownerName": primaryZone?.zoneID?.ownerRecordName,
            "zoneName": primaryZone?.zoneID?.zoneName,
            "zoneType": primaryZone?.zoneID?.zoneType,
        };
 
        const sharedZone = availableZones.find(zone => zone.zoneID.zoneName.startsWith(`SharedSync-`));
        if (sharedZone) {
            this.logger.debug(`Found shared zone ${sharedZone.zoneID?.zoneName}`);
            this.iCloudPhotosAccount.shared = {
                "ownerName": sharedZone.zoneID?.ownerRecordName,
                "zoneName": sharedZone.zoneID?.zoneName,
                "zoneType": sharedZone.zoneID?.zoneType,
            };
        }
 
        this.validatePhotosAccount(!sharedZone ? Zones.Primary : undefined);
    }
 
    /**
     * Validates that the object is in a authenticated iCloud state
     * @throws An iCloudAuthError, if the cloud cookies are no longer valid
     */
    validateCloudCookies() {
        if (!this.iCloudCookies || this.iCloudCookies.length === 0) {
            throw new iCPSError(AUTH_ERR.COOKIE_VALIDATION)
                .addMessage(`No cookies loaded`);
        }
 
        const expiredCookies = this.iCloudCookies
            .filter(cookie => cookie.expires !== `Infinity`) // Being explicit about that
            .filter(cookie => cookie.TTL() <= 0)
            .filter(cookie => (cookie.expires as Date).getTime() !== 1000);
 
        if (expiredCookies.length > 0) {
            this.logger.trace(`Found expired cookies: ${JSON.stringify(expiredCookies)}`);
            // Cleaning data before attaching it to error
            this.iCloudCookies.forEach(cookie => {
                cookie.value = ``;
            });
            throw new iCPSError(AUTH_ERR.COOKIE_VALIDATION)
                .addMessage(`Some cookies are expired`)
                .addContext(`iCloudCookies`, this.iCloudCookies);
        }
    }
 
    /**
     * Validates that the object holds all information required to perform actions against the photos service
     * @param zone - The zone to validate, undefined to validate both
     * @throws An iCloudAuthError, if the photos account cannot be validated
     */
    validatePhotosAccount(zone?: Zones) {
        this.validateCloudCookies();
 
        if (!this.iCloudPhotosAccount.photosDomain || this.iCloudPhotosAccount.photosDomain.length === 0) {
            throw new iCPSError(AUTH_ERR.PHOTOS_ACCOUNT_VALIDATION)
                .addMessage(`PhotosDomain invalid`)
                .addContext(`invalidPhotosAccount`, this.iCloudPhotosAccount);
        }
 
        if (zone) {
            this.validateZone(zone);
        } else {
            this.validateZone(Zones.Primary);
            this.validateZone(Zones.Shared);
        }
    }
 
    /**
     * Validates a given zone
     */
    validateZone(zone: Zones) {
        if (zone === Zones.Primary) {
            if (!this.iCloudPhotosAccount.primary) {
                throw new iCPSError(AUTH_ERR.PHOTOS_ACCOUNT_VALIDATION)
                    .addMessage(`PrimaryZone missing`)
                    .addContext(`invalidPhotosAccount`, this.iCloudPhotosAccount);
            }
 
            if (!this.iCloudPhotosAccount.primary?.zoneName || this.iCloudPhotosAccount.primary?.zoneName.length === 0) {
                throw new iCPSError(AUTH_ERR.PHOTOS_ACCOUNT_VALIDATION)
                    .addMessage(`Primary ZoneName invalid`)
                    .addContext(`invalidPhotosAccount`, this.iCloudPhotosAccount);
            }
 
            if (!this.iCloudPhotosAccount.primary?.zoneType || this.iCloudPhotosAccount.primary?.zoneType.length === 0) {
                throw new iCPSError(AUTH_ERR.PHOTOS_ACCOUNT_VALIDATION)
                    .addMessage(`Primary ZoneType invalid`)
                    .addContext(`invalidPhotosAccount`, this.iCloudPhotosAccount);
            }
 
            if (!this.iCloudPhotosAccount.primary?.ownerName || this.iCloudPhotosAccount.primary?.ownerName.length === 0) {
                throw new iCPSError(AUTH_ERR.PHOTOS_ACCOUNT_VALIDATION)
                    .addMessage(`Primary OwnerName invalid`)
                    .addContext(`invalidPhotosAccount`, this.iCloudPhotosAccount);
            }
        }
 
        if (zone === Zones.Shared) {
            if (!this.iCloudPhotosAccount.shared) {
                throw new iCPSError(AUTH_ERR.PHOTOS_ACCOUNT_VALIDATION)
                    .addMessage(`PrimaryZone missing`)
                    .addContext(`invalidPhotosAccount`, this.iCloudPhotosAccount);
            }
 
            if (!this.iCloudPhotosAccount.shared?.zoneName || this.iCloudPhotosAccount.shared?.zoneName.length === 0) {
                throw new iCPSError(AUTH_ERR.PHOTOS_ACCOUNT_VALIDATION)
                    .addMessage(`Shared ZoneName invalid`)
                    .addContext(`invalidPhotosAccount`, this.iCloudPhotosAccount);
            }
 
            if (!this.iCloudPhotosAccount.shared?.zoneType || this.iCloudPhotosAccount.shared?.zoneType.length === 0) {
                throw new iCPSError(AUTH_ERR.PHOTOS_ACCOUNT_VALIDATION)
                    .addMessage(`Shared ZoneType invalid`)
                    .addContext(`invalidPhotosAccount`, this.iCloudPhotosAccount);
            }
 
            if (!this.iCloudPhotosAccount.shared?.ownerName || this.iCloudPhotosAccount.shared?.ownerName.length === 0) {
                throw new iCPSError(AUTH_ERR.PHOTOS_ACCOUNT_VALIDATION)
                    .addMessage(`Shared OwnerName invalid`)
                    .addContext(`invalidPhotosAccount`, this.iCloudPhotosAccount);
            }
        }
    }
 
    /**
     * Validates that the objects holds all account secrets
     * @throws An iCloudAuthError, if the account secrets cannot be validated
     */
    validateAccountSecrets() {
        if (!this.iCloudAccountSecrets.username || this.iCloudAccountSecrets.username.length === 0) {
            throw new iCPSError(AUTH_ERR.ACCOUNT_SECRETS_VALIDATION)
                .addMessage(`Username invalid`);
        }
 
        if (!this.iCloudAccountSecrets.password || this.iCloudAccountSecrets.password.length === 0) {
            throw new iCPSError(AUTH_ERR.ACCOUNT_SECRETS_VALIDATION)
                .addMessage(`Password invalid`);
        }
    }
 
    /**
     * Validates the authentication secrets
     * @throws An iCloudAuthError, if authentication secrets cannot be validated
     */
    validateAuthSecrets() {
        if (!this.iCloudAuthSecrets.aasp || this.iCloudAuthSecrets.aasp.length === 0) {
            throw new iCPSError(AUTH_ERR.AUTH_SECRETS_VALIDATION)
                .addMessage(`aasp invalid`)
                .addContext(`invalidAuthSecrets`, this.iCloudAuthSecrets);
        }
 
        if (!this.iCloudAuthSecrets.scnt || this.iCloudAuthSecrets.scnt.length === 0) {
            throw new iCPSError(AUTH_ERR.AUTH_SECRETS_VALIDATION)
                .addMessage(`scnt invalid`)
                .addContext(`invalidAuthSecrets`, this.iCloudAuthSecrets);
        }
 
        if (!this.iCloudAuthSecrets.sessionId || this.iCloudAuthSecrets.sessionId.length === 0) {
            throw new iCPSError(AUTH_ERR.AUTH_SECRETS_VALIDATION)
                .addMessage(`sessionId invalid`)
                .addContext(`invalidAuthSecrets`, this.iCloudAuthSecrets);
        }
    }
 
    /**
     * Validates the account tokens
     * @throws An iCloudAuthError, if account tokens cannot be validated
     */
    validateAccountTokens() {
        if (!this.iCloudAccountTokens.sessionToken || this.iCloudAccountTokens.sessionToken.length === 0) {
            throw new iCPSError(AUTH_ERR.ACCOUNT_TOKEN_VALIDATION)
                .addMessage(`sessionToken invalid`);
        }
 
        if (!this.iCloudAccountTokens.trustToken || this.iCloudAccountTokens.trustToken.length === 0) {
            throw new iCPSError(AUTH_ERR.ACCOUNT_TOKEN_VALIDATION)
                .addMessage(`trustToken invalid`);
        }
    }
 
    /**
     *
     * @returns True if shared library is available, false otherwise
     */
    sharedLibraryAvailable(): boolean {
        return Boolean(this.iCloudPhotosAccount.shared);
    }
}