|
|
@ -9,82 +9,51 @@ |
|
|
|
import Foundation |
|
|
|
|
|
|
|
class LoginInteractor { |
|
|
|
|
|
|
|
// MARK: Properties |
|
|
|
|
|
|
|
weak var output: LoginInteractorOutput? |
|
|
|
private let service: LoginServiceType |
|
|
|
|
|
|
|
// MARK: Initialization |
|
|
|
|
|
|
|
init(service: LoginServiceType) { |
|
|
|
self.service = service |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func toBase64(text: String) -> String? { |
|
|
|
guard let data = text.data(using: String.Encoding.utf8) else { |
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|
return data.base64EncodedString(options: Data.Base64EncodingOptions(rawValue: 0)) |
|
|
|
|
|
|
|
// MARK: Properties |
|
|
|
|
|
|
|
weak var output: LoginInteractorOutput? |
|
|
|
private let service: LoginServiceType |
|
|
|
|
|
|
|
// MARK: Initialization |
|
|
|
|
|
|
|
init(service: LoginServiceType) { |
|
|
|
self.service = service |
|
|
|
} |
|
|
|
|
|
|
|
func toBase64(text: String) -> String? { |
|
|
|
guard let data = text.data(using: String.Encoding.utf8) else { |
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|
private func isValid(userName: String, password: String) -> (isValid: Bool, error: Error){ |
|
|
|
var error = "" |
|
|
|
var isValid = true |
|
|
|
if userName.isEmpty { |
|
|
|
error = error + "\n" + "email_valid_error".localized(); |
|
|
|
isValid = false |
|
|
|
} |
|
|
|
|
|
|
|
if password.isEmpty {error = error + "\n" + "password_empty_error".localized(); |
|
|
|
isValid = false |
|
|
|
} |
|
|
|
let _error = NSError.init(domain: "LoginInteractor", code: 0, userInfo: [NSLocalizedDescriptionKey : error]) |
|
|
|
return (isValid, _error) |
|
|
|
} |
|
|
|
// MARK: Converting entities |
|
|
|
return data.base64EncodedString(options: Data.Base64EncodingOptions(rawValue: 0)) |
|
|
|
} |
|
|
|
|
|
|
|
// MARK: Converting entities |
|
|
|
} |
|
|
|
|
|
|
|
// MARK: Login interactor input interface |
|
|
|
|
|
|
|
extension LoginInteractor: LoginInteractorInput { |
|
|
|
|
|
|
|
|
|
|
|
func login(userName: String, password: String, encryptedPassword: String) { |
|
|
|
let username = userName |
|
|
|
let validationResult = self.isValid(userName: username, password: encryptedPassword) |
|
|
|
if !validationResult.isValid { |
|
|
|
self.output?.show(error: validationResult.error) |
|
|
|
return |
|
|
|
} |
|
|
|
guard let passwordBase64Data = self.toBase64(text: password) else { |
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
print("password converted is") |
|
|
|
print(passwordBase64Data) |
|
|
|
|
|
|
|
// send encrypted password to server for kftc |
|
|
|
self.service._login(userId: username, password: encryptedPassword, success: { (user) in |
|
|
|
|
|
|
|
let accessCode = user?.accessCode ?? "" |
|
|
|
let accessCodeBase64 = accessCode |
|
|
|
Utility.save(user: user, accessCodeBase64: accessCodeBase64, password: encryptedPassword, login: true) |
|
|
|
|
|
|
|
self.output?.loggedIn() |
|
|
|
|
|
|
|
}) { (error) in |
|
|
|
self.output?.show(error: error) |
|
|
|
guard |
|
|
|
let storedEmail = KeyChain.shared.get(key: .id), |
|
|
|
storedEmail == username else { |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
KeyChain.shared.removeAll() |
|
|
|
} |
|
|
|
|
|
|
|
func login(userName: String, encryptedPassword: String) { |
|
|
|
// send encrypted password to server for kftc |
|
|
|
self.service._login(userId: userName, password: encryptedPassword, success: { (user) in |
|
|
|
let accessCode = user?.accessCode ?? "" |
|
|
|
let accessCodeBase64 = accessCode |
|
|
|
Utility.save(user: user, accessCodeBase64: accessCodeBase64, password: encryptedPassword, login: true) |
|
|
|
|
|
|
|
self.output?.loggedIn() |
|
|
|
|
|
|
|
}) { (error) in |
|
|
|
self.output?.show(error: error) |
|
|
|
guard |
|
|
|
let storedEmail = KeyChain.shared.get(key: .id), |
|
|
|
storedEmail == userName else { |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
KeyChain.shared.removeAll() |
|
|
|
} |
|
|
|
} |
|
|
|
} |