IOS-1542 Collapse create/update code for credentials. IOS-1542
authorDirk Zimmermann <dz@pep.security>
Thu, 25 Apr 2019 16:25:40 +0200
branchIOS-1542
changeset 8379f19109e055f9
parent 8378 73a67e61d662
child 8380 fd237aaceec6
IOS-1542 Collapse create/update code for credentials.
pEpForiOS/Models/VerifiableAccount/VerifiableAccount.swift
     1.1 --- a/pEpForiOS/Models/VerifiableAccount/VerifiableAccount.swift	Thu Apr 25 16:25:40 2019 +0200
     1.2 +++ b/pEpForiOS/Models/VerifiableAccount/VerifiableAccount.swift	Thu Apr 25 16:25:40 2019 +0200
     1.3 @@ -194,8 +194,8 @@
     1.4                  trusted: false,
     1.5                  transport: transportSMTP)
     1.6  
     1.7 -            let credentialsImap = theImapServer.credentials ?? createCredentials(
     1.8 -                context: moc,
     1.9 +            let credentialsImap = update(
    1.10 +                credentials: theImapServer.credentials ?? CdServerCredentials.create(context: moc),
    1.11                  loginName: loginName,
    1.12                  address: address,
    1.13                  password: password,
    1.14 @@ -203,8 +203,8 @@
    1.15              credentialsImap.servers = NSSet(array: [theImapServer])
    1.16              theImapServer.credentials = credentialsImap
    1.17  
    1.18 -            let credentialsSmtp = theSmtpServer.credentials ?? createCredentials(
    1.19 -                context: moc,
    1.20 +            let credentialsSmtp = update(
    1.21 +                credentials: theSmtpServer.credentials ?? CdServerCredentials.create(context: moc),
    1.22                  loginName: loginName,
    1.23                  address: address,
    1.24                  password: password,
    1.25 @@ -289,32 +289,32 @@
    1.26          identity.userID = CdIdentity.pEpOwnUserID
    1.27      }
    1.28  
    1.29 -    /// Create credentials for the given parameters.
    1.30 +    /// Updates credentials with the given parameters.
    1.31      ///
    1.32      /// - Note: There is either an ordinary password, so a key chain entry
    1.33      ///         gets produced, or an access token (for OAUTH2),
    1.34      ///         in which case the token gets persisted into the key chain.
    1.35 -    private func createCredentials(context: NSManagedObjectContext,
    1.36 -                                   loginName: String?,
    1.37 -                                   address: String?,
    1.38 -                                   password: String?,
    1.39 -                                   accessToken: OAuth2AccessTokenProtocol?)
    1.40 -        -> CdServerCredentials {
    1.41 -            let credentials = CdServerCredentials.create(context: context)
    1.42 -            credentials.loginName = loginName ?? address
    1.43 +    private func update(credentials: CdServerCredentials,
    1.44 +                        loginName: String?,
    1.45 +                        address: String?,
    1.46 +                        password: String?,
    1.47 +                        accessToken: OAuth2AccessTokenProtocol?) -> CdServerCredentials {
    1.48 +        credentials.loginName = loginName ?? address
    1.49  
    1.50 -            let keyChainId = UUID().uuidString
    1.51 -            var payload: String? = nil
    1.52 -            if let token = accessToken {
    1.53 -                payload = token.persistBase64Encoded()
    1.54 -            } else {
    1.55 -                payload = password
    1.56 -            }
    1.57 +        var payload: String? = nil
    1.58 +        if let token = accessToken {
    1.59 +            payload = token.persistBase64Encoded()
    1.60 +        } else {
    1.61 +            payload = password
    1.62 +        }
    1.63  
    1.64 -            KeyChain.updateCreateOrDelete(password: payload, forKey: keyChainId)
    1.65 -            credentials.key = keyChainId
    1.66 +        // Reuse key, or create a new one.
    1.67 +        // In any case, update the payload (the password or a current OAUTH2 token).
    1.68 +        let keyChainId = credentials.key ?? UUID().uuidString
    1.69 +        credentials.key = keyChainId
    1.70 +        KeyChain.updateCreateOrDelete(password: payload, forKey: keyChainId)
    1.71  
    1.72 -            return credentials
    1.73 +        return credentials
    1.74      }
    1.75  
    1.76      private func update(server: CdServer,