2015-08-24 18:44:47 +00:00
|
|
|
//
|
|
|
|
// ISStoreAccount.swift
|
|
|
|
// mas-cli
|
|
|
|
//
|
|
|
|
// Created by Andrew Naylor on 22/08/2015.
|
|
|
|
// Copyright (c) 2015 Andrew Naylor. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2018-10-14 19:41:19 +00:00
|
|
|
import StoreFoundation
|
|
|
|
import CommerceKit
|
|
|
|
|
2018-04-04 03:04:56 +00:00
|
|
|
extension ISStoreAccount: StoreAccount {
|
2015-08-24 18:44:47 +00:00
|
|
|
static var primaryAccountIsPresentAndSignedIn: Bool {
|
2016-09-17 12:58:38 +00:00
|
|
|
return CKAccountStore.shared().primaryAccountIsPresentAndSignedIn
|
2015-08-24 18:44:47 +00:00
|
|
|
}
|
2018-04-15 03:03:19 +00:00
|
|
|
|
2018-04-04 03:04:56 +00:00
|
|
|
static var primaryAccount: StoreAccount? {
|
2018-08-11 23:06:38 +00:00
|
|
|
var account: ISStoreAccount?
|
|
|
|
|
|
|
|
if #available(macOS 10.13, *) {
|
|
|
|
let group = DispatchGroup()
|
|
|
|
group.enter()
|
|
|
|
|
|
|
|
let accountService: ISAccountService = ISServiceProxy.genericShared().accountService
|
|
|
|
accountService.primaryAccount { (storeAccount: ISStoreAccount) in
|
|
|
|
account = storeAccount
|
|
|
|
group.leave()
|
|
|
|
}
|
|
|
|
|
2018-08-11 23:08:43 +00:00
|
|
|
_ = group.wait(timeout: .now() + 30)
|
2018-08-11 23:06:38 +00:00
|
|
|
} else {
|
|
|
|
// macOS 10.9-10.12
|
|
|
|
let accountStore = CKAccountStore.shared()
|
|
|
|
account = accountStore.primaryAccount
|
|
|
|
}
|
|
|
|
|
|
|
|
return account
|
2015-08-24 18:44:47 +00:00
|
|
|
}
|
2018-04-15 03:03:19 +00:00
|
|
|
|
2018-04-04 03:04:56 +00:00
|
|
|
static func signIn(username: String, password: String, systemDialog: Bool = false) throws -> StoreAccount {
|
2019-01-12 01:06:02 +00:00
|
|
|
var storeAccount: ISStoreAccount?
|
|
|
|
var maserror: MASError?
|
2018-04-15 03:03:19 +00:00
|
|
|
|
2018-04-06 03:29:15 +00:00
|
|
|
let accountService: ISAccountService = ISServiceProxy.genericShared().accountService
|
2016-02-14 01:22:33 +00:00
|
|
|
let client = ISStoreClient(storeClientType: 0)
|
|
|
|
accountService.setStoreClient(client)
|
2018-08-11 03:04:21 +00:00
|
|
|
|
|
|
|
let context = ISAuthenticationContext(accountID: 0)
|
|
|
|
context.appleIDOverride = username
|
|
|
|
|
|
|
|
if systemDialog {
|
|
|
|
context.appleIDOverride = username
|
|
|
|
} else {
|
|
|
|
context.demoMode = true
|
|
|
|
context.demoAccountName = username
|
|
|
|
context.demoAccountPassword = password
|
|
|
|
context.demoAutologinMode = true
|
|
|
|
}
|
2018-02-21 16:18:47 +00:00
|
|
|
|
2016-09-17 12:58:38 +00:00
|
|
|
let group = DispatchGroup()
|
|
|
|
group.enter()
|
2018-02-21 16:18:47 +00:00
|
|
|
|
2018-08-11 03:04:21 +00:00
|
|
|
// Only works on macOS Sierra and below
|
2019-01-12 01:06:02 +00:00
|
|
|
accountService.signIn(with: context) { success, account, error in
|
2018-08-11 03:04:21 +00:00
|
|
|
if success {
|
2019-01-12 01:06:02 +00:00
|
|
|
storeAccount = account
|
2018-08-11 03:04:21 +00:00
|
|
|
} else {
|
2019-01-12 01:06:02 +00:00
|
|
|
maserror = .signInFailed(error: error as NSError?)
|
2018-08-11 03:04:21 +00:00
|
|
|
}
|
|
|
|
group.leave()
|
|
|
|
}
|
2018-02-21 16:18:47 +00:00
|
|
|
|
2016-09-17 16:30:58 +00:00
|
|
|
if systemDialog {
|
|
|
|
group.wait()
|
|
|
|
} else {
|
2019-01-12 01:06:02 +00:00
|
|
|
_ = group.wait(timeout: .now() + 30)
|
2016-09-17 16:30:58 +00:00
|
|
|
}
|
2019-01-12 01:06:02 +00:00
|
|
|
|
|
|
|
if let account = storeAccount {
|
2016-02-14 01:22:33 +00:00
|
|
|
return account
|
|
|
|
}
|
2019-01-12 01:06:02 +00:00
|
|
|
|
|
|
|
throw maserror ?? MASError.signInFailed(error: nil)
|
2016-02-14 01:22:33 +00:00
|
|
|
}
|
2015-08-24 18:44:47 +00:00
|
|
|
}
|