All files / lib/resources network-types.ts

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

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 4151x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1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 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 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; rv:97.0) Gecko/20100101 Firefox/97.0`;
 
/**
 * 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`,
    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`,
};
 
/**
 * 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: {
                DEVICE_RESEND: `/verify/trusteddevice`,
                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`,
        },
    },
    /**
     * 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`,
            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/private`,
        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 = {
    /**
     * 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, // eslint-disable-line
        /**
         * Should hold the 'aasp' cookie
         * @minItems 1
         */
        'set-cookie': string[], // eslint-disable-line
    }
}
 
/**
 * 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 MFA resend request on a trusted device
 * @see {@link ENDPOINTS.AUTH.PATH.MFA.DEVICE_RESEND}
 */
export type ResendMFADeviceResponse = {
    data: {
         /**
         * Number of available trusted devices
         * @minimum 1
         */
        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,
    authenticationType: `hsa2`,
    hsa2Account: true,
    restrictedAccount: false,
}
 
/**
 * Object representing a trusted phone number used in MFA resend responses
 */
type TrustedPhoneNumber = {
    /**
     * @minimum 0
     */
    id: number,
    /**
     * @minLength 1
     */
    numberWithDialCode: string,
    /**
     * @pattern ^sms|voice$
     */
    pushMode: string,
    /**
     * @minLength 1
     */
    obfuscatedNumber: string,
    /**
     * @minLength 1
     * @maxLength 2
     */
    lastTwoDigits: string
}
 
/**
 * Format of the expected security code used in MFA resend responses
 */
type SecurityCodeFormat = {
    length: 6,
    tooManyCodesSent: false,
    tooManyCodesValidated: false,
    securityCodeLocked: false,
    securityCodeCooldown: false
}
 
/**
 * 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, // eslint-disable-line
        /**
         * Session token to setup the account
         * @minLength 1
         */
        'x-apple-session-token': string, // eslint-disable-line
    }
}
 
/**
 * The expected response format for the account setup request
 * @see {@link ENDPOINTS.SETUP.PATH.ACCOUNT}
 */
export type SetupResponse = {
    headers: {
        /**
         * Should hold the apple authentication
         * @minItems 1
         */
        'set-cookie': string[],  // eslint-disable-line
    }
    data: {
        dsInfo: {
            /**
             * Web access is necessary for the application
             */
            isWebAccessAllowed: true,
        }
        /**
         * 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
         * @minItems 2
         */
        'set-cookie': string[],  // eslint-disable-line
    }
    data: {
        /**
         * Needs to be yes, otherwise this tool will not work
         */
        isWebAccessAllowed: true,
        /**
         * Consent needs to be previously provided, not yet supported by this tool
         */
        isDeviceConsentedForPCS: true,
        /**
         * There is also the case of "Requested the device to upload cookies." - not sure though what to do in this case, as I'll be sending all available cookies
         */
        message: `Cookies attached.` | `Cookies already present.`,
        /**
         * Gives a unix timestamp when the PCS consents expires
         */
        deviceConsentForPCSExpiry: number,
        /**
         * Can also be "failure" not sure how to handle this though
         */
        status: `success`
    }
}
 
/**
 * 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 1
         */
        zones: PhotosSetupResponseZone[]
    }
}
 
/**
 * Response zone object from photos setup response
 */
type PhotosSetupResponseZone = {
    zoneID: {
        /**
         * @minLength 1
         * @pattern ^(PrimarySync|SharedSync(-[0-9A-F?]+)+|CMM(-[0-9A-F]+)+)$
         */
        zoneName: string,
        /**
         * @minLength 1
         */
        ownerRecordName: string,
        /**
         * Fixed zone type
         */
        zoneType: `REGULAR_CUSTOM_ZONE`,
    }
    /**
     * Might be marked as deleted
     */
    deleted?: boolean
}