Repository Public
1#!/usr/bin/env python32# Registers the macOS and iOS bundle ids of SP Message with the Associated Domains capability and creates the3# Developer ID provisioning profile for the Mac app, using the EXISTING App Store Connect API key.4# Idempotent: existing resources are reused; no certificate is ever created. Nothing secret is printed.5import base64, hashlib, os, subprocess, sys6sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))7import asc89HERE = os.path.dirname(os.path.abspath(__file__))10OUT = os.path.join(HERE, "..", "build", "SPMessage_Developer_ID.provisionprofile")11NAME = "SP Message Developer ID"1213def bundle(identifier, name, platform):14 s, d = asc.call("GET", f"/v1/bundleIds?filter[identifier]={identifier}&limit=20")15 ids = [b for b in d.get("data", []) if b["attributes"]["identifier"] == identifier]16 if ids: bid = ids[0]["id"]; print("bundle id exists:", identifier)17 else:18 s, d = asc.call("POST", "/v1/bundleIds", {"data": {"type": "bundleIds", "attributes": {"identifier": identifier, "name": name, "platform": platform}}})19 if s >= 300: sys.exit(f"cannot create bundle id {identifier}: HTTP {s} {str(d)[:300]}")20 bid = d["data"]["id"]; print("bundle id created:", identifier)21 s, d = asc.call("GET", f"/v1/bundleIds/{bid}/bundleIdCapabilities")22 if "ASSOCIATED_DOMAINS" not in [c["attributes"]["capabilityType"] for c in d.get("data", [])]:23 s, d = asc.call("POST", "/v1/bundleIdCapabilities", {"data": {"type": "bundleIdCapabilities", "attributes": {"capabilityType": "ASSOCIATED_DOMAINS"},24 "relationships": {"bundleId": {"data": {"type": "bundleIds", "id": bid}}}}})25 if s >= 300: sys.exit(f"cannot enable Associated Domains on {identifier}: HTTP {s}")26 print("capability enabled: ASSOCIATED_DOMAINS on", identifier)27 return bid2829mac = bundle("ai.spboucher.spmessage", "SP Message", "MAC_OS")30bundle("ai.spboucher.spmessage.ios", "SP Message iOS", "IOS")3132out = subprocess.run(["security", "find-certificate", "-c", "Developer ID Application", "-Z"], capture_output=True, text=True).stdout33local = [l.split(":")[1].strip() for l in out.splitlines() if l.startswith("SHA-1")]34if not local: sys.exit("no Developer ID Application certificate in the keychain")35s, d = asc.call("GET", "/v1/certificates?filter[certificateType]=DEVELOPER_ID_APPLICATION,DEVELOPER_ID_APPLICATION_G2&limit=100")36cert = next((c["id"] for c in d.get("data", []) if hashlib.sha1(base64.b64decode(c["attributes"]["certificateContent"])).hexdigest().upper() in local), None)37if not cert: sys.exit("the keychain's Developer ID certificate is not visible through the API")38s, d = asc.call("GET", "/v1/profiles?filter[profileType]=MAC_APP_DIRECT&limit=200")39active = [p for p in d.get("data", []) if p["attributes"]["name"] == NAME and p["attributes"]["profileState"] == "ACTIVE"]40if active: p = active[0]; print("profile exists:", NAME)41else:42 for stale in [x for x in d.get("data", []) if x["attributes"]["name"] == NAME]:43 asc.call("DELETE", f"/v1/profiles/{stale['id']}"); print("stale profile removed")44 s, d = asc.call("POST", "/v1/profiles", {"data": {"type": "profiles", "attributes": {"name": NAME, "profileType": "MAC_APP_DIRECT"}, "relationships": {45 "bundleId": {"data": {"type": "bundleIds", "id": mac}}, "certificates": {"data": [{"type": "certificates", "id": cert}]}}}})46 if s >= 300: sys.exit(f"cannot create the profile: HTTP {s} {str(d)[:300]}")47 p = d["data"]; print("profile created:", NAME)48os.makedirs(os.path.dirname(OUT), exist_ok=True)49open(OUT, "wb").write(base64.b64decode(p["attributes"]["profileContent"]))50print("written: build/SPMessage_Developer_ID.provisionprofile | uuid", p["attributes"]["uuid"], "| expires", p["attributes"]["expirationDate"][:10])51