Repository Public
1#!/usr/bin/env python32# Author: Simon-Pierre Boucher — contact@spboucher.ai3# File: Scripts/asc.py4# Description: Minimal App Store Connect API client (existing API key) used to register the bundle id, its Associated Domains capability and the Developer ID profile.5#6# The key id / issuer id / .p8 path are read from the environment or from the existing release tooling of the Mac;7# nothing secret is printed.8import base64, json, os, re, subprocess, sys, time, urllib.request, urllib.error, glob910def creds():11 kid, iss, path = os.environ.get("ASC_KEY_ID"), os.environ.get("ASC_ISSUER_ID"), os.environ.get("ASC_KEY_PATH")12 if not (kid and iss):13 ref = os.path.expanduser("~/Desktop/Cluster/apple/maclustr-ios-v2/app/scripts/testflight-archive-upload.sh")14 if os.path.exists(ref):15 s = open(ref).read()16 m1, m2 = re.search(r"-authenticationKeyID\s+(\S+)", s), re.search(r"-authenticationKeyIssuerID\s+([0-9a-fA-F-]{36})", s)17 if m1 and m2: kid, iss = m1.group(1).strip('"'), m2.group(1)18 if not path and kid:19 for d in ("~/.appstoreconnect/private_keys", "~/.private_keys", "~/private_keys"):20 p = os.path.expanduser(f"{d}/AuthKey_{kid}.p8")21 if os.path.exists(p): path = p; break22 if not (kid and iss and path): sys.exit("ERROR: App Store Connect API key not found (set ASC_KEY_ID, ASC_ISSUER_ID, ASC_KEY_PATH).")23 return kid, iss, path2425def b64(b): return base64.urlsafe_b64encode(b).rstrip(b"=")2627def token():28 kid, iss, path = creds()29 head = b64(json.dumps({"alg": "ES256", "kid": kid, "typ": "JWT"}).encode())30 body = b64(json.dumps({"iss": iss, "iat": int(time.time()), "exp": int(time.time()) + 900, "aud": "appstoreconnect-v1"}).encode())31 msg = head + b"." + body32 der = subprocess.run(["openssl", "dgst", "-sha256", "-sign", path], input=msg, capture_output=True, check=True).stdout33 # DER ECDSA signature → raw r||s (JOSE)34 i = 2 if der[1] < 0x80 else 2 + (der[1] & 0x7f)35 rl = der[i + 1]; r = der[i + 2:i + 2 + rl]; j = i + 2 + rl; sl = der[j + 1]; s = der[j + 2:j + 2 + sl]36 raw = r[-32:].rjust(32, b"\0") + s[-32:].rjust(32, b"\0")37 return (msg + b"." + b64(raw)).decode()3839def call(method, path, body=None):40 req = urllib.request.Request("https://api.appstoreconnect.apple.com" + path, method=method, data=json.dumps(body).encode() if body else None,41 headers={"Authorization": "Bearer " + token(), "Content-Type": "application/json"})42 try:43 with urllib.request.urlopen(req, timeout=60) as r:44 t = r.read(); return r.status, (json.loads(t) if t else {})45 except urllib.error.HTTPError as e:46 t = e.read()47 try: return e.code, json.loads(t)48 except Exception: return e.code, {"raw": t.decode(errors="replace")[:500]}4950if __name__ == "__main__":51 method, path = sys.argv[1], sys.argv[2]52 body = json.loads(sys.argv[3]) if len(sys.argv) > 3 else None53 status, data = call(method, path, body)54 print(status); print(json.dumps(data, indent=1)[:6000])55