SPB Git

Repository Public

asc.py 2.6 KB 50 lines python

1#!/usr/bin/env python32"""Minimal App Store Connect API client for MacLustr Command Center.34Credentials are never stored in the repository: set ASC_KEY_ID, ASC_ISSUER_ID and (optionally) ASC_KEY_PATH;5the .p8 is looked up in ~/.appstoreconnect/private_keys by default. Nothing secret is printed.67  asc.py get  '/v1/bundleIds?filter[identifier]=ai.spboucher.clustr'      read-only query8  asc.py post <path> <json-file>                                           (used by provision.py only)9"""10import base64, json, os, subprocess, sys, time, urllib.request, urllib.error1112def creds():13    kid, iss = os.environ.get("ASC_KEY_ID"), os.environ.get("ASC_ISSUER_ID")14    path = os.environ.get("ASC_KEY_PATH") or (kid and os.path.expanduser(f"~/.appstoreconnect/private_keys/AuthKey_{kid}.p8"))15    if not (kid and iss and path and os.path.exists(path)):16        sys.exit("ASC credentials missing: set ASC_KEY_ID, ASC_ISSUER_ID (and ASC_KEY_PATH).")17    return kid, iss, path1819def _b64(b): return base64.urlsafe_b64encode(b).rstrip(b"=")2021def token():22    kid, iss, path = creds()23    now = int(time.time())24    msg = _b64(json.dumps({"alg": "ES256", "kid": kid, "typ": "JWT"}).encode()) + b"." + \25          _b64(json.dumps({"iss": iss, "iat": now, "exp": now + 600, "aud": "appstoreconnect-v1"}).encode())26    der = subprocess.run(["openssl", "dgst", "-sha256", "-sign", path], input=msg, capture_output=True, check=True).stdout27    i = 2 if der[1] < 0x80 else 2 + (der[1] & 0x7F)          # DER ECDSA → raw r||s28    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]29    return (msg + b"." + _b64(r[-32:].rjust(32, b"\0") + s[-32:].rjust(32, b"\0"))).decode()3031def call(method, path, body=None):32    req = urllib.request.Request("https://api.appstoreconnect.apple.com" + path, method=method,33                                 data=json.dumps(body).encode() if body is not None else None,34                                 headers={"Authorization": "Bearer " + token(), "Content-Type": "application/json"})35    try:36        with urllib.request.urlopen(req, timeout=60) as r:37            t = r.read()38            return r.status, (json.loads(t) if t else {})39    except urllib.error.HTTPError as e:40        t = e.read()41        try: return e.code, json.loads(t)42        except Exception: return e.code, {"raw": t.decode(errors="replace")[:500]}4344if __name__ == "__main__":45    if len(sys.argv) < 3 or sys.argv[1] not in ("get", "post"):46        sys.exit(__doc__)47    body = json.load(open(sys.argv[3])) if sys.argv[1] == "post" else None48    status, data = call(sys.argv[1].upper(), sys.argv[2], body)49    print(status); print(json.dumps(data, indent=1)[:6000])50
Files main
Menu

Theme