Apple published a new method to authenticate against CloudKit, server-to-server. https://developer.apple.com/library/content/documentation/DataManagement/Conceptual/CloudKitWebServicesReference/SettingUpWebServices.html#//apple_ref/doc/uid/TP40015240-CH24-SW6
I tried to authenticate against CloudKit and this method. At first I generated the key pair and gave the public key to CloudKit, no problem so far.
I started to build the request header. According to the documentation it should look like this:
X-Apple-CloudKit-Request-KeyID: [keyID]
X-Apple-CloudKit-Request-ISO8601Date: [date]
X-Apple-CloudKit-Request-SignatureV1: [signature]
- [keyID], no problem. You can find this in the CloudKit dashboard.
- [Date], I think this should work: 2016-02-06T20:41:00Z
- [signature], here is the problem...
The documentation says:
The signature created in Step 1.
Step 1 says:
Concatenate the following parameters and separate them with colons.
[Current date]:[Request body]:[Web Service URL]
I asked myself "Why do I have to generate the key pair?".
But step 2 says:
Compute the ECDSA signature of this message with your private key.
Maybe they mean to sign the concatenated signature with the private key and put this into the header? Anyway I tried both...
My sample for this (unsigned) signature value looks like:
2016-02-06T20:41:00Z:YTdkNzAwYTllNjI1M2EyZTllNDNiZjVmYjg0MWFhMGRiMTE2MjI1NTYwNTA2YzQyODc4MjUwNTQ0YTE5YTg4Yw==:https://api.apple-cloudkit.com/database/1/[iCloud Container]/development/public/records/lookup
The request body value is SHA256 hashed and after that base64 encoded. My question is, I should concatenate with a ":" but the url and the date also contains ":". Is it correct? (I also tried to URL-Encode the URL and delete the ":" in the date).
At next I signed this signature string with ECDSA, put it into the header and send it. But I always get 401 "Authentication failed" back. To sign it, I used the ecdsa python module, with following commands:
from ecdsa import SigningKey
a = SigningKey.from_pem(open("path_to_pem_file").read())
b = "[date]:[base64(request_body)]:/database/1/iCloud....."
print a.sign(b).encode('hex')
Maybe the python module doesn't work correctly. But it can generate the right public key from the private key. So I hope the other functions also work.
Has anybody managed to authenticate against CloudKit with the server-to-server method? How does it work correctly?
Edit: Correct python version that works
from ecdsa import SigningKey
import ecdsa, base64, hashlib
a = SigningKey.from_pem(open("path_to_pem_file").read())
b = "[date]:[base64(sha256(request_body))]:/database/1/iCloud....."
signature = a.sign(b, hashfunc=hashlib.sha256, sigencode=ecdsa.util.sigencode_der)
signature = base64.b64encode(signature)
print signature #include this into the header
base64(sha256(request_body))
isbase64.b64encode(hashlib.sha256(request_body).hexdigest())
, right? Also, why does the ISO date time string have aZ
at the end? – Creephttps://api.apple-cloudkit.com
. This worked for me. – Creep