All files / lib/resources network-types.ts

100% Statements 572/572
100% Branches 1/1
100% Functions 1/1
100% Lines 572/572

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 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 5721x 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 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 36x 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 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 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 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 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 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 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x
/**
 * This file holds information relevant to networking, as well as type definitions of the expected responses
 */
 
import {jsonc} from "jsonc";
import {Resources} from "./main.js";
 
/**
 * Hard coded client id, extracted from previous requests
 */
export const CLIENT_ID = `d39ba9916b7251055b22c7f910e2ea796ee65e98b2ddecea8f5dde8d9d1a815d`;
 
/**
 * User Agent this CLI is using. Emulating a Firefox Browser
 */
export const USER_AGENT = `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36`;
 
/**
 * Client information shared with the iCloud backend based on the user agent
 */
export const CLIENT_INFO = jsonc.stringify({
    U: USER_AGENT,
    L: `en-US`,
    Z: `GMT+01:00`,
    V: `1.1`,
    F: ``,
});
 
/**
 * Keys for dynamic header values
 */
export const HEADER_KEYS = {
    SCNT: `scnt`,
    SESSION_ID: `X-Apple-ID-Session-Id`,
    AUTH_ATTRIBUTES: `X-Apple-Auth-Attributes`,
    FRAME_ID: `X-Apple-Frame-Id`,
    OAUTH_STATE: `X-Apple-OAuth-State`,
    COOKIE: `Cookie`,
};
 
/**
 * Keys for dynamic cookie values
 */
export const COOKIE_KEYS = {
    AASP: `aasp`,
    X_APPLE: `X-APPLE-`,
    PCS_PHOTOS: `X-APPLE-WEBAUTH-PCS-Photos`,
    PCS_SHARING: `X-APPLE-WEBAUTH-PCS-Sharing`,
    WEBAUTH_TOKEN: `X-APPLE-WEBAUTH-TOKEN`,
};
 
/**
 * List of endpoints used in this application
 */
export const ENDPOINTS = {
    /**
     * Authentication endpoints needed to acquire the session secret and two trust token
     */
    AUTH: {
        BASE: `https://idmsa.apple.com/appleauth/auth`,
        PATH: {
            SIGNIN: {
                LEGACY: `/signin`,
                INIT: `/signin/init`,
                COMPLETE: `/signin/complete`,
            },
            MFA: {
                /**
                 * Since iOS 26.4, the code is no longer pushed to trusted devices automatically - it needs to be requested via a PUT request to this endpoint
                 */
                DEVICE_RESEND: `/verify/trusteddevice/securitycode`,
                DEVICE_ENTER: `/verify/trusteddevice/securitycode`,
                PHONE_RESEND: `/verify/phone`,
                PHONE_ENTER: `/verify/phone/securitycode`,
                /**
                 * Security key endpoints:
                 * SECURITY_KEY_ENTER: '/verify/security/key'
                 * Payload:
                 * \{
                 *      "challenge":"43-character-challenge",
                 *      "clientData":"[redacted]",
                 *      "signatureData":"[redacted]",
                 *      "authenticatorData":"[redacted]",
                 *      "userHandle":"[redacted]",
                 *      "credentialID":"[redacted]",
                 *      "rpId":"apple.com"
                 * \}
                 */
            },
            TRUST: `/2sv/trust`,
            /**
             * Since iOS 26.4 the backend might require a second SRP-based password proof ('escrow'), signaled through the 'X-Apple-EDP' or 'X-Apple-PDP' header on a 409 response
             */
            ESCROW: {
                INIT: `/escrow/init`,
                COMPLETE: `/escrow/complete`,
            },
        },
    },
    /**
     * Setup endpoints needed to acquire cookies and Photos URL
     */
    SETUP: {
        BASE(): string {
            return `https://setup.${Resources.network().iCloudRegionUrl()}`;
        },
        PATH: {
            ACCOUNT_LOGIN: `/setup/ws/1/accountLogin`,
            LOGOUT: `/setup/ws/1/logout`,
            REQUEST_PCS: `/setup/ws/1/requestPCS`,
        },
    },
    /**
     * Photos endpoints needed to access the photos library
     */
    PHOTOS: {
        /**
         * Base URL for photos requests is dynamic - the path is static
         */
        BASE_PATH: `/database/1/com.apple.photos.cloud/production`,
        AREAS: {
            PRIVATE: `/private`,
            SHARED: `/shared`,
        },
        PATH: {
            QUERY: `/records/query`,
            MODIFY: `/records/modify`,
            ZONES: `/changes/database`,
        },
    },
};
 
/**
 * The expected response format for the signin request
 * @see {@link ENDPOINTS.AUTH.PATH.SIGNIN.LEGACY}
 * @see {@link ENDPOINTS.AUTH.PATH.SIGNIN.COMPLETE}
 */
export type SigninResponse = {
    /**
     * 409 if MFA or escrow is required, 200 if the trust token was accepted
     */
    status: 200 | 409,
    /**
     * Data should be irrelevant for this one
     */
    data: {
        authType: `hsa2`,
    },
    headers: {
        /**
         * Scnt token - required to keep track of MFA request
         * @minLength 1
         */
        scnt: string,
        /**
         * Session secret - required to keep track of MFA request
         * @minLength 1
         */
        'x-apple-session-token': string,
        /**
         * Session id used by the web frontend to track the authentication flow
         * @minLength 1
         */
        'x-apple-id-session-id'?: string,
        /**
         * Country code of the account, used during account setup
         */
        'x-apple-id-account-country'?: string,
        /**
         * Should hold the 'aasp' cookie
         * @minItems 1
         */
        'set-cookie': string[],
    }
}
 
/**
 * Currently supported SRP password hashing protocols
 */
export type SRPProtocol = `s2k` | `s2k_fo`;
 
/**
 * The expected response format for the signin init request
 * @see {@link ENDPOINTS.AUTH.PATH.SIGNIN.INIT}
 */
export type SigninInitResponse = {
    data: {
        /**
         * Number of iterations for PBKDF2 key derivation function
         */
        iteration: number,
        /**
         * Salt for the PBKDF2 key derivation function and SRP protocol
         * @minLength 1
         */
        salt: string,
        /**
         * How to encode the hashed password
         */
        protocol: SRPProtocol,
        /**
         * The servers's ephemeral public key
         * @minLength 1
         */
        b: string,
        /**
         * Some kind of session ID
         * @minLength 1
         */
        c: string
    },
    headers: {
        /**
         * Scnt token - required to keep track of auth request
         * @minLength 1
         */
        scnt: string
    }
}
 
/**
 * The expected response format for the escrow init request - holding the same SRP challenge as the signin init response
 * @see {@link ENDPOINTS.AUTH.PATH.ESCROW.INIT}
 */
export type EscrowInitResponse = {
    data: SigninInitResponse[`data`]
}
 
/**
 * The expected response format when getting auth information
 * @see {@link ENDPOINTS.AUTH.BASE}
 */
export type AuthInformationResponse = {
    data: {
        /**
         * @minItems 1
         */
        trustedPhoneNumbers: TrustedPhoneNumber[]
    }
}
 
/**
 * The expected response format for the MFA resend request on a trusted device
 * @see {@link ENDPOINTS.AUTH.PATH.MFA.DEVICE_RESEND}
 */
export type ResendMFADeviceResponse = {
    /**
     * Apple does not reliably return a body for this request (usually status 202 without content)
     */
    data?: {
        /**
         * Number of available trusted devices
         */
        trustedDeviceCount?: number,
        /**
         * Properties of the requested security code
         */
        securityCode?: SecurityCodeFormat,
        /**
         * Object holding information about alternative phone number verification
         */
        phoneNumberVerification?: PhoneNumberVerification
    } | ``
}
 
/**
 * The expected response format for the MFA resend request on a trusted phone number
 * @see {@link ENDPOINTS.AUTH.PATH.MFA.PHONE_RESEND}
 */
export type ResendMFAPhoneResponse = {
    data: PhoneNumberVerification
}
 
/**
 * Information about phone number verification used in MFA resend responses
 */
type PhoneNumberVerification = {
    /**
     * The phone number used for verification
     */
    trustedPhoneNumber: TrustedPhoneNumber,
    trustedPhoneNumbers?: TrustedPhoneNumber[],
    securityCode?: SecurityCodeFormat,
}
 
/**
 * Object representing a trusted phone number used in MFA resend responses
 */
export type TrustedPhoneNumber = {
    /**
     * @minimum 0
     */
    id: number,
    /**
     * @minLength 1
     */
    numberWithDialCode: string,
    /**
     * @pattern ^sms|voice$
     */
    pushMode?: string,
    obfuscatedNumber?: string,
    lastTwoDigits?: string,
    /**
     * Flag provided by the backend, that needs to be echoed when submitting a code received through this number
     */
    nonFTEU?: boolean
}
 
/**
 * Format of the expected security code used in MFA resend responses
 */
type SecurityCodeFormat = {
    length?: number,
    tooManyCodesSent?: boolean,
    tooManyCodesValidated?: boolean,
    securityCodeLocked?: boolean,
    securityCodeCooldown?: boolean,
    /**
     * Since iOS 26.4 a successfully validated code is acknowledged with status 409 and this flag set to true
     */
    valid?: boolean
}
 
/**
 * The expected response format for the device trust request
 * @see {@link ENDPOINTS.AUTH.PATH.TRUST}
 */
export type TrustResponse = {
    headers: {
        /**
         * TwoTrust token for future requests
         * @minLength 1
         */
        'x-apple-twosv-trust-token': string,
        /**
         * Session token to setup the account
         * @minLength 1
         */
        'x-apple-session-token': string,
    }
}
 
/**
 * The expected response format for the account setup request
 * @see {@link ENDPOINTS.SETUP.PATH.ACCOUNT_LOGIN}
 */
export type SetupResponse = {
    headers: {
        /**
         * Should hold the apple authentication
         * @minItems 1
         */
        'set-cookie': string[],
    }
    data: {
        dsInfo: {
            /**
             * Web access is necessary for the application
             */
            isWebAccessAllowed: true,
        }
        /**
         * Set, if the account requires a repair through the iCloud web frontend - if missing not necessary
         */
        isRepairNeeded?: boolean,
        /**
         * Set, if updated terms and conditions need to be accepted through the iCloud web frontend - if missing not necessary
         */
        termsUpdateNeeded?: boolean,
        /**
         * Holds the dynamic iCloud service URLs
         */
        webservices: {
            /**
             * Service for iCloud Photos
             */
            ckdatabasews: {
                /**
                 * @minLength 1
                 */
                url: string,
                /**
                 * Shows if additional PCS cookies are required - if missing not necessary
                 */
                pcsRequired?: boolean,
                /**
                 * Service needs to be active
                 */
                status: `active`
            }
        }
    }
}
 
/**
 * The expected response when trying to acquire PCS cookies
 */
export type PCSResponse = {
    headers: {
        /**
         * Should hold the PCS cookies
         */
        'set-cookie'?: string[],
    }
    data: {
        /**
         * Needs to be yes, otherwise this tool will not work
         */
        isWebAccessAllowed: true,
        /**
         * @minLength 1
         */
        message: string,
        /**
         * Status of the PCS request
         */
        status: `success` | `failure`
    }
}
 
/**
 * The expected response format for the photos setup request
 */
export type PhotosSetupResponse = {
    data: {
        /**
         * Should always be false
         */
        moreComing: false,
        /**
         * Sync token - currently not used
         * @minLength 1
         */
        syncToken: string,
        /**
         * The list of photos account zones - either primary or primary and shared
         * @minItems 0
         */
        zones: PhotosSetupResponseZone[]
    }
}
 
/**
 * Response zone object from photos setup response
 */
export type PhotosSetupResponseZone = {
    zoneID: {
        /**
         * @minLength 1
         */
        zoneName: string,
        /**
         * @minLength 1
         */
        ownerRecordName: string,
        /**
         * Fixed zone type
         */
        zoneType: `REGULAR_CUSTOM_ZONE`,
    }
    /**
     * Might be marked as deleted
     */
    deleted?: boolean
}
 
/**
 * A service error, as returned by the auth backend
 */
type ServiceError = {
    code?: string,
    message?: string,
}
 
/**
 * The expected response format for the MFA code submission
 * @see {@link ENDPOINTS.AUTH.PATH.MFA.DEVICE_ENTER}
 * @see {@link ENDPOINTS.AUTH.PATH.MFA.PHONE_ENTER}
 */
export type MFASubmitResponse = {
    /**
     * 204 (device) or 200 (phone) for an accepted code - since iOS 26.4 the backend responds with 409, the body indicates if the code was valid
     */
    status: 200 | 204 | 409,
    /**
     * Empty for status 204
     */
    data: {
        /**
         * Result of the code verification (since iOS 26.4)
         */
        securityCode?: {
            valid?: boolean,
        },
        /**
         * Errors, e.g. if the code was incorrect
         */
        service_errors?: ServiceError[],
    } | ``,
    headers: {
        /**
         * Updated session token, provided for a valid code since iOS 26.4
         * @minLength 1
         */
        'x-apple-session-token'?: string,
    },
}
 
/**
 * The expected response format for the escrow completion request
 * @see {@link ENDPOINTS.AUTH.PATH.ESCROW.COMPLETE}
 */
export type EscrowCompleteResponse = {
    headers: {
        /**
         * Updated session token
         * @minLength 1
         */
        'x-apple-session-token'?: string,
    },
}
 
/**
 * The expected response format for the logout request
 * @see {@link ENDPOINTS.SETUP.PATH.LOGOUT}
 */
export type LogoutResponse = {
    /**
     * 200 if the logout was successful, 421 if the session was no longer valid
     */
    status: 200 | 421,
}
 
/**
 * The expected response format for CloudKit record queries and operations
 * The records themselves are parsed defensively by the query parser
 * @see {@link ENDPOINTS.PHOTOS.PATH.QUERY}
 * @see {@link ENDPOINTS.PHOTOS.PATH.MODIFY}
 */
export type CloudKitRecordsResponse = {
    data: {
        records: any[],
        /**
         * Provided by queries, if more results are available - requests the next page when sent with the same query
         */
        continuationMarker?: string,
    },
}
 
/**
 * The expected response format for health check pings - a successful plain text response (healthchecks.io responds with 'OK')
 */
export type HealthCheckPingResponse = {
    /**
     * Any successful status (healthchecks.io responds with 200)
     * @minimum 200
     * @maximum 299
     */
    status: number,
    headers: {
        /**
         * The ping endpoint responds with plain text
         * @pattern ^text/plain
         */
        'content-type': string,
    },
    /**
     * The raw response body
     */
    text: string,
}