{-# LANGUAGE NamedFieldPuns #-}{-# LANGUAGE OverloadedStrings #-}{- |Module : Network.HStratus.Internal.EndpointsCopyright : (c) 2025 Tim EmiolaMaintainer : Tim Emiola <adetokunbo@emio.la>SPDX-License-Identifier: BSD3-}module Network.HStratus.Internal.Endpoints ( -- * Types Endpoints (..) , Realm (..) -- * Region selection , realmEndpoints -- * Request builders , signinInitBase , signinCompleteBase , validateBase , accountLoginBase , twoSvTrust , twoFaOptionsBase , verifySecurityCodeReq , validateVerification , sendVerification , listDevices -- * Request modifiers , extendPath , toPut , withHeaders , withBody , withAcceptJson , withWidgetKey , withAppleOauthHeaders -- * Header helpers , homeHeaders , icloudBrowserHeaders -- * Shared service utilities , icloudHome , stripTrailingSlash , lookupWebservice )whereimport Control.Exception (throwIO)import Data.ByteString (ByteString)import qualified Data.ByteString.Char8 as BS8import qualified Data.ByteString.Lazy as LBSimport Data.CaseInsensitive (mk)import Data.Map.Strict (Map)import qualified Data.Map.Strict as Mapimport Data.String.Conv (toS)import Data.Text (Text)import Network.HStratus.Internal.HttpErrors (AuthError (..))import Network.HStratus.Internal.Session (Webservice (..))import Network.HTTP.Client ( Request (..) , RequestBody (..) , defaultRequest , parseRequest )import Network.HTTP.Types ( Header , HeaderName , RequestHeaders , hAccept , hReferer , hUserAgent , methodGet , methodPost , methodPut )-- | @RequestHeaders@ that include the @Endpoint@ @home@homeHeaders :: Endpoints -> RequestHeadershomeHeaders ep = [(hOrigin, epHome ep), (hReferer, epHome ep <> "/")]commonHeaders :: Endpoints -> RequestHeaderscommonHeaders ep = userAgent : homeHeaders ep-- | Construct a new @Request@ with that path changed by adding a suffixextendPath :: Request -> ByteString -> RequestextendPath req suffix = req{path = path req <> suffix}-- | Construct a new @Request@ with the method changed to @GET@toGet :: Request -> RequesttoGet req = req{method = methodGet}-- | Construct a new @Request@ with the method changed to @PUT@toPut :: Request -> RequesttoPut req = req{method = methodPut}data Endpoints = Endpoints { epHome :: !ByteString -- ^ home origin, e.g. @https://www.icloud.com@; used in @Origin@ and @Referer@ headers , epAuth :: !Request -- ^ base request for the authentication endpoint (@idmsa.apple.com@) , epSetup :: !Request -- ^ base request for the setup\/account endpoint (@setup.icloud.com@) , epWidgetKey :: !ByteString -- ^ value sent as @X-Apple-Widget-Key@ and @X-Apple-OAuth-Client-Id@; override if Apple rotates it }data Realm = China | Usual deriving (Eq, Show)realmEndpoints :: Realm -> EndpointsrealmEndpoints China = chinaEndpointsrealmEndpoints Usual = usualEndpoints-- | The iCloud home origin used in @Origin@ and @Referer@ headers.icloudHome :: ByteStringicloudHome = "https://www.icloud.com"usualEndpoints :: EndpointsusualEndpoints = Endpoints { epHome = icloudHome , epAuth = authReq , epSetup = setupReq , epWidgetKey = iCloudKey }chinaEndpoints :: EndpointschinaEndpoints = Endpoints { epHome = "https://www.icloud.com.cn" , epAuth = authReq -- idmsa.apple.com is Apple's global SRP endpoint; not region-specific , epSetup = setupReq{host = "setup.icloud.com.cn"} , epWidgetKey = iCloudKey }apiRequest :: RequestapiRequest = defaultRequest { secure = True , port = 443 , method = methodPost }authReq :: RequestauthReq = apiRequest{host = "idmsa.apple.com", path = "/appleauth/auth"}setupReq :: RequestsetupReq = apiRequest{host = "setup.icloud.com", path = "/setup/ws/1"}appleOauthHeaders :: ByteString -> [Header]appleOauthHeaders key = [ ("X-Apple-OAuth-Client-Id", key) , ("X-Apple-OAuth-Client-Type", "firstPartyAuth") , ("X-Apple-OAuth-Redirect-URI", "https://www.icloud.com") , ("X-Apple-OAuth-Require-Grant-Code", "true") , ("X-Apple-OAuth-Response-Mode", "web_message") , ("X-Apple-OAuth-Response-Type", "code") , ("X-Apple-Widget-Key", key) ]iCloudKey :: ByteStringiCloudKey = "d39ba9916b7251055b22c7f910e2ea796ee65e98b2ddecea8f5dde8d9d1a815d"browserAgent :: ByteStringbrowserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"userAgent :: HeaderuserAgent = (hUserAgent, browserAgent)acceptJson :: HeaderacceptJson = (hAccept, "application/json")-- | build the basic @Request@ to make the initiate signinsigninInitBase :: Endpoints -> RequestsigninInitBase = (`extendPath` "/signin/init") . epAuth-- | build the basic @Request@ to complete signinsigninCompleteBase :: Endpoints -> RequestsigninCompleteBase = let withQuery x = x{queryString = "isRememberMeEnabled=true"} in withQuery . (`extendPath` "/signin/complete") . epAuth-- | build the basic @Request@ to that validates user credentialsvalidateBase :: Endpoints -> RequestvalidateBase ep = withHeaders (commonHeaders ep) $ (`extendPath` "/validate") $ epSetup ep-- | build the basic @Request@ to that performs loginaccountLoginBase :: Endpoints -> RequestaccountLoginBase = (`extendPath` "/accountLogin") . epSetup-- | build the basic @Request@ to that makes the saved credentials trustedtwoSvTrust :: Endpoints -> RequesttwoSvTrust = (`extendPath` "/2sv/trust") . toGet . epAuth-- | build the @Request@ to fetch the 2FA options after the 409 from signin/completetwoFaOptionsBase :: Endpoints -> RequesttwoFaOptionsBase = toGet . epAuth-- | build the @Request@ to that verifies makes a security codeverifySecurityCodeReq :: Text -> Endpoints -> RequestverifySecurityCodeReq codeType = (`extendPath` ("/verify/" <> toS codeType <> "/securitycode")) . epAuthvalidateVerification :: Endpoints -> RequestvalidateVerification = (`extendPath` "/validateVerificationCode") . epSetupsendVerification :: Endpoints -> RequestsendVerification = (`extendPath` "/sendVerificationCode") . epSetuplistDevices :: Endpoints -> RequestlistDevices = (`extendPath` "/listDevices") . toGet . epSetupwithHeaders :: RequestHeaders -> Request -> RequestwithHeaders newHeaders req = req{requestHeaders = newHeaders <> requestHeaders req}withBody :: LBS.LazyByteString -> Request -> RequestwithBody b req = req{requestBody = RequestBodyLBS b}hOrigin :: HeaderNamehOrigin = mk "Origin"withAcceptJson :: RequestHeaders -> RequestHeaderswithAcceptJson = (acceptJson :)withWidgetKey :: ByteString -> RequestHeaders -> RequestHeaderswithWidgetKey key = (("X-Apple-Widget-Key", key) :)withAppleOauthHeaders :: ByteString -> RequestHeaders -> RequestHeaderswithAppleOauthHeaders key = (appleOauthHeaders key <>)-- | Standard browser-style headers sent with every iCloud service request.icloudBrowserHeaders :: RequestHeadersicloudBrowserHeaders = [ acceptJson , userAgent , (hOrigin, icloudHome) , (hReferer, icloudHome <> "/") ]-- | Strip a trailing @/@ from a strict 'ByteString' path.stripTrailingSlash :: ByteString -> ByteStringstripTrailingSlash bs | not (BS8.null bs) && BS8.last bs == '/' = BS8.init bs | otherwise = bs{- | Look up a service URL by key in the webservices map and parse it into a'Request'. Fails with an informative message if the key is absent.-}lookupWebservice :: Text -> Map Text Webservice -> IO RequestlookupWebservice key ws = case Map.lookup key ws of Nothing -> throwIO $ WebserviceNotFound key Just (Webservice _ (Just "inactive")) -> throwIO $ WebserviceNotFound key Just (Webservice url _) -> parseRequest (toS url)