Repository Public
1#!/usr/bin/env python32# Author: Simon-Pierre Boucher — contact@spboucher.ai3# File: Scripts/provision.py4# Description: Registers SP Wallet's bundle identifiers (release and development variants), the Associated Domains capability of the app, and their Developer ID provisioning profiles — with the EXISTING App Store Connect API key and Developer ID certificate.5#6# Idempotent: existing resources are reused; no certificate is ever created. Nothing secret is printed.7import base64, hashlib, os, subprocess, sys8sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))9import asc1011OUT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "Build", "profiles")12# identifier, display name, profile name, file, capabilities13TARGETS = [14 ("ai.spboucher.spwallet", "SP Wallet", "SP Wallet Developer ID", "SPWallet.provisionprofile", ["ASSOCIATED_DOMAINS"]),15 ("ai.spboucher.spwallet.dev", "SP Wallet Dev", "SP Wallet Dev Developer ID", "SPWalletDev.provisionprofile", []),16]1718out = subprocess.run(["security", "find-certificate", "-c", "Developer ID Application", "-Z"], capture_output=True, text=True).stdout19local = [l.split(":")[1].strip() for l in out.splitlines() if l.startswith("SHA-1")]20if not local: sys.exit("no Developer ID Application certificate in the keychain")21s, d = asc.call("GET", "/v1/certificates?filter[certificateType]=DEVELOPER_ID_APPLICATION,DEVELOPER_ID_APPLICATION_G2&limit=100")22cert = next((c["id"] for c in d.get("data", []) if hashlib.sha1(base64.b64decode(c["attributes"]["certificateContent"])).hexdigest().upper() in local), None)23if not cert: sys.exit("the keychain's Developer ID certificate is not visible through the API")24s, allp = asc.call("GET", "/v1/profiles?filter[profileType]=MAC_APP_DIRECT&limit=200")25os.makedirs(OUT, exist_ok=True)2627for BID, DISPLAY, NAME, FILE, CAPS in TARGETS:28 s, d = asc.call("GET", f"/v1/bundleIds?filter[identifier]={BID}&limit=20")29 ids = [b for b in d.get("data", []) if b["attributes"]["identifier"] == BID]30 if ids: bid = ids[0]["id"]; print("bundle id exists:", BID)31 else:32 s, d = asc.call("POST", "/v1/bundleIds", {"data": {"type": "bundleIds", "attributes": {"identifier": BID, "name": DISPLAY, "platform": "MAC_OS"}}})33 if s >= 300: sys.exit(f"cannot create bundle id {BID}: HTTP {s}")34 bid = d["data"]["id"]; print("bundle id created:", BID)35 s, d = asc.call("GET", f"/v1/bundleIds/{bid}/bundleIdCapabilities")36 have = [c["attributes"]["capabilityType"] for c in d.get("data", [])]37 changed = False38 for cap in CAPS:39 if cap in have: continue40 s, d = asc.call("POST", "/v1/bundleIdCapabilities", {"data": {"type": "bundleIdCapabilities", "attributes": {"capabilityType": cap}, "relationships": {"bundleId": {"data": {"type": "bundleIds", "id": bid}}}}})41 if s >= 300: sys.exit(f"cannot enable {cap} on {BID}: HTTP {s}")42 print("capability enabled:", cap); changed = True43 if changed: s, allp = asc.call("GET", "/v1/profiles?filter[profileType]=MAC_APP_DIRECT&limit=200")44 mine = [p for p in allp.get("data", []) if p["attributes"]["name"] == NAME]45 active = [p for p in mine if p["attributes"]["profileState"] == "ACTIVE"]46 if active: p = active[0]; print("profile exists:", NAME)47 else:48 # a capability change on the App ID invalidates its profiles: the stale one is replaced49 for stale in mine: asc.call("DELETE", f"/v1/profiles/{stale['id']}"); print("stale profile removed:", stale["attributes"]["profileState"])50 s, d = asc.call("POST", "/v1/profiles", {"data": {"type": "profiles", "attributes": {"name": NAME, "profileType": "MAC_APP_DIRECT"}, "relationships": {"bundleId": {"data": {"type": "bundleIds", "id": bid}}, "certificates": {"data": [{"type": "certificates", "id": cert}]}}}})51 if s >= 300: sys.exit(f"cannot create the profile {NAME}: HTTP {s}")52 p = d["data"]; print("profile created:", NAME)53 path = os.path.join(OUT, FILE)54 open(path, "wb").write(base64.b64decode(p["attributes"]["profileContent"]))55 print(" written:", os.path.relpath(path), "| uuid", p["attributes"]["uuid"], "| expires", p["attributes"]["expirationDate"][:10])56 for dest in ("~/Library/Developer/Xcode/UserData/Provisioning Profiles", "~/Library/MobileDevice/Provisioning Profiles"):57 dd = os.path.expanduser(dest); os.makedirs(dd, exist_ok=True)58 open(os.path.join(dd, p["attributes"]["uuid"] + ".provisionprofile"), "wb").write(base64.b64decode(p["attributes"]["profileContent"]))59