From 6efc671aea231b83b672e4d2fd72da7db0bca847 Mon Sep 17 00:00:00 2001 From: Alexey Gerasimov Date: Tue, 25 Apr 2017 17:59:41 +0300 Subject: [PATCH] BasePassCodeService added --- .../project.pbxproj | 4 + .../Services/BasePassCodeService.swift | 99 +++++++++++++++++++ LeadKitAdditions/Podfile | 1 + LeadKitAdditions/Podfile.lock | 5 +- 4 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 LeadKitAdditions/LeadKitAdditions/Services/BasePassCodeService.swift diff --git a/LeadKitAdditions/LeadKitAdditions.xcodeproj/project.pbxproj b/LeadKitAdditions/LeadKitAdditions.xcodeproj/project.pbxproj index 8fc15cd..c0835e2 100644 --- a/LeadKitAdditions/LeadKitAdditions.xcodeproj/project.pbxproj +++ b/LeadKitAdditions/LeadKitAdditions.xcodeproj/project.pbxproj @@ -18,6 +18,7 @@ EF05EDC11EAF706200CAE7B6 /* BaseDateFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF05EDBE1EAF706200CAE7B6 /* BaseDateFormatter.swift */; }; EF05EDC21EAF706200CAE7B6 /* DefaultNetworkService.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF05EDBF1EAF706200CAE7B6 /* DefaultNetworkService.swift */; }; EF05EDC61EAF70EB00CAE7B6 /* TouchIDService.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF05EDC51EAF70EB00CAE7B6 /* TouchIDService.swift */; }; + EF05EDC81EAF91D500CAE7B6 /* BasePassCodeService.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF05EDC71EAF91D500CAE7B6 /* BasePassCodeService.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -36,6 +37,7 @@ EF05EDBE1EAF706200CAE7B6 /* BaseDateFormatter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BaseDateFormatter.swift; sourceTree = ""; }; EF05EDBF1EAF706200CAE7B6 /* DefaultNetworkService.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DefaultNetworkService.swift; sourceTree = ""; }; EF05EDC51EAF70EB00CAE7B6 /* TouchIDService.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TouchIDService.swift; sourceTree = ""; }; + EF05EDC71EAF91D500CAE7B6 /* BasePassCodeService.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BasePassCodeService.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -102,6 +104,7 @@ CAE698F31E968E28000394B0 /* Services */ = { isa = PBXGroup; children = ( + EF05EDC71EAF91D500CAE7B6 /* BasePassCodeService.swift */, EF05EDB31EAF703A00CAE7B6 /* BaseUserService.swift */, EF05EDC51EAF70EB00CAE7B6 /* TouchIDService.swift */, ); @@ -288,6 +291,7 @@ EF05EDC01EAF706200CAE7B6 /* ApiResponse.swift in Sources */, EF05EDBC1EAF705500CAE7B6 /* ConnectionError.swift in Sources */, EF05EDB41EAF703A00CAE7B6 /* BaseUserService.swift in Sources */, + EF05EDC81EAF91D500CAE7B6 /* BasePassCodeService.swift in Sources */, EF05EDC11EAF706200CAE7B6 /* BaseDateFormatter.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/LeadKitAdditions/LeadKitAdditions/Services/BasePassCodeService.swift b/LeadKitAdditions/LeadKitAdditions/Services/BasePassCodeService.swift new file mode 100644 index 0000000..6bec7f9 --- /dev/null +++ b/LeadKitAdditions/LeadKitAdditions/Services/BasePassCodeService.swift @@ -0,0 +1,99 @@ +// +// Copyright (c) 2017 Touch Instinct +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the Software), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// + +import KeychainAccess +import CocoaLumberjack + +open class BasePassCodeService { + + open class var keychainService: String { + return Bundle.main.bundleIdentifier ?? "" + } + + public init() { + let isInitialLoad = UserDefaults.standard.bool(forKey: Keys.isInitialLoad) + if isInitialLoad { + UserDefaults.standard.set(false, forKey: Keys.isInitialLoad) + reset() + } + } + + // MARK: - Private stuff + + fileprivate lazy var keychain: Keychain = { + return Keychain(service: keychainService) + .synchronizable(false) + }() + + fileprivate var passCodeHash: String? { + return keychain[Keys.passCodeHash] + } + + fileprivate enum Keys { + static let passCodeHash = "passCodeHash" + static let isTouchIdEnabled = "isTouchIdEnabled" + static let isInitialLoad = "isInitialLoad" + } + + fileprivate enum Values { + static let touchIdEnabled = "touchIdEnabled" + } + +} + +extension BasePassCodeService { + + public var isPassCodeSaved: Bool { + return keychain[Keys.passCodeHash] != nil + } + + public var isTouchIdEnabled: Bool { + get { + return keychain[Keys.isTouchIdEnabled] == Values.touchIdEnabled + } + set { + keychain[Keys.isTouchIdEnabled] = newValue ? Values.touchIdEnabled : nil + } + } + + public func save(passCode: String?) { + keychain[Keys.passCodeHash] = passCode?.hashMD5 + } + + public func check(passCode: String) -> Bool { + return passCode.hashMD5 == passCodeHash + } + + public func reset() { + save(passCode: nil) + isTouchIdEnabled = false + } + +} + +private extension String { + + var hashMD5: String? { + return self + } + +} diff --git a/LeadKitAdditions/Podfile b/LeadKitAdditions/Podfile index 2629275..05844c4 100644 --- a/LeadKitAdditions/Podfile +++ b/LeadKitAdditions/Podfile @@ -8,6 +8,7 @@ target 'LeadKitAdditions' do pod 'LeadKit', :git => 'https://github.com/TouchInstinct/LeadKit.git', :branch => 'fix/sharedApplication', :commit => 'fd0eb18b8a6680ff16bbb1668d1ae0d29f29fad7' pod 'TableKit' + pod 'KeychainAccess' end diff --git a/LeadKitAdditions/Podfile.lock b/LeadKitAdditions/Podfile.lock index dcb0d7d..343610a 100644 --- a/LeadKitAdditions/Podfile.lock +++ b/LeadKitAdditions/Podfile.lock @@ -3,6 +3,7 @@ PODS: - CocoaLumberjack/Default (3.1.0) - CocoaLumberjack/Swift (3.1.0): - CocoaLumberjack/Default + - KeychainAccess (3.0.2) - LeadKit (0.4.6): - CocoaLumberjack/Swift (~> 3.1.0) - ObjectMapper (~> 2.1) @@ -23,6 +24,7 @@ PODS: - Toast-Swift (2.0.0) DEPENDENCIES: + - KeychainAccess - LeadKit (from `https://github.com/TouchInstinct/LeadKit.git`, commit `fd0eb18b8a6680ff16bbb1668d1ae0d29f29fad7`, branch `fix/sharedApplication`) - TableKit @@ -40,6 +42,7 @@ CHECKOUT OPTIONS: SPEC CHECKSUMS: Alamofire: dc44b1600b800eb63da6a19039a0083d62a6a62d CocoaLumberjack: 8311463ddf9ee86a06ef92a071dd656c89244500 + KeychainAccess: a986406022dfc7c634c691ad3bec670cc6a32002 LeadKit: d688a8bef79de7bbd83d553da3cb6c5292d48f2d ObjectMapper: fb30f71e08470d1e5a20b199fafe1246281db898 RxAlamofire: 0b1fa48f545fffe7f7a28af2086bcaa3b5946cc9 @@ -48,6 +51,6 @@ SPEC CHECKSUMS: TableKit: 02e041b443f75fa3e9f1ee6024d4b256305bd904 Toast-Swift: 5b2f8f720f7e78e48511f693df1f9c9a6e38a25a -PODFILE CHECKSUM: ce4fe179e6470b751617d19baacbce25502a7002 +PODFILE CHECKSUM: f6bfca600b479c264f39b85593af2629edff3515 COCOAPODS: 1.2.1