bsm-project/Friendvatars/AuthController.swift

67 lines
1.9 KiB
Swift
Raw Normal View History

2018-11-19 14:31:37 +01:00
import Foundation
import CryptoSwift
final class AuthController {
static let serviceName = "FriendvatarsService"
static var isSignedIn: Bool {
guard let currentUser = Settings.currentUser else {
return false
}
do {
let password = try KeychainPasswordItem(service: serviceName, account: currentUser.email).readPassword()
return password.count > 0
} catch {
return false
}
}
class func passwordHash(from email: String, password: String) -> String {
let salt = "x4vV8bGgqqmQwgCoyXFQj+(o.nUNQhVP7ND"
return "\(password).\(email).\(salt)".sha256()
}
2018-11-19 16:58:23 +01:00
class func logIn(_ user: User, password: String) throws {
do {
let finalHash = passwordHash(from: user.email, password: password)
let keychainPassword = try KeychainPasswordItem(service: serviceName, account: user.email).readPassword()
print(keychainPassword)
if(keychainPassword == finalHash) {
Settings.currentUser = user
NotificationCenter.default.post(name: .loginStatusChanged, object: nil)
}
} catch {
print("nie dziala")
}
}
2018-11-19 14:31:37 +01:00
class func signIn(_ user: User, password: String) throws {
let finalHash = passwordHash(from: user.email, password: password)
try KeychainPasswordItem(service: serviceName, account: user.email).savePassword(finalHash)
Settings.currentUser = user
NotificationCenter.default.post(name: .loginStatusChanged, object: nil)
}
class func signOut() throws {
guard let currentUser = Settings.currentUser else {
return
}
2018-11-19 16:58:23 +01:00
//try KeychainPasswordItem(service: serviceName, account: currentUser.email).deleteItem()
2018-11-19 14:31:37 +01:00
Settings.currentUser = nil
NotificationCenter.default.post(name: .loginStatusChanged, object: nil)
}
}
extension Notification.Name {
static let loginStatusChanged = Notification.Name("com.razeware.auth.changed")
}