| 1 | import Foundation |
| 2 | #if os(macOS) |
| 3 | import Security |
| 4 | #endif |
| 5 | |
| 6 | /// Which APNs gateway this build's tokens belong to, read from the build |
| 7 | /// itself, never guessed: a token sent to the wrong gateway is refused with |
| 8 | /// BadDeviceToken and the server then deletes the registration. |
| 9 | /// |
| 10 | /// - iPhone, App Store and TestFlight: Apple strips the provisioning profile |
| 11 | /// from the bundle and the distribution signature carries |
| 12 | /// `aps-environment` production. No embedded profile means production. |
| 13 | /// - iPhone, an Xcode build on a device: the embedded development profile |
| 14 | /// says `aps-environment` development. |
| 15 | /// - iPhone Simulator: development (the Simulator registers with the sandbox). |
| 16 | /// - Mac: the signature's own `com.apple.developer.aps-environment`. A |
| 17 | /// Developer ID app is production; an ad hoc build has none and cannot |
| 18 | /// receive pushes, so it does not register. |
| 19 | /// |
| 20 | /// Equipo's APNs key is production only (CARLOS platform push, D1): a |
| 21 | /// development build registers honestly as development, and the server's |
| 22 | /// sender cannot reach it. |
| 23 | public enum PushEnvironment { |
| 24 | public static func resolve(platform: PushPlatform, simulator: Bool, embeddedProfile: Data?, signedEntitlement: String?) -> APNsEnvironment? { |
| 25 | switch platform { |
| 26 | case .macos: |
| 27 | return signedEntitlement.flatMap(APNsEnvironment.init(rawValue:)) |
| 28 | case .ios: |
| 29 | if simulator { return .development } |
| 30 | guard let embeddedProfile else { return .production } |
| 31 | return apsEnvironment(inProfile: embeddedProfile).flatMap(APNsEnvironment.init(rawValue:)) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | /// The `aps-environment` a provisioning profile grants: its property |
| 36 | /// list sits inside a CMS envelope. |
| 37 | public static func apsEnvironment(inProfile data: Data) -> String? { |
| 38 | guard let start = data.range(of: Data("<?xml".utf8)), |
| 39 | let end = data.range(of: Data("</plist>".utf8), in: start.lowerBound..<data.endIndex), |
| 40 | let plist = try? PropertyListSerialization.propertyList(from: data[start.lowerBound..<end.upperBound], format: nil) as? [String: Any], |
| 41 | let entitlements = plist["Entitlements"] as? [String: Any] else { return nil } |
| 42 | return (entitlements["aps-environment"] ?? entitlements["com.apple.developer.aps-environment"]) as? String |
| 43 | } |
| 44 | |
| 45 | /// This running build's gateway, or nil when it cannot receive pushes. |
| 46 | public static var current: APNsEnvironment? { |
| 47 | #if os(iOS) |
| 48 | #if targetEnvironment(simulator) |
| 49 | let simulator = true |
| 50 | #else |
| 51 | let simulator = false |
| 52 | #endif |
| 53 | let profile = Bundle.main.url(forResource: "embedded", withExtension: "mobileprovision").flatMap { try? Data(contentsOf: $0) } |
| 54 | return resolve(platform: .ios, simulator: simulator, embeddedProfile: profile, signedEntitlement: nil) |
| 55 | #else |
| 56 | var value: String? |
| 57 | if let task = SecTaskCreateFromSelf(nil) { |
| 58 | value = SecTaskCopyValueForEntitlement(task, "com.apple.developer.aps-environment" as CFString, nil) as? String |
| 59 | } |
| 60 | return resolve(platform: .macos, simulator: false, embeddedProfile: nil, signedEntitlement: value) |
| 61 | #endif |
| 62 | } |
| 63 | } |
| 64 | |