IOS-1542 WIP LoginViewModel using VerifiableAccountProtocol. IOS-1542
authorDirk Zimmermann <dz@pep.security>
Thu, 18 Apr 2019 16:20:54 +0200
branchIOS-1542
changeset 83218943ce579269
parent 8312 e59c460fd936
child 8322 36efa1c97e66
IOS-1542 WIP LoginViewModel using VerifiableAccountProtocol.
pEpForiOS/UI/Login/LoginViewController.swift
pEpForiOS/UI/Login/ViewModel/LoginViewModel.swift
     1.1 --- a/pEpForiOS/UI/Login/LoginViewController.swift	Thu Apr 18 07:51:46 2019 +0200
     1.2 +++ b/pEpForiOS/UI/Login/LoginViewController.swift	Thu Apr 18 16:20:54 2019 +0200
     1.3 @@ -88,7 +88,7 @@
     1.4  
     1.5      override func didSetAppConfig() {
     1.6          super.didSetAppConfig()
     1.7 -        loginViewModel.verificationService = VerificationService()
     1.8 +        loginViewModel.verificationService = VerifiableAccount()
     1.9      }
    1.10  
    1.11      override func viewDidLoad() {
     2.1 --- a/pEpForiOS/UI/Login/ViewModel/LoginViewModel.swift	Thu Apr 18 07:51:46 2019 +0200
     2.2 +++ b/pEpForiOS/UI/Login/ViewModel/LoginViewModel.swift	Thu Apr 18 16:20:54 2019 +0200
     2.3 @@ -23,8 +23,9 @@
     2.4          let mySelfer: KickOffMySelfProtocol
     2.5      }
     2.6  
     2.7 -    var loginAccount: Account?
     2.8 -    var verificationService: VerificationServiceProtocol?
     2.9 +    /// Holding both the data of the current account in verification,
    2.10 +    /// and also the implementation of the verification.
    2.11 +    var verificationService: VerifiableAccountProtocol?
    2.12  
    2.13      /** If the last login attempt was via OAuth2, this will collect temporary parameters */
    2.14      private var lastOAuth2Parameters: OAuth2Parameters?
    2.15 @@ -45,18 +46,13 @@
    2.16      var currentOauth2Authorizer: OAuth2AuthorizationProtocol?
    2.17  
    2.18      /**
    2.19 -     The most current account in verification.
    2.20 -     */
    2.21 -    var accountInVerification: VerifiableAccount?
    2.22 -
    2.23 -    /**
    2.24       Helper model to handle most of the OAuth2 authorization.
    2.25       */
    2.26      var oauth2Model = OAuth2AuthViewModel()
    2.27  
    2.28      let qualifyServerService = QualifyServerIsLocalService()
    2.29  
    2.30 -    init(verificationService: VerificationServiceProtocol? = nil) {
    2.31 +    init(verificationService: VerifiableAccountProtocol? = nil) {
    2.32          self.verificationService = verificationService
    2.33      }
    2.34  
    2.35 @@ -120,32 +116,29 @@
    2.36              let smtpTransport = ConnectionTransport(
    2.37                  accountSettingsTransport: outgoingServer.transport, smtpPort: outgoingServer.port)
    2.38  
    2.39 +            var newAccount = verificationService ?? VerifiableAccount()
    2.40 +
    2.41 +            newAccount.verifiableAccountDelegate = self
    2.42 +            newAccount.address = accountName
    2.43 +            newAccount.userName = userName
    2.44 +            newAccount.loginName = loginName
    2.45 +
    2.46              // Note: auth method is never taken from LAS. We either have OAuth2,
    2.47              // as determined previously, or we will defer to pantomime to find out the best method.
    2.48 -            let newAccount = VerifiableAccount(
    2.49 -                verifiableAccountDelegate: self,
    2.50 -                address: accountName,
    2.51 -                userName: userName,
    2.52 -                loginName: loginName,
    2.53 -                authMethod: accessToken != nil ? .saslXoauth2 : nil,
    2.54 -                password: accessToken == nil ? password : nil,
    2.55 -                accessToken: accessToken,
    2.56 -                serverIMAP: incomingServer.hostname,
    2.57 -                portIMAP: UInt16(incomingServer.port),
    2.58 -                transportIMAP: imapTransport,
    2.59 -                serverSMTP: outgoingServer.hostname,
    2.60 -                portSMTP: UInt16(outgoingServer.port),
    2.61 -                transportSMTP: smtpTransport,
    2.62 -                trustedImapServer: false)
    2.63 +            newAccount.authMethod = accessToken != nil ? .saslXoauth2 : nil
    2.64  
    2.65 -            accountInVerification = newAccount
    2.66 +            newAccount.password = password
    2.67 +            newAccount.accessToken = accessToken
    2.68 +            newAccount.serverIMAP = incomingServer.hostname
    2.69 +            newAccount.portIMAP = UInt16(incomingServer.port)
    2.70 +            newAccount.transportIMAP = imapTransport
    2.71 +            newAccount.serverSMTP = outgoingServer.hostname
    2.72 +            newAccount.portSMTP = UInt16(outgoingServer.port)
    2.73 +            newAccount.transportSMTP = smtpTransport
    2.74 +            newAccount.trustedImapServer = false
    2.75  
    2.76 -            do {
    2.77 -                try verifyAccount(model: newAccount)
    2.78 -            } catch {
    2.79 -                Logger.frontendLogger.error("%{public}@", error.localizedDescription)
    2.80 -                loginViewModelLoginErrorDelegate?.handle(loginError: error)
    2.81 -            }
    2.82 +            verificationService = newAccount
    2.83 +            verifyAccount(model: newAccount)
    2.84          }
    2.85      }
    2.86  
    2.87 @@ -153,33 +146,28 @@
    2.88      ///
    2.89      /// - Parameter model: account data
    2.90      /// - Throws: AccountVerificationError
    2.91 -    func verifyAccount(model: VerifiableAccount) throws {
    2.92 -        do {
    2.93 -            let account = try model.account()
    2.94 -            loginAccount = account // have to store that for callback use
    2.95 -
    2.96 -            if let imapServer = account.imapServer?.address {
    2.97 -                qualifyServerService.delegate = self
    2.98 -                qualifyServerService.qualify(serverName: imapServer)
    2.99 -            } else {
   2.100 -                accountHasBeenQualified(trusted: false)
   2.101 -            }
   2.102 -        } catch {
   2.103 -            throw error
   2.104 +    func verifyAccount(model: VerifiableAccountProtocol?) {
   2.105 +        if let imapServer = verificationService?.serverIMAP {
   2.106 +            qualifyServerService.delegate = self
   2.107 +            qualifyServerService.qualify(serverName: imapServer)
   2.108 +        } else {
   2.109 +            accountHasBeenQualified(trusted: false)
   2.110          }
   2.111      }
   2.112  
   2.113      func accountHasBeenQualified(trusted: Bool) {
   2.114 -        guard let verificationService = verificationService else {
   2.115 +        guard var theVerificationService = verificationService else {
   2.116              Logger.frontendLogger.errorAndCrash("no VerificationService")
   2.117              return
   2.118          }
   2.119 -        guard let account = loginAccount else {
   2.120 -            Logger.frontendLogger.errorAndCrash("have lost loginAccount")
   2.121 -            return
   2.122 +
   2.123 +        theVerificationService.trustedImapServer = trusted
   2.124 +        do {
   2.125 +            try theVerificationService.verify()
   2.126 +        } catch {
   2.127 +            Logger.frontendLogger.error("%{public}@", error.localizedDescription)
   2.128 +            loginViewModelLoginErrorDelegate?.handle(loginError: error)
   2.129          }
   2.130 -        account.imapServer?.trusted = trusted
   2.131 -        verificationService.requestVerification(account: account, delegate: self)
   2.132      }
   2.133  
   2.134      /**
   2.135 @@ -192,24 +180,6 @@
   2.136      }
   2.137  }
   2.138  
   2.139 -// MARK: - AccountVerificationServiceDelegate
   2.140 -
   2.141 -extension LoginViewModel: AccountVerificationServiceDelegate {
   2.142 -    func verified(account: Account,
   2.143 -                  service: AccountVerificationServiceProtocol,
   2.144 -                  result: AccountVerificationResult) {
   2.145 -        if result == .ok {
   2.146 -            //remove obsolete code on EmailListViewController
   2.147 -            MessageModelUtil.performAndWait {
   2.148 -                account.save()
   2.149 -            }
   2.150 -            mySelfer?.startMySelf()
   2.151 -        }
   2.152 -        accountVerificationResultDelegate?.didVerify(result: result,
   2.153 -                                                     accountInput: accountInVerification)
   2.154 -    }
   2.155 -}
   2.156 -
   2.157  // MARK: - OAuth2AuthViewModelDelegate
   2.158  
   2.159  extension LoginViewModel: OAuth2AuthViewModelDelegate {
   2.160 @@ -224,7 +194,7 @@
   2.161                      return
   2.162                  }
   2.163                  login(accountName: oauth2Params.emailAddress, userName: oauth2Params.userName,
   2.164 -                      accessToken: accessToken, mySelfer: oauth2Params.mySelfer)
   2.165 +                      accessToken: token, mySelfer: oauth2Params.mySelfer)
   2.166              } else {
   2.167                  loginViewModelOAuth2ErrorDelegate?.handle(
   2.168                      oauth2Error: OAuth2AuthViewModelError.noToken)
   2.169 @@ -252,5 +222,24 @@
   2.170  
   2.171  extension LoginViewModel: VerifiableAccountDelegate {
   2.172      func didEndVerification(result: Result<Void, Error>) {
   2.173 +        switch result {
   2.174 +        case .success(()):
   2.175 +            //remove obsolete code on EmailListViewController
   2.176 +            // TODO save
   2.177 +            /*
   2.178 +            MessageModelUtil.performAndWait {
   2.179 +                account.save()
   2.180 +            }
   2.181 +             */
   2.182 +            mySelfer?.startMySelf()
   2.183 +        case .failure(let error):
   2.184 +            break
   2.185 +        }
   2.186 +
   2.187 +        // Notify
   2.188 +        /*
   2.189 +        accountVerificationResultDelegate?.didVerify(result: result,
   2.190 +                                                     accountInput: verificationService)
   2.191 +         */
   2.192      }
   2.193  }