2019-05-24 11:29:27 -07:00
//
2019-07-01 15:19:22 -07:00
// A L T D e v i c e M a n a g e r + I n s t a l l a t i o n . s w i f t
2019-05-24 11:29:27 -07:00
// A l t S e r v e r
//
2019-07-01 15:19:22 -07:00
// C r e a t e d b y R i l e y T e s t u t o n 7 / 1 / 1 9 .
2019-05-24 11:29:27 -07:00
// C o p y r i g h t © 2 0 1 9 R i l e y T e s t u t . A l l r i g h t s r e s e r v e d .
//
import Cocoa
2019-07-01 15:19:22 -07:00
import UserNotifications
2019-11-18 14:17:57 -08:00
import ObjectiveC
2019-05-24 11:29:27 -07:00
2022-11-21 17:50:42 -06:00
#if STAGING
let altstoreSourceURL = URL ( string : " https://f000.backblazeb2.com/file/altstore-staging/apps-staging.json " ) !
#else
let altstoreSourceURL = URL ( string : " https://apps.altstore.io " ) !
#endif
#if BETA
let altstoreBundleID = " com.rileytestut.AltStore.Beta "
#else
let altstoreBundleID = " com.rileytestut.AltStore "
#endif
2020-05-19 18:30:53 -07:00
2022-11-21 17:50:42 -06:00
private let appGroupsSemaphore = DispatchSemaphore ( value : 1 )
2021-05-20 13:11:54 -07:00
private let developerDiskManager = DeveloperDiskManager ( )
2022-11-21 17:50:42 -06:00
private let session : URLSession = {
let configuration = URLSessionConfiguration . default
configuration . requestCachePolicy = . reloadIgnoringLocalCacheData
configuration . urlCache = nil
let session = URLSession ( configuration : configuration )
return session
} ( )
extension OperationError
{
enum Code : Int , ALTErrorCode
{
typealias Error = OperationError
case cancelled
case noTeam
case missingPrivateKey
case missingCertificate
}
static let cancelled = OperationError ( code : . cancelled )
static let noTeam = OperationError ( code : . noTeam )
static let missingPrivateKey = OperationError ( code : . missingPrivateKey )
static let missingCertificate = OperationError ( code : . missingCertificate )
}
struct OperationError : ALTLocalizedError
2019-05-29 15:50:53 -07:00
{
2022-11-21 17:50:42 -06:00
var code : Code
var errorTitle : String ?
var errorFailure : String ?
2023-01-24 13:56:41 -06:00
var errorFailureReason : String {
2022-11-21 17:50:42 -06:00
switch self . code
2019-05-29 15:50:53 -07:00
{
2019-09-14 11:28:57 -07:00
case . cancelled : return NSLocalizedString ( " The operation was cancelled. " , comment : " " )
2023-01-24 13:56:41 -06:00
case . noTeam : return NSLocalizedString ( " You are not a member of any developer teams. " , comment : " " )
case . missingPrivateKey : return NSLocalizedString ( " The developer certificate's private key could not be found. " , comment : " " )
case . missingCertificate : return NSLocalizedString ( " The developer certificate could not be found. " , comment : " " )
2021-10-04 15:36:16 -07:00
}
}
2019-05-29 15:50:53 -07:00
}
2019-05-24 11:29:27 -07:00
2022-11-21 17:50:42 -06:00
private extension ALTDeviceManager
{
struct Source : Decodable
{
struct App : Decodable
{
struct Version : Decodable
{
var version : String
var downloadURL : URL
var minimumOSVersion : OperatingSystemVersion ? {
return self . minOSVersion . map { OperatingSystemVersion ( string : $0 ) }
}
private var minOSVersion : String ?
}
var name : String
var bundleIdentifier : String
var versions : [ Version ] ?
}
var name : String
var identifier : String
var apps : [ App ]
}
}
2019-07-01 15:19:22 -07:00
extension ALTDeviceManager
2019-05-29 15:50:53 -07:00
{
2022-11-22 13:12:10 -06:00
func installApplication ( at ipaFileURL : URL ? , to altDevice : ALTDevice , appleID : String , password : String , completion : @ escaping ( Result < ALTApplication , Error > ) -> Void )
2019-05-29 15:50:53 -07:00
{
2019-06-25 13:34:12 -07:00
let destinationDirectoryURL = FileManager . default . temporaryDirectory . appendingPathComponent ( UUID ( ) . uuidString )
2022-11-22 13:12:10 -06:00
var appName = ipaFileURL ? . deletingPathExtension ( ) . lastPathComponent ? ? NSLocalizedString ( " AltStore " , comment : " " )
2021-06-04 13:57:40 -07:00
2023-01-24 13:56:41 -06:00
func finish ( _ result : Result < ALTApplication , Error > , failure : String ? = nil )
2019-05-29 15:50:53 -07:00
{
DispatchQueue . main . async {
2021-06-04 13:57:40 -07:00
switch result
{
case . success ( let app ) : completion ( . success ( app ) )
case . failure ( var error as NSError ) :
2023-01-24 13:56:41 -06:00
error = error . withLocalizedTitle ( String ( format : NSLocalizedString ( " %@ could not be installed onto %@. " , comment : " " ) , appName , altDevice . name ) )
if let failure , error . localizedFailure = = nil
2021-06-04 13:57:40 -07:00
{
2023-01-24 13:56:41 -06:00
error = error . withLocalizedFailure ( failure )
2021-06-04 13:57:40 -07:00
}
completion ( . failure ( error ) )
}
2019-05-29 15:50:53 -07:00
}
2019-06-25 13:34:12 -07:00
try ? FileManager . default . removeItem ( at : destinationDirectoryURL )
}
2019-11-18 14:17:57 -08:00
AnisetteDataManager . shared . requestAnisetteData { ( result ) in
2019-05-29 15:50:53 -07:00
do
{
2019-11-18 14:17:57 -08:00
let anisetteData = try result . get ( )
2019-07-01 15:19:22 -07:00
2019-11-18 14:17:57 -08:00
self . authenticate ( appleID : appleID , password : password , anisetteData : anisetteData ) { ( result ) in
2019-05-29 15:50:53 -07:00
do
{
2019-11-18 14:17:57 -08:00
let ( account , session ) = try result . get ( )
2019-05-29 15:50:53 -07:00
2019-11-18 14:17:57 -08:00
self . fetchTeam ( for : account , session : session ) { ( result ) in
2019-05-29 15:50:53 -07:00
do
{
2019-11-18 14:17:57 -08:00
let team = try result . get ( )
2019-05-29 15:50:53 -07:00
2021-05-20 13:11:54 -07:00
self . register ( altDevice , team : team , session : session ) { ( result ) in
2019-05-29 15:50:53 -07:00
do
{
2019-11-18 14:17:57 -08:00
let device = try result . get ( )
2021-05-20 13:11:54 -07:00
device . osVersion = altDevice . osVersion
2019-05-29 15:50:53 -07:00
2019-11-18 14:17:57 -08:00
self . fetchCertificate ( for : team , session : session ) { ( result ) in
2019-05-29 15:50:53 -07:00
do
{
2019-11-18 14:17:57 -08:00
let certificate = try result . get ( )
2019-06-26 17:05:52 -07:00
2022-11-22 13:12:10 -06:00
if ipaFileURL = = nil
2020-11-11 17:25:16 -08:00
{
// S h o w a l e r t b e f o r e d o w n l o a d i n g r e m o t e . i p a .
2022-11-22 13:20:14 -06:00
self . showInstallationAlert ( appName : NSLocalizedString ( " AltStore " , comment : " " ) , deviceName : altDevice . name )
2020-11-11 17:25:16 -08:00
}
2021-05-20 13:11:54 -07:00
self . prepare ( device ) { ( result ) in
switch result
2019-05-29 15:50:53 -07:00
{
2021-05-20 13:11:54 -07:00
case . failure ( let error ) :
print ( " Failed to install DeveloperDiskImage.dmg to \( device ) . " , error )
fallthrough // C o n t i n u e i n s t a l l i n g a p p e v e n i f w e c o u l d n ' t i n s t a l l D e v e l o p e r d i s k i m a g e .
case . success :
2022-11-22 13:12:10 -06:00
self . downloadApp ( from : ipaFileURL , for : altDevice ) { ( result ) in
2019-06-25 13:34:12 -07:00
do
{
2021-05-20 13:11:54 -07:00
let fileURL = try result . get ( )
try FileManager . default . createDirectory ( at : destinationDirectoryURL , withIntermediateDirectories : true , attributes : nil )
let appBundleURL = try FileManager . default . unzipAppBundle ( at : fileURL , toDirectory : destinationDirectoryURL )
guard let application = ALTApplication ( fileURL : appBundleURL ) else { throw ALTError ( . invalidApp ) }
2022-11-22 13:12:10 -06:00
if ipaFileURL != nil
2021-05-20 13:11:54 -07:00
{
// S h o w a l e r t a f t e r " d o w n l o a d i n g " l o c a l . i p a .
2022-11-22 13:20:14 -06:00
self . showInstallationAlert ( appName : application . name , deviceName : altDevice . name )
2021-05-20 13:11:54 -07:00
}
2019-06-25 13:34:12 -07:00
2021-05-20 13:11:54 -07:00
appName = application . name
// R e f r e s h a n i s e t t e d a t a t o p r e v e n t s e s s i o n t i m e o u t s .
AnisetteDataManager . shared . requestAnisetteData { ( result ) in
2019-06-26 17:05:52 -07:00
do
{
2021-05-20 13:11:54 -07:00
let anisetteData = try result . get ( )
session . anisetteData = anisetteData
2019-06-26 17:05:52 -07:00
2021-05-20 13:11:54 -07:00
self . prepareAllProvisioningProfiles ( for : application , device : device , team : team , session : session ) { ( result ) in
do
{
let profiles = try result . get ( )
self . install ( application , to : device , team : team , certificate : certificate , profiles : profiles ) { ( result ) in
2023-01-24 13:56:41 -06:00
finish ( result . map { application } )
2021-05-20 13:11:54 -07:00
}
}
catch
{
2023-01-24 13:56:41 -06:00
finish ( . failure ( error ) , failure : NSLocalizedString ( " AltServer could not fetch new provisioning profiles. " , comment : " " ) )
2021-05-20 13:11:54 -07:00
}
2019-06-26 17:05:52 -07:00
}
}
catch
{
2023-01-24 13:56:41 -06:00
finish ( . failure ( error ) )
2019-06-26 17:05:52 -07:00
}
2019-06-25 13:34:12 -07:00
}
}
catch
{
2023-01-24 13:56:41 -06:00
let failure = String ( format : NSLocalizedString ( " %@ could not be downloaded. " , comment : " " ) , appName )
finish ( . failure ( error ) , failure : failure )
2019-06-25 13:34:12 -07:00
}
}
2019-05-29 15:50:53 -07:00
}
}
}
catch
{
2023-01-24 13:56:41 -06:00
finish ( . failure ( error ) , failure : NSLocalizedString ( " A valid signing certificate could not be created. " , comment : " " ) )
2019-05-29 15:50:53 -07:00
}
}
}
catch
{
2023-01-24 13:56:41 -06:00
finish ( . failure ( error ) , failure : NSLocalizedString ( " Your device could not be registered with your development team. " , comment : " " ) )
2019-05-29 15:50:53 -07:00
}
}
}
catch
{
2023-01-24 13:56:41 -06:00
finish ( . failure ( error ) )
2019-05-29 15:50:53 -07:00
}
}
}
catch
{
2023-01-24 13:56:41 -06:00
finish ( . failure ( error ) , failure : NSLocalizedString ( " AltServer could not sign in with your Apple ID. " , comment : " " ) )
2019-05-29 15:50:53 -07:00
}
}
}
catch
{
2023-01-24 13:56:41 -06:00
finish ( . failure ( error ) )
2019-05-29 15:50:53 -07:00
}
}
}
2020-11-11 17:25:16 -08:00
}
2021-05-20 13:11:54 -07:00
extension ALTDeviceManager
{
func prepare ( _ device : ALTDevice , completionHandler : @ escaping ( Result < Void , Error > ) -> Void )
{
ALTDeviceManager . shared . isDeveloperDiskImageMounted ( for : device ) { ( isMounted , error ) in
switch ( isMounted , error )
{
case ( _ , let error ? ) : return completionHandler ( . failure ( error ) )
case ( true , _ ) : return completionHandler ( . success ( ( ) ) )
case ( false , _ ) :
developerDiskManager . downloadDeveloperDisk ( for : device ) { ( result ) in
switch result
{
case . failure ( let error ) : completionHandler ( . failure ( error ) )
case . success ( ( let diskFileURL , let signatureFileURL ) ) :
ALTDeviceManager . shared . installDeveloperDiskImage ( at : diskFileURL , signatureURL : signatureFileURL , to : device ) { ( success , error ) in
switch Result ( success , error )
{
2022-03-01 16:03:03 -08:00
case . failure ( let error as ALTServerError ) where error . code = = . incompatibleDeveloperDisk :
developerDiskManager . setDeveloperDiskCompatible ( false , with : device )
completionHandler ( . failure ( error ) )
case . failure ( let error ) :
// D o n ' t m a r k d e v e l o p e r d i s k a s i n c o m p a t i b l e b e c a u s e i t p r o b a b l y f a i l e d f o r a d i f f e r e n t r e a s o n .
completionHandler ( . failure ( error ) )
case . success :
developerDiskManager . setDeveloperDiskCompatible ( true , with : device )
completionHandler ( . success ( ( ) ) )
2021-05-20 13:11:54 -07:00
}
}
}
}
}
}
}
}
2020-11-11 17:25:16 -08:00
private extension ALTDeviceManager
{
2022-11-22 13:12:10 -06:00
func downloadApp ( from url : URL ? , for device : ALTDevice , completionHandler : @ escaping ( Result < URL , Error > ) -> Void )
2019-06-26 17:05:52 -07:00
{
2022-11-22 13:12:10 -06:00
if let url , url . isFileURL
{
return completionHandler ( . success ( url ) )
}
2020-11-11 17:25:16 -08:00
2022-11-21 17:50:42 -06:00
self . fetchAltStoreDownloadURL ( for : device ) { result in
switch result
{
case . failure ( let error ) : completionHandler ( . failure ( error ) )
case . success ( let url ) :
let downloadTask = URLSession . shared . downloadTask ( with : url ) { ( fileURL , response , error ) in
do
{
if let response = response as ? HTTPURLResponse
{
guard response . statusCode != 404 else { throw CocoaError ( . fileNoSuchFile , userInfo : [ NSURLErrorKey : url ] ) }
}
let ( fileURL , _ ) = try Result ( ( fileURL , response ) , error ) . get ( )
completionHandler ( . success ( fileURL ) )
do { try FileManager . default . removeItem ( at : fileURL ) }
catch { print ( " Failed to remove downloaded .ipa. " , error ) }
}
catch
{
completionHandler ( . failure ( error ) )
}
}
downloadTask . resume ( )
}
}
}
func fetchAltStoreDownloadURL ( for device : ALTDevice , completion : @ escaping ( Result < URL , Error > ) -> Void )
{
let dataTask = session . dataTask ( with : altstoreSourceURL ) { ( data , response , error ) in
2019-06-26 17:05:52 -07:00
do
{
2022-11-21 17:50:42 -06:00
if let response = response as ? HTTPURLResponse
{
guard response . statusCode != 404 else { throw CocoaError ( . fileNoSuchFile , userInfo : [ NSURLErrorKey : altstoreSourceURL ] ) }
}
let ( data , _ ) = try Result ( ( data , response ) , error ) . get ( )
let source = try Foundation . JSONDecoder ( ) . decode ( Source . self , from : data )
2022-11-28 12:23:25 -06:00
guard let altstore = source . apps . first ( where : { $0 . bundleIdentifier = = altstoreBundleID } ) else {
let debugDescription = String ( format : NSLocalizedString ( " App with bundle ID '%@' does not exist in source JSON. " , comment : " " ) , altstoreBundleID )
throw CocoaError ( . coderValueNotFound , userInfo : [ NSDebugDescriptionErrorKey : debugDescription ] )
}
guard let versions = altstore . versions else {
let debugDescription = String ( format : NSLocalizedString ( " There is no 'versions' key for %@. " , comment : " " ) , altstore . bundleIdentifier )
throw CocoaError ( . coderReadCorrupt , userInfo : [ NSDebugDescriptionErrorKey : debugDescription ] )
}
guard let latestVersion = versions . first else {
let debugDescription = String ( format : NSLocalizedString ( " The 'versions' array is empty for %@. " , comment : " " ) , altstore . bundleIdentifier )
throw CocoaError ( . coderValueNotFound , userInfo : [ NSDebugDescriptionErrorKey : debugDescription ] )
}
2022-11-21 17:50:42 -06:00
2022-11-28 12:23:25 -06:00
let osName = device . type . osName ? ? " iOS "
2022-11-21 17:50:42 -06:00
let minOSVersionString = latestVersion . minimumOSVersion ? . stringValue ? ? " 12.2 "
guard let latestSupportedVersion = altstore . versions ? . first ( where : { appVersion in
if let minOSVersion = appVersion . minimumOSVersion , device . osVersion < minOSVersion
{
return false
}
return true
} ) else { throw ALTServerError ( . unsupportediOSVersion , userInfo : [ ALTAppNameErrorKey : " AltStore " ,
ALTOperatingSystemNameErrorKey : osName ,
ALTOperatingSystemVersionErrorKey : minOSVersionString ] ) }
guard latestSupportedVersion . version != latestVersion . version else {
// T h e n e w e s t v e r s i o n i s a l s o t h e n e w e s t c o m p a t i b l e v e r s i o n , s o r e t u r n i t s d o w n l o a d U R L .
return completion ( . success ( latestVersion . downloadURL ) )
}
DispatchQueue . main . async {
var message = String ( format : NSLocalizedString ( " %@ is running %@ %@, but AltStore requires %@ %@ or later. " , comment : " " ) , device . name , osName , device . osVersion . stringValue , osName , minOSVersionString )
message += " \n \n "
message += NSLocalizedString ( " Would you like to download the last version compatible with your device instead? " , comment : " " )
let alert = NSAlert ( )
alert . messageText = String ( format : NSLocalizedString ( " Unsupported %@ Version " , comment : " " ) , osName )
alert . informativeText = message
let buttonTitle = String ( format : NSLocalizedString ( " Download %@ %@ " , comment : " " ) , altstore . name , latestSupportedVersion . version )
alert . addButton ( withTitle : buttonTitle )
alert . addButton ( withTitle : NSLocalizedString ( " Cancel " , comment : " " ) )
let index = alert . runModal ( )
if index = = . alertFirstButtonReturn
{
completion ( . success ( latestSupportedVersion . downloadURL ) )
}
else
{
completion ( . failure ( OperationError . cancelled ) )
}
}
2020-11-11 17:25:16 -08:00
2019-06-26 17:05:52 -07:00
}
2022-11-28 12:23:25 -06:00
catch let serverError as ALTServerError where serverError . code = = . unsupportediOSVersion
{
// D o n ' t a d d l o c a l i z e d f a i l u r e f o r u n s u p p o r t e d i O S v e r s i o n e r r o r s .
completion ( . failure ( serverError ) )
}
catch let error as NSError
2019-06-26 17:05:52 -07:00
{
2022-11-28 12:23:25 -06:00
completion ( . failure ( error . withLocalizedFailure ( " The download URL could not be determined. " ) ) )
2019-06-26 17:05:52 -07:00
}
}
2022-11-21 17:50:42 -06:00
dataTask . resume ( )
2019-06-26 17:05:52 -07:00
}
2019-11-18 14:17:57 -08:00
func authenticate ( appleID : String , password : String , anisetteData : ALTAnisetteData , completionHandler : @ escaping ( Result < ( ALTAccount , ALTAppleAPISession ) , Error > ) -> Void )
2019-05-29 15:50:53 -07:00
{
2019-11-18 14:17:57 -08:00
func handleVerificationCode ( _ completionHandler : @ escaping ( String ? ) -> Void )
{
DispatchQueue . main . async {
let alert = NSAlert ( )
alert . messageText = NSLocalizedString ( " Two-Factor Authentication Enabled " , comment : " " )
alert . informativeText = NSLocalizedString ( " Please enter the 6-digit verification code that was sent to your Apple devices. " , comment : " " )
let textField = NSTextField ( frame : NSRect ( x : 0 , y : 0 , width : 300 , height : 22 ) )
textField . delegate = self
textField . translatesAutoresizingMaskIntoConstraints = false
textField . placeholderString = NSLocalizedString ( " 123456 " , comment : " " )
alert . accessoryView = textField
alert . window . initialFirstResponder = textField
alert . addButton ( withTitle : NSLocalizedString ( " Continue " , comment : " " ) )
alert . addButton ( withTitle : NSLocalizedString ( " Cancel " , comment : " " ) )
self . securityCodeAlert = alert
self . securityCodeTextField = textField
self . validate ( )
NSRunningApplication . current . activate ( options : . activateIgnoringOtherApps )
let response = alert . runModal ( )
if response = = . alertFirstButtonReturn
{
let code = textField . stringValue
completionHandler ( code )
}
else
{
completionHandler ( nil )
}
}
}
ALTAppleAPI . shared . authenticate ( appleID : appleID , password : password , anisetteData : anisetteData , verificationHandler : handleVerificationCode ) { ( account , session , error ) in
if let account = account , let session = session
{
completionHandler ( . success ( ( account , session ) ) )
}
else
{
2023-01-24 13:56:41 -06:00
completionHandler ( . failure ( error ? ? ALTAppleAPIError . unknown ( ) ) )
2019-11-18 14:17:57 -08:00
}
2019-05-29 15:50:53 -07:00
}
}
2019-11-18 14:17:57 -08:00
func fetchTeam ( for account : ALTAccount , session : ALTAppleAPISession , completionHandler : @ escaping ( Result < ALTTeam , Error > ) -> Void )
2019-05-29 15:50:53 -07:00
{
2019-11-18 14:17:57 -08:00
ALTAppleAPI . shared . fetchTeams ( for : account , session : session ) { ( teams , error ) in
2019-05-29 15:50:53 -07:00
do
{
let teams = try Result ( teams , error ) . get ( )
2020-12-03 15:05:17 -06:00
if let team = teams . first ( where : { $0 . type = = . individual } )
2019-09-13 14:26:21 -07:00
{
2020-12-03 15:02:35 -06:00
return completionHandler ( . success ( team ) )
2019-09-13 14:26:21 -07:00
}
2020-12-03 15:05:17 -06:00
else if let team = teams . first ( where : { $0 . type = = . free } )
2019-09-13 14:26:21 -07:00
{
2020-12-03 15:02:35 -06:00
return completionHandler ( . success ( team ) )
2019-09-13 14:26:21 -07:00
}
else if let team = teams . first
{
2020-12-03 15:02:35 -06:00
return completionHandler ( . success ( team ) )
2019-09-13 14:26:21 -07:00
}
else
{
2023-01-24 13:56:41 -06:00
throw OperationError ( . noTeam )
2019-09-13 14:26:21 -07:00
}
2019-05-29 15:50:53 -07:00
}
catch
{
2020-12-03 15:02:35 -06:00
completionHandler ( . failure ( error ) )
2019-05-29 15:50:53 -07:00
}
}
}
2019-11-18 14:17:57 -08:00
func fetchCertificate ( for team : ALTTeam , session : ALTAppleAPISession , completionHandler : @ escaping ( Result < ALTCertificate , Error > ) -> Void )
2019-05-29 15:50:53 -07:00
{
2019-11-18 14:17:57 -08:00
ALTAppleAPI . shared . fetchCertificates ( for : team , session : session ) { ( certificates , error ) in
2019-05-29 15:50:53 -07:00
do
{
let certificates = try Result ( certificates , error ) . get ( )
2021-05-20 13:11:54 -07:00
let certificateFileURL = FileManager . default . certificatesDirectory . appendingPathComponent ( team . identifier + " .p12 " )
try FileManager . default . createDirectory ( at : FileManager . default . certificatesDirectory , withIntermediateDirectories : true , attributes : nil )
2020-12-03 15:02:35 -06:00
var isCancelled = false
2019-09-14 11:28:57 -07:00
// C h e c k i f t h e r e i s a n o t h e r A l t S t o r e c e r t i f i c a t e , w h i c h m e a n s A l t S t o r e h a s b e e n i n s t a l l e d w i t h t h i s A p p l e I D b e f o r e .
2021-10-04 15:45:16 -07:00
let altstoreCertificate = certificates . first { $0 . machineName ? . starts ( with : " AltStore " ) = = true }
if let previousCertificate = altstoreCertificate
2019-09-14 11:28:57 -07:00
{
2020-12-03 15:02:35 -06:00
if FileManager . default . fileExists ( atPath : certificateFileURL . path ) ,
let data = try ? Data ( contentsOf : certificateFileURL ) ,
let certificate = ALTCertificate ( p12Data : data , password : previousCertificate . machineIdentifier )
{
2020-12-17 14:45:03 -06:00
// M a n u a l l y s e t m a c h i n e I d e n t i f i e r s o w e c a n e n c r y p t + e m b e d c e r t i f i c a t e i f n e e d e d .
certificate . machineIdentifier = previousCertificate . machineIdentifier
2020-12-03 15:02:35 -06:00
return completionHandler ( . success ( certificate ) )
}
2019-09-14 11:28:57 -07:00
DispatchQueue . main . sync {
let alert = NSAlert ( )
2020-12-03 15:02:35 -06:00
alert . messageText = NSLocalizedString ( " Multiple AltServers Not Supported " , comment : " " )
alert . informativeText = NSLocalizedString ( " Please use the same AltServer you previously used with this Apple ID, or else apps installed with other AltServers will stop working. \n \n Are you sure you want to continue? " , comment : " " )
2019-09-14 11:28:57 -07:00
alert . addButton ( withTitle : NSLocalizedString ( " Continue " , comment : " " ) )
alert . addButton ( withTitle : NSLocalizedString ( " Cancel " , comment : " " ) )
NSRunningApplication . current . activate ( options : . activateIgnoringOtherApps )
let buttonIndex = alert . runModal ( )
if buttonIndex = = NSApplication . ModalResponse . alertSecondButtonReturn
{
isCancelled = true
}
}
2023-01-24 13:56:41 -06:00
guard ! isCancelled else { return completionHandler ( . failure ( OperationError ( . cancelled ) ) ) }
2020-12-03 15:02:35 -06:00
}
2021-10-04 15:43:31 -07:00
func addCertificate ( )
2019-05-29 15:50:53 -07:00
{
2019-11-18 14:17:57 -08:00
ALTAppleAPI . shared . addCertificate ( machineName : " AltStore " , to : team , session : session ) { ( certificate , error ) in
2019-05-29 15:50:53 -07:00
do
{
let certificate = try Result ( certificate , error ) . get ( )
2023-01-24 13:56:41 -06:00
guard let privateKey = certificate . privateKey else { throw OperationError ( . missingPrivateKey ) }
2019-05-29 15:50:53 -07:00
2019-11-18 14:17:57 -08:00
ALTAppleAPI . shared . fetchCertificates ( for : team , session : session ) { ( certificates , error ) in
2019-05-29 15:50:53 -07:00
do
{
let certificates = try Result ( certificates , error ) . get ( )
2019-06-18 17:40:30 -07:00
guard let certificate = certificates . first ( where : { $0 . serialNumber = = certificate . serialNumber } ) else {
2023-01-24 13:56:41 -06:00
throw OperationError ( . missingCertificate )
2019-05-29 15:50:53 -07:00
}
certificate . privateKey = privateKey
completionHandler ( . success ( certificate ) )
2020-12-03 15:02:35 -06:00
if let machineIdentifier = certificate . machineIdentifier ,
let encryptedData = certificate . encryptedP12Data ( withPassword : machineIdentifier )
{
// C a c h e c e r t i f i c a t e .
do { try encryptedData . write ( to : certificateFileURL , options : . atomic ) }
catch { print ( " Failed to cache certificate: " , error ) }
}
2019-05-29 15:50:53 -07:00
}
catch
{
completionHandler ( . failure ( error ) )
}
}
}
catch
{
completionHandler ( . failure ( error ) )
}
}
}
2021-10-04 15:43:31 -07:00
2021-10-04 15:45:16 -07:00
if let certificate = altstoreCertificate ? ? certificates . first
2021-10-04 15:43:31 -07:00
{
if team . type != . free
{
DispatchQueue . main . sync {
let alert = NSAlert ( )
alert . messageText = NSLocalizedString ( " Installing this app will revoke your iOS development certificate. " , comment : " " )
alert . informativeText = NSLocalizedString ( " " "
This will not affect apps you ' ve submitted to the App Store , but may cause apps you ' ve installed to your devices with Xcode to stop working until you reinstall them .
To prevent this from happening , feel free to try again with another Apple ID .
" " " , comment: " " )
alert . addButton ( withTitle : NSLocalizedString ( " Continue " , comment : " " ) )
alert . addButton ( withTitle : NSLocalizedString ( " Cancel " , comment : " " ) )
NSRunningApplication . current . activate ( options : . activateIgnoringOtherApps )
let buttonIndex = alert . runModal ( )
if buttonIndex = = NSApplication . ModalResponse . alertSecondButtonReturn
{
isCancelled = true
}
}
2023-01-24 13:56:41 -06:00
guard ! isCancelled else { return completionHandler ( . failure ( OperationError ( . cancelled ) ) ) }
2021-10-04 15:43:31 -07:00
}
ALTAppleAPI . shared . revoke ( certificate , for : team , session : session ) { ( success , error ) in
do
{
try Result ( success , error ) . get ( )
addCertificate ( )
}
catch
{
completionHandler ( . failure ( error ) )
}
}
}
else
{
addCertificate ( )
}
2019-05-29 15:50:53 -07:00
}
catch
{
completionHandler ( . failure ( error ) )
}
}
}
2020-11-11 17:50:19 -08:00
func prepareAllProvisioningProfiles ( for application : ALTApplication , device : ALTDevice , team : ALTTeam , session : ALTAppleAPISession ,
2020-10-15 11:37:58 -07:00
completion : @ escaping ( Result < [ String : ALTProvisioningProfile ] , Error > ) -> Void )
{
2020-11-11 17:50:19 -08:00
self . prepareProvisioningProfile ( for : application , parentApp : nil , device : device , team : team , session : session ) { ( result ) in
2020-10-15 11:37:58 -07:00
do
{
let profile = try result . get ( )
var profiles = [ application . bundleIdentifier : profile ]
var error : Error ?
let dispatchGroup = DispatchGroup ( )
for appExtension in application . appExtensions
{
dispatchGroup . enter ( )
2020-11-11 17:50:19 -08:00
self . prepareProvisioningProfile ( for : appExtension , parentApp : application , device : device , team : team , session : session ) { ( result ) in
2020-10-15 11:37:58 -07:00
switch result
{
case . failure ( let e ) : error = e
case . success ( let profile ) : profiles [ appExtension . bundleIdentifier ] = profile
}
dispatchGroup . leave ( )
}
}
dispatchGroup . notify ( queue : . global ( ) ) {
if let error = error
{
completion ( . failure ( error ) )
}
else
{
completion ( . success ( profiles ) )
}
}
}
catch
{
completion ( . failure ( error ) )
}
}
}
2020-11-11 17:50:19 -08:00
func prepareProvisioningProfile ( for application : ALTApplication , parentApp : ALTApplication ? , device : ALTDevice , team : ALTTeam , session : ALTAppleAPISession , completionHandler : @ escaping ( Result < ALTProvisioningProfile , Error > ) -> Void )
2020-10-15 11:37:58 -07:00
{
2020-11-11 17:25:16 -08:00
let parentBundleID = parentApp ? . bundleIdentifier ? ? application . bundleIdentifier
let updatedParentBundleID : String
if application . isAltStoreApp
{
// U s e l e g a c y b u n d l e I D f o r m a t f o r A l t S t o r e ( a n d i t s e x t e n s i o n s ) .
updatedParentBundleID = " com. \( team . identifier ) . \( parentBundleID ) "
}
else
{
updatedParentBundleID = parentBundleID + " . " + team . identifier // A p p e n d j u s t t e a m i d e n t i f i e r t o m a k e i t h a r d e r t o t r a c k .
}
let bundleID = application . bundleIdentifier . replacingOccurrences ( of : parentBundleID , with : updatedParentBundleID )
let preferredName : String
if let parentApp = parentApp
{
preferredName = parentApp . name + " " + application . name
}
else
{
preferredName = application . name
}
self . registerAppID ( name : preferredName , bundleID : bundleID , team : team , session : session ) { ( result ) in
2020-10-15 11:37:58 -07:00
do
{
let appID = try result . get ( )
self . updateFeatures ( for : appID , app : application , team : team , session : session ) { ( result ) in
do
{
let appID = try result . get ( )
self . updateAppGroups ( for : appID , app : application , team : team , session : session ) { ( result ) in
do
{
let appID = try result . get ( )
2020-11-11 17:50:19 -08:00
self . fetchProvisioningProfile ( for : appID , device : device , team : team , session : session ) { ( result ) in
2020-10-15 11:37:58 -07:00
completionHandler ( result )
}
}
catch
{
completionHandler ( . failure ( error ) )
}
}
}
catch
{
completionHandler ( . failure ( error ) )
}
}
}
catch
{
completionHandler ( . failure ( error ) )
}
}
}
2020-11-11 17:25:16 -08:00
func registerAppID ( name appName : String , bundleID : String , team : ALTTeam , session : ALTAppleAPISession , completionHandler : @ escaping ( Result < ALTAppID , Error > ) -> Void )
2019-05-29 15:50:53 -07:00
{
2019-11-18 14:17:57 -08:00
ALTAppleAPI . shared . fetchAppIDs ( for : team , session : session ) { ( appIDs , error ) in
2019-05-29 15:50:53 -07:00
do
{
let appIDs = try Result ( appIDs , error ) . get ( )
if let appID = appIDs . first ( where : { $0 . bundleIdentifier = = bundleID } )
{
completionHandler ( . success ( appID ) )
}
else
{
2019-11-18 14:17:57 -08:00
ALTAppleAPI . shared . addAppID ( withName : appName , bundleIdentifier : bundleID , team : team , session : session ) { ( appID , error ) in
2019-05-29 15:50:53 -07:00
completionHandler ( Result ( appID , error ) )
}
}
}
catch
{
completionHandler ( . failure ( error ) )
}
}
}
2019-11-18 14:17:57 -08:00
func updateFeatures ( for appID : ALTAppID , app : ALTApplication , team : ALTTeam , session : ALTAppleAPISession , completionHandler : @ escaping ( Result < ALTAppID , Error > ) -> Void )
2019-06-25 13:34:12 -07:00
{
let requiredFeatures = app . entitlements . compactMap { ( entitlement , value ) -> ( ALTFeature , Any ) ? in
2019-09-30 13:59:17 -07:00
guard let feature = ALTFeature ( entitlement : entitlement ) else { return nil }
2019-06-25 13:34:12 -07:00
return ( feature , value )
}
var features = requiredFeatures . reduce ( into : [ ALTFeature : Any ] ( ) ) { $0 [ $1 . 0 ] = $1 . 1 }
if let applicationGroups = app . entitlements [ . appGroups ] as ? [ String ] , ! applicationGroups . isEmpty
{
2022-03-31 12:50:50 -07:00
// A p p u s e s a p p g r o u p s , s o a s s i g n ` t r u e ` t o e n a b l e t h e f e a t u r e .
2019-06-25 13:34:12 -07:00
features [ . appGroups ] = true
}
2022-03-31 12:50:50 -07:00
else
{
// A p p h a s n o a p p g r o u p s , s o a s s i g n ` f a l s e ` t o d i s a b l e t h e f e a t u r e .
features [ . appGroups ] = false
}
2019-06-25 13:34:12 -07:00
2020-05-19 18:30:53 -07:00
var updateFeatures = false
// D e t e r m i n e w h e t h e r t h e r e q u i r e d f e a t u r e s a r e a l r e a d y e n a b l e d f o r t h e A p p I D .
for ( feature , value ) in features
{
if let appIDValue = appID . features [ feature ] as AnyObject ? , ( value as AnyObject ) . isEqual ( appIDValue )
{
// A p p I D a l r e a d y h a s t h i s f e a t u r e e n a b l e d a n d t h e v a l u e s a r e t h e s a m e .
continue
}
2022-03-31 12:50:50 -07:00
else if appID . features [ feature ] = = nil , let shouldEnableFeature = value as ? Bool , ! shouldEnableFeature
{
// A p p I D d o e s n ' t a l r e a d y h a v e t h i s f e a t u r e e n a b l e d , b u t w e w a n t i t d i s a b l e d a n y w a y .
continue
}
2020-05-19 18:30:53 -07:00
else
{
// A p p I D e i t h e r d o e s n ' t h a v e t h i s f e a t u r e e n a b l e d o r t h e v a l u e h a s c h a n g e d ,
// s o w e n e e d t o u p d a t e i t t o r e f l e c t n e w v a l u e s .
updateFeatures = true
break
}
}
2019-06-25 13:34:12 -07:00
2020-05-19 18:30:53 -07:00
if updateFeatures
{
let appID = appID . copy ( ) as ! ALTAppID
appID . features = features
ALTAppleAPI . shared . update ( appID , team : team , session : session ) { ( appID , error ) in
completionHandler ( Result ( appID , error ) )
}
}
else
{
completionHandler ( . success ( appID ) )
}
}
func updateAppGroups ( for appID : ALTAppID , app : ALTApplication , team : ALTTeam , session : ALTAppleAPISession , completionHandler : @ escaping ( Result < ALTAppID , Error > ) -> Void )
{
2022-03-29 19:51:54 -07:00
guard let applicationGroups = app . entitlements [ . appGroups ] as ? [ String ] , ! applicationGroups . isEmpty else {
// A s s i g n i n g a n A p p I D t o a n e m p t y a p p g r o u p a r r a y f a i l s ,
// s o j u s t d o n o t h i n g i f t h e r e a r e n o a p p g r o u p s .
return completionHandler ( . success ( appID ) )
2020-05-19 18:30:53 -07:00
}
2022-03-29 19:47:46 -07:00
// D i s p a t c h o n t o g l o b a l q u e u e t o p r e v e n t a p p G r o u p s S e m a p h o r e d e a d l o c k .
2020-05-19 18:30:53 -07:00
DispatchQueue . global ( ) . async {
// E n s u r e w e ' r e n o t c o n c u r r e n t l y f e t c h i n g a n d u p d a t i n g a p p g r o u p s ,
// w h i c h c a n l e a d t o r a c e c o n d i t i o n s s u c h a s a d d i n g a n a p p g r o u p t w i c e .
2022-03-29 19:47:46 -07:00
appGroupsSemaphore . wait ( )
2020-05-19 18:30:53 -07:00
func finish ( _ result : Result < ALTAppID , Error > )
{
2022-03-29 19:47:46 -07:00
appGroupsSemaphore . signal ( )
2020-05-19 18:30:53 -07:00
completionHandler ( result )
}
ALTAppleAPI . shared . fetchAppGroups ( for : team , session : session ) { ( groups , error ) in
switch Result ( groups , error )
{
case . failure ( let error ) : finish ( . failure ( error ) )
case . success ( let fetchedGroups ) :
let dispatchGroup = DispatchGroup ( )
var groups = [ ALTAppGroup ] ( )
var errors = [ Error ] ( )
for groupIdentifier in applicationGroups
{
let adjustedGroupIdentifier = groupIdentifier + " . " + team . identifier
if let group = fetchedGroups . first ( where : { $0 . groupIdentifier = = adjustedGroupIdentifier } )
{
groups . append ( group )
}
else
{
dispatchGroup . enter ( )
// N o t a l l c h a r a c t e r s a r e a l l o w e d i n g r o u p n a m e s , s o w e r e p l a c e p e r i o d s w i t h s p a c e s ( l i k e A p p l e d o e s ) .
let name = " AltStore " + groupIdentifier . replacingOccurrences ( of : " . " , with : " " )
ALTAppleAPI . shared . addAppGroup ( withName : name , groupIdentifier : adjustedGroupIdentifier , team : team , session : session ) { ( group , error ) in
switch Result ( group , error )
{
case . success ( let group ) : groups . append ( group )
case . failure ( let error ) : errors . append ( error )
}
dispatchGroup . leave ( )
}
}
}
dispatchGroup . notify ( queue : . global ( ) ) {
if let error = errors . first
{
finish ( . failure ( error ) )
}
else
{
ALTAppleAPI . shared . assign ( appID , to : Array ( groups ) , team : team , session : session ) { ( success , error ) in
let result = Result ( success , error )
finish ( result . map { _ in appID } )
}
}
}
}
}
2019-06-25 13:34:12 -07:00
}
}
2019-11-18 14:17:57 -08:00
func register ( _ device : ALTDevice , team : ALTTeam , session : ALTAppleAPISession , completionHandler : @ escaping ( Result < ALTDevice , Error > ) -> Void )
2019-05-29 15:50:53 -07:00
{
2020-11-11 17:50:19 -08:00
ALTAppleAPI . shared . fetchDevices ( for : team , types : device . type , session : session ) { ( devices , error ) in
2019-05-29 15:50:53 -07:00
do
{
let devices = try Result ( devices , error ) . get ( )
if let device = devices . first ( where : { $0 . identifier = = device . identifier } )
{
completionHandler ( . success ( device ) )
}
else
{
2020-11-11 17:50:19 -08:00
ALTAppleAPI . shared . registerDevice ( name : device . name , identifier : device . identifier , type : device . type , team : team , session : session ) { ( device , error ) in
2019-05-29 15:50:53 -07:00
completionHandler ( Result ( device , error ) )
}
}
}
catch
{
completionHandler ( . failure ( error ) )
}
}
}
2020-11-11 17:50:19 -08:00
func fetchProvisioningProfile ( for appID : ALTAppID , device : ALTDevice , team : ALTTeam , session : ALTAppleAPISession , completionHandler : @ escaping ( Result < ALTProvisioningProfile , Error > ) -> Void )
2019-05-29 15:50:53 -07:00
{
2020-11-11 17:50:19 -08:00
ALTAppleAPI . shared . fetchProvisioningProfile ( for : appID , deviceType : device . type , team : team , session : session ) { ( profile , error ) in
2019-05-29 15:50:53 -07:00
completionHandler ( Result ( profile , error ) )
}
}
2020-10-15 11:37:58 -07:00
func install ( _ application : ALTApplication , to device : ALTDevice , team : ALTTeam , certificate : ALTCertificate , profiles : [ String : ALTProvisioningProfile ] , completionHandler : @ escaping ( Result < Void , Error > ) -> Void )
2019-05-29 15:50:53 -07:00
{
2020-10-15 11:37:58 -07:00
func prepare ( _ bundle : Bundle , additionalInfoDictionaryValues : [ String : Any ] = [ : ] ) throws
{
guard let identifier = bundle . bundleIdentifier else { throw ALTError ( . missingAppBundle ) }
guard let profile = profiles [ identifier ] else { throw ALTError ( . missingProvisioningProfile ) }
guard var infoDictionary = bundle . completeInfoDictionary else { throw ALTError ( . missingInfoPlist ) }
infoDictionary [ kCFBundleIdentifierKey as String ] = profile . bundleIdentifier
infoDictionary [ Bundle . Info . altBundleID ] = identifier
2021-09-13 17:11:53 -04:00
if ( infoDictionary . keys . contains ( Bundle . Info . deviceID ) ) {
infoDictionary [ Bundle . Info . deviceID ] = device . identifier
}
2020-10-15 11:37:58 -07:00
for ( key , value ) in additionalInfoDictionaryValues
{
infoDictionary [ key ] = value
}
if let appGroups = profile . entitlements [ . appGroups ] as ? [ String ]
{
infoDictionary [ Bundle . Info . appGroups ] = appGroups
}
try ( infoDictionary as NSDictionary ) . write ( to : bundle . infoPlistURL )
}
2019-06-25 13:34:12 -07:00
DispatchQueue . global ( ) . async {
do
{
2020-10-15 11:37:58 -07:00
guard let appBundle = Bundle ( url : application . fileURL ) else { throw ALTError ( . missingAppBundle ) }
guard let infoDictionary = appBundle . completeInfoDictionary else { throw ALTError ( . missingInfoPlist ) }
2020-05-19 18:30:53 -07:00
let openAppURL = URL ( string : " altstore- " + application . bundleIdentifier + " :// " ) !
var allURLSchemes = infoDictionary [ Bundle . Info . urlTypes ] as ? [ [ String : Any ] ] ? ? [ ]
// E m b e d o p e n U R L s o A l t B a c k u p c a n r e t u r n t o A l t S t o r e .
let altstoreURLScheme = [ " CFBundleTypeRole " : " Editor " ,
" CFBundleURLName " : application . bundleIdentifier ,
" CFBundleURLSchemes " : [ openAppURL . scheme ! ] ] as [ String : Any ]
allURLSchemes . append ( altstoreURLScheme )
2020-10-15 11:37:58 -07:00
var additionalValues : [ String : Any ] = [ Bundle . Info . urlTypes : allURLSchemes ]
2020-05-19 18:30:53 -07:00
2020-11-11 17:25:16 -08:00
if application . isAltStoreApp
2019-10-28 12:53:56 -07:00
{
2020-11-11 17:25:16 -08:00
additionalValues [ Bundle . Info . deviceID ] = device . identifier
additionalValues [ Bundle . Info . serverID ] = UserDefaults . standard . serverID
2020-10-15 11:37:58 -07:00
2020-11-11 17:25:16 -08:00
if
let machineIdentifier = certificate . machineIdentifier ,
let encryptedData = certificate . encryptedP12Data ( withPassword : machineIdentifier )
{
additionalValues [ Bundle . Info . certificateID ] = certificate . serialNumber
let certificateURL = application . fileURL . appendingPathComponent ( " ALTCertificate.p12 " )
try encryptedData . write ( to : certificateURL , options : . atomic )
}
2019-10-28 12:53:56 -07:00
}
2022-03-29 19:34:47 -07:00
else if infoDictionary . keys . contains ( Bundle . Info . deviceID )
{
// T h e r e i s a n A L T D e v i c e I D e n t r y , s o a s s u m e t h e a p p i s u s i n g A l t K i t a n d r e p l a c e i t w i t h t h e d e v i c e ' s U D I D .
additionalValues [ Bundle . Info . deviceID ] = device . identifier
additionalValues [ Bundle . Info . serverID ] = UserDefaults . standard . serverID
}
2019-06-25 13:34:12 -07:00
2020-10-15 11:37:58 -07:00
try prepare ( appBundle , additionalInfoDictionaryValues : additionalValues )
for appExtension in application . appExtensions
{
guard let bundle = Bundle ( url : appExtension . fileURL ) else { throw ALTError ( . missingAppBundle ) }
try prepare ( bundle )
}
2019-06-25 13:34:12 -07:00
let resigner = ALTSigner ( team : team , certificate : certificate )
2020-10-15 11:37:58 -07:00
resigner . signApp ( at : application . fileURL , provisioningProfiles : Array ( profiles . values ) ) { ( success , error ) in
2019-06-25 13:34:12 -07:00
do
{
try Result ( success , error ) . get ( )
2020-11-11 17:25:16 -08:00
let activeProfiles : Set < String > ? = ( team . type = = . free && application . isAltStoreApp ) ? Set ( profiles . values . map ( \ . bundleIdentifier ) ) : nil
2020-03-11 13:51:39 -07:00
ALTDeviceManager . shared . installApp ( at : application . fileURL , toDeviceWithUDID : device . identifier , activeProvisioningProfiles : activeProfiles ) { ( success , error ) in
2019-06-25 13:34:12 -07:00
completionHandler ( Result ( success , error ) )
}
}
catch
{
print ( " Failed to install app " , error )
completionHandler ( . failure ( error ) )
2019-05-29 15:50:53 -07:00
}
}
}
2019-06-25 13:34:12 -07:00
catch
{
print ( " Failed to install AltStore " , error )
completionHandler ( . failure ( error ) )
}
2019-05-29 15:50:53 -07:00
}
2019-05-24 11:29:27 -07:00
}
2020-11-11 17:25:16 -08:00
func showInstallationAlert ( appName : String , deviceName : String )
{
let content = UNMutableNotificationContent ( )
content . title = String ( format : NSLocalizedString ( " Installing %@ to %@... " , comment : " " ) , appName , deviceName )
content . body = NSLocalizedString ( " This may take a few seconds. " , comment : " " )
let request = UNNotificationRequest ( identifier : UUID ( ) . uuidString , content : content , trigger : nil )
UNUserNotificationCenter . current ( ) . add ( request )
}
2019-05-24 11:29:27 -07:00
}
2019-11-18 14:17:57 -08:00
private var securityCodeAlertKey = 0
private var securityCodeTextFieldKey = 0
extension ALTDeviceManager : NSTextFieldDelegate
{
var securityCodeAlert : NSAlert ? {
get { return objc_getAssociatedObject ( self , & securityCodeAlertKey ) as ? NSAlert }
set { objc_setAssociatedObject ( self , & securityCodeAlertKey , newValue , . OBJC_ASSOCIATION_RETAIN_NONATOMIC ) }
}
var securityCodeTextField : NSTextField ? {
get { return objc_getAssociatedObject ( self , & securityCodeTextFieldKey ) as ? NSTextField }
set { objc_setAssociatedObject ( self , & securityCodeTextFieldKey , newValue , . OBJC_ASSOCIATION_RETAIN_NONATOMIC ) }
}
public func controlTextDidChange ( _ obj : Notification )
{
self . validate ( )
}
public func controlTextDidEndEditing ( _ obj : Notification )
{
self . validate ( )
}
private func validate ( )
{
guard let code = self . securityCodeTextField ? . stringValue . trimmingCharacters ( in : . whitespacesAndNewlines ) else { return }
if code . count = = 6
{
self . securityCodeAlert ? . buttons . first ? . isEnabled = true
}
else
{
self . securityCodeAlert ? . buttons . first ? . isEnabled = false
}
self . securityCodeAlert ? . layout ( )
}
}