PulseCanvas / app · pulsecanvas.ai / scripts / prepare-creators-icloud.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Validate a Developer ID profile before attaching iCloud signing entitlements.""" |
| 3 | from datetime import datetime, timezone |
| 4 | from pathlib import Path |
| 5 | import plistlib |
| 6 | import shutil |
| 7 | import subprocess |
| 8 | import sys |
| 9 | |
| 10 | CONTAINER = "iCloud.com.protomota.pulsecanvas" |
| 11 | TEAM = "22HV69L32T" |
| 12 | |
| 13 | |
| 14 | def entitlements(profile: dict, bundle_id: str) -> dict: |
| 15 | """Return only the capabilities needed by this app; fail before changing files.""" |
| 16 | expiry = profile.get("ExpirationDate") |
| 17 | if not isinstance(expiry, datetime) or expiry.replace(tzinfo=timezone.utc) <= datetime.now(timezone.utc): |
| 18 | raise ValueError("The Developer ID provisioning profile has expired.") |
| 19 | if profile.get("TeamIdentifier") != [TEAM] or profile.get("ProvisionsAllDevices") is not True: |
| 20 | raise ValueError("Use a Protomota Developer ID distribution profile, not a development or App Store profile.") |
| 21 | allowed = profile.get("Entitlements", {}) |
| 22 | app_key = next((key for key in ("com.apple.application-identifier", "application-identifier") if key in allowed), None) |
| 23 | if app_key is None or allowed[app_key] != TEAM + "." + bundle_id or allowed.get("get-task-allow") is True: |
| 24 | raise ValueError("The profile must match the exact release bundle ID and disable debugging.") |
| 25 | result = {app_key: allowed[app_key], "com.apple.developer.team-identifier": TEAM} |
| 26 | required = { |
| 27 | "com.apple.developer.ubiquity-container-identifiers": [CONTAINER], |
| 28 | "com.apple.developer.icloud-container-identifiers": [CONTAINER], |
| 29 | "com.apple.developer.icloud-services": ["CloudDocuments"], |
| 30 | } |
| 31 | for key, values in required.items(): |
| 32 | grant = allowed.get(key) |
| 33 | # Apple may authorize the service name with a wildcard. Sign only CloudDocuments. |
| 34 | services_wildcard = key == "com.apple.developer.icloud-services" and grant == "*" |
| 35 | if not services_wildcard and (not isinstance(grant, list) or not set(values).issubset(grant)): |
| 36 | raise ValueError("The profile is missing the PulseCanvas iCloud Documents capability: " + key) |
| 37 | result[key] = values |
| 38 | environment = allowed.get("com.apple.developer.icloud-container-environment") |
| 39 | if environment: |
| 40 | if environment != "Production" and "Production" not in environment: |
| 41 | raise ValueError("The distribution profile must permit the Production iCloud environment.") |
| 42 | result["com.apple.developer.icloud-container-environment"] = "Production" |
| 43 | return result |
| 44 | |
| 45 | |
| 46 | def main() -> None: |
| 47 | app, profile_path, output = map(Path, sys.argv[1:]) |
| 48 | info_path = app / "Contents/Info.plist" |
| 49 | info = plistlib.loads(info_path.read_bytes()) |
| 50 | decoded = subprocess.run(["security", "cms", "-D", "-i", str(profile_path)], check=True, capture_output=True).stdout |
| 51 | signing = entitlements(plistlib.loads(decoded), info["CFBundleIdentifier"]) |
| 52 | template = Path(__file__).resolve().parents[1] / "macOS/Info.plist" |
| 53 | info["NSUbiquitousContainers"] = plistlib.loads(template.read_bytes())["NSUbiquitousContainers"] |
| 54 | output.write_bytes(plistlib.dumps(signing)) |
| 55 | shutil.copyfile(profile_path, app / "Contents/embedded.provisionprofile") |
| 56 | info_path.write_bytes(plistlib.dumps(info)) |
| 57 | print("Validated Creators iCloud profile and prepared signing entitlements.") |
| 58 | |
| 59 | |
| 60 | if __name__ == "__main__": |
| 61 | main() |