-
-
Save czottmann/f49c6ca89368b7d66096221e320c06b8 to your computer and use it in GitHub Desktop.
A code snippet for detecting the TestFlight environment for a macOS app at runtime
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import Security | |
extension Bundle { | |
/// Returns whether the bundle was signed for TestFlight beta distribution by checking | |
/// the existence of a specific extension (marker OID) on the code signing certificate. | |
/// | |
/// This routine is inspired by the source code from ProcInfo, the underlying library | |
/// of the WhatsYourSign code signature checking tool developed by Objective-See. Initially, | |
/// it checked the common name but was changed to an extension check to make it more | |
/// future-proof. | |
/// | |
/// For more information, see the following references: | |
/// - https://github.com/objective-see/ProcInfo/blob/master/procInfo/Signing.m#L184-L247 | |
/// - https://gist.github.com/lukaskubanek/cbfcab29c0c93e0e9e0a16ab09586996#gistcomment-3993808 | |
internal var isTestFlight: Bool { | |
var status = noErr | |
var code: SecStaticCode? | |
status = SecStaticCodeCreateWithPath(bundleURL as CFURL, [], &code) | |
guard status == noErr, let code = code else { return false } | |
var requirement: SecRequirement? | |
status = SecRequirementCreateWithString( | |
"anchor apple generic and certificate leaf[field.1.2.840.113635.100.6.1.25.1]" as CFString, | |
[], // default | |
&requirement | |
) | |
guard status == noErr, let requirement = requirement else { return false } | |
status = SecStaticCodeCheckValidity( | |
code, | |
[], // default | |
requirement | |
) | |
return status == errSecSuccess | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment