Modern Apps git

xtool.git

Public
git clone https://git.maproj.org/xtool.git
appstore-distribution ▾

xtool.git / Sources / XKit / DeveloperServices / OpenAPI / ASCKey.swift

ASCKey.swift110 lines · 3.1 KiBRaw
1import Foundation
2import Crypto
3
4public struct ASCKey: Sendable {
5 public var id: String
6 public var issuerID: String
7 public var pem: String
8
9 public init(id: String, issuerID: String, pem: String) {
10 self.id = id
11 self.issuerID = issuerID
12 self.pem = pem
13 }
14}
15
16actor ASCJWTGenerator {
17 // the duration for which we generate JWTs.
18 // ASC allows a maximum of 20 minutes. We use
19 // 15 minutes to account for clock drift.
20 private static let ttl: TimeInterval = 60 * 15
21
22 // the minimum remaining ttl for us to consider reusing a previous key.
23 // that is, we reuse the last JWT if it has at least [threshold] seconds
24 // left before it expires.
25 private static let tolerance: TimeInterval = 60 * 2
26
27 private static let encoder: JSONEncoder = {
28 let encoder = JSONEncoder()
29 encoder.dateEncodingStrategy = .secondsSince1970
30 encoder.outputFormatting = .sortedKeys
31 return encoder
32 }()
33
34 private var lastJWT: (jwt: String, renewAt: Date)?
35
36 private var parsedKey: P256.Signing.PrivateKey?
37
38 nonisolated let key: ASCKey
39 init(key: ASCKey) {
40 self.key = key
41 }
42
43 private struct Header: Encodable {
44 let alg = "ES256"
45 let typ = "JWT"
46 let kid: String
47 }
48
49 private struct Payload: Encodable {
50 let aud = "appstoreconnect-v1"
51 let iss: String
52 let iat: Date
53 let exp: Date
54 }
55
56 private func encode(_ value: some Encodable) throws -> String {
57 try ASCJWTGenerator.encoder.encode(value).base64URLEncodedString()
58 }
59
60 private func getKey() throws -> P256.Signing.PrivateKey {
61 if let parsedKey { return parsedKey }
62
63 let key = try P256.Signing.PrivateKey(pemRepresentation: key.pem)
64 self.parsedKey = key
65
66 return key
67 }
68
69 func generate() throws -> String {
70 if let lastJWT, lastJWT.renewAt > Date() {
71 return lastJWT.jwt
72 }
73
74 let encodedHeader = try encode(Header(kid: key.id))
75
76 let issuedAt = Date()
77 let expiry = issuedAt + ASCJWTGenerator.ttl
78 let renewAt = expiry - ASCJWTGenerator.tolerance
79 let encodedPayload = try encode(Payload(iss: key.issuerID, iat: issuedAt, exp: expiry))
80
81 let body = "\(encodedHeader).\(encodedPayload)"
82 let signature = try getKey()
83 .signature(for: Data(body.utf8))
84 .rawRepresentation
85 .base64URLEncodedString()
86
87 let jwt = "\(body).\(signature)"
88 lastJWT = (jwt, renewAt)
89 return jwt
90 }
91}
92
93extension Data {
94 fileprivate func base64URLEncodedString() -> String {
95 self
96 .base64EncodedString()
97 .replacingOccurrences(of: "+", with: "-")
98 .replacingOccurrences(of: "/", with: "_")
99 .replacingOccurrences(of: "=", with: "")
100 }
101}
102
103extension ASCKey {
104 /// Mints a short-lived App Store Connect JWT (ES256) for direct REST
105 /// calls (e.g. `xtool dist`), reusing the same signing logic as the
106 /// OpenAPI middleware.
107 public func makeJWT() async throws -> String {
108 try await ASCJWTGenerator(key: self).generate()
109 }
110}