2017-01-12 21:37:37 +01:00
|
|
|
//
|
|
|
|
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
|
|
|
|
//
|
2016-10-10 22:02:09 +02:00
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import PromiseKit
|
|
|
|
|
2016-11-12 18:22:29 +01:00
|
|
|
/**
|
|
|
|
* Signal is actually two services - textSecure for messages and red phone (for calls).
|
|
|
|
* AccountManager delegates to both.
|
|
|
|
*/
|
2017-01-12 21:37:37 +01:00
|
|
|
class AccountManager: NSObject {
|
2016-10-10 22:02:09 +02:00
|
|
|
let TAG = "[AccountManager]"
|
|
|
|
let textSecureAccountManager: TSAccountManager
|
2016-11-12 18:22:29 +01:00
|
|
|
let networkManager: TSNetworkManager
|
2017-08-11 22:29:05 +02:00
|
|
|
let preferences: PropertyListPreferences
|
2016-10-10 22:02:09 +02:00
|
|
|
|
2017-08-11 22:29:05 +02:00
|
|
|
var pushManager: PushManager {
|
|
|
|
// dependency injection hack since PushManager has *alot* of dependencies, and would induce a cycle.
|
|
|
|
return PushManager.shared()
|
|
|
|
}
|
|
|
|
|
|
|
|
required init(textSecureAccountManager: TSAccountManager, preferences: PropertyListPreferences) {
|
2016-11-12 18:22:29 +01:00
|
|
|
self.networkManager = textSecureAccountManager.networkManager
|
2016-10-10 22:02:09 +02:00
|
|
|
self.textSecureAccountManager = textSecureAccountManager
|
2017-08-11 22:29:05 +02:00
|
|
|
self.preferences = preferences
|
2016-10-10 22:02:09 +02:00
|
|
|
}
|
|
|
|
|
2016-11-12 18:22:29 +01:00
|
|
|
// MARK: registration
|
|
|
|
|
2016-10-10 22:02:09 +02:00
|
|
|
@objc func register(verificationCode: String) -> AnyPromise {
|
2017-01-12 21:37:37 +01:00
|
|
|
return AnyPromise(register(verificationCode: verificationCode))
|
2016-10-10 22:02:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func register(verificationCode: String) -> Promise<Void> {
|
2017-08-11 22:29:05 +02:00
|
|
|
let registrationPromise = firstly {
|
2016-10-10 22:02:09 +02:00
|
|
|
Promise { fulfill, reject in
|
|
|
|
if verificationCode.characters.count == 0 {
|
|
|
|
let error = OWSErrorWithCodeDescription(.userError,
|
|
|
|
NSLocalizedString("REGISTRATION_ERROR_BLANK_VERIFICATION_CODE",
|
|
|
|
comment: "alert body during registration"))
|
|
|
|
reject(error)
|
|
|
|
}
|
|
|
|
fulfill()
|
|
|
|
}
|
|
|
|
}.then {
|
2017-01-12 21:37:37 +01:00
|
|
|
Logger.debug("\(self.TAG) verification code looks well formed.")
|
2016-10-10 22:02:09 +02:00
|
|
|
return self.registerForTextSecure(verificationCode: verificationCode)
|
2017-08-11 22:29:05 +02:00
|
|
|
}.then {
|
|
|
|
return SyncPushTokensJob.run(pushManager: self.pushManager, accountManager: self, preferences: self.preferences)
|
2016-10-10 22:02:09 +02:00
|
|
|
}.then {
|
|
|
|
Logger.debug("\(self.TAG) successfully registered for TextSecure")
|
|
|
|
}
|
2017-08-11 22:29:05 +02:00
|
|
|
registrationPromise.retainUntilComplete()
|
|
|
|
|
|
|
|
return registrationPromise
|
2016-10-10 22:02:09 +02:00
|
|
|
}
|
|
|
|
|
2016-11-12 18:22:29 +01:00
|
|
|
private func registerForTextSecure(verificationCode: String) -> Promise<Void> {
|
|
|
|
return Promise { fulfill, reject in
|
|
|
|
self.textSecureAccountManager.verifyAccount(withCode:verificationCode,
|
|
|
|
success:fulfill,
|
|
|
|
failure:reject)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Push Tokens
|
|
|
|
|
2016-10-10 22:02:09 +02:00
|
|
|
func updatePushTokens(pushToken: String, voipToken: String) -> Promise<Void> {
|
|
|
|
return firstly {
|
|
|
|
return self.updateTextSecurePushTokens(pushToken: pushToken, voipToken: voipToken)
|
|
|
|
}.then {
|
|
|
|
Logger.info("\(self.TAG) Successfully updated text secure push tokens.")
|
2016-11-12 18:22:29 +01:00
|
|
|
// TODO code cleanup - convert to `return Promise(value: nil)` and test
|
2017-01-12 21:37:37 +01:00
|
|
|
return Promise { fulfill, _ in
|
|
|
|
fulfill()
|
2016-10-10 22:02:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func updateTextSecurePushTokens(pushToken: String, voipToken: String) -> Promise<Void> {
|
|
|
|
return Promise { fulfill, reject in
|
|
|
|
self.textSecureAccountManager.registerForPushNotifications(pushToken:pushToken,
|
|
|
|
voipToken:voipToken,
|
|
|
|
success:fulfill,
|
|
|
|
failure:reject)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-12 18:22:29 +01:00
|
|
|
// MARK: Turn Server
|
2016-10-10 22:02:09 +02:00
|
|
|
|
2016-11-12 18:22:29 +01:00
|
|
|
func getTurnServerInfo() -> Promise<TurnServerInfo> {
|
2016-10-10 22:02:09 +02:00
|
|
|
return Promise { fulfill, reject in
|
2016-11-12 18:22:29 +01:00
|
|
|
self.networkManager.makeRequest(TurnServerInfoRequest(),
|
2017-07-08 23:43:34 +02:00
|
|
|
success: { (_: URLSessionDataTask, responseObject: Any?) in
|
2016-11-12 18:22:29 +01:00
|
|
|
guard responseObject != nil else {
|
|
|
|
return reject(OWSErrorMakeUnableToProcessServerResponseError())
|
|
|
|
}
|
2016-10-10 22:02:09 +02:00
|
|
|
|
2016-11-12 18:22:29 +01:00
|
|
|
if let responseDictionary = responseObject as? [String: AnyObject] {
|
|
|
|
if let turnServerInfo = TurnServerInfo(attributes:responseDictionary) {
|
|
|
|
return fulfill(turnServerInfo)
|
|
|
|
}
|
2017-02-02 21:51:07 +01:00
|
|
|
Logger.error("\(self.TAG) unexpected server response:\(responseDictionary)")
|
2016-11-12 18:22:29 +01:00
|
|
|
}
|
|
|
|
return reject(OWSErrorMakeUnableToProcessServerResponseError())
|
|
|
|
},
|
2017-07-08 23:43:34 +02:00
|
|
|
failure: { (_: URLSessionDataTask, error: Error) in
|
2016-11-12 18:22:29 +01:00
|
|
|
return reject(error)
|
|
|
|
})
|
2016-10-10 22:02:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|