|
| 1 | +// |
| 2 | +// StudyplusKeychain.swift |
| 3 | +// StudyplusSDK |
| 4 | +// |
| 5 | +// The MIT License (MIT) |
| 6 | +// |
| 7 | +// Copyright (c) 2021 Studyplus inc. |
| 8 | +// |
| 9 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | +// of this software and associated documentation files (the "Software"), to deal |
| 11 | +// in the Software without restriction, including without limitation the rights |
| 12 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | +// copies of the Software, and to permit persons to whom the Software is |
| 14 | +// furnished to do so, subject to the following conditions: |
| 15 | +// |
| 16 | +// The above copyright notice and this permission notice shall be included in |
| 17 | +// all copies or substantial portions of the Software. |
| 18 | +// |
| 19 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | +// THE SOFTWARE. |
| 26 | + |
| 27 | +import Foundation |
| 28 | + |
| 29 | +internal struct StudyplusKeychain { |
| 30 | + private static let accessTokenStoreKey: String = "accessToken" |
| 31 | + private static let usernameStoreKey: String = "username" |
| 32 | + |
| 33 | + static internal func accessToken(serviceName: String) -> String? { |
| 34 | + let query = [ |
| 35 | + kSecClass: kSecClassGenericPassword, |
| 36 | + kSecAttrService: serviceName, |
| 37 | + kSecAttrSynchronizable: kSecAttrSynchronizableAny, |
| 38 | + kSecMatchLimit: kSecMatchLimitOne, |
| 39 | + kSecReturnData: true, |
| 40 | + kSecAttrAccount: accessTokenStoreKey |
| 41 | + ] as CFDictionary |
| 42 | + |
| 43 | + var item: CFTypeRef? |
| 44 | + let status = SecItemCopyMatching(query, &item) |
| 45 | + guard status == errSecSuccess, let data = item as? Data else { |
| 46 | + return nil |
| 47 | + } |
| 48 | + |
| 49 | + return String(data: data, encoding: .utf8) |
| 50 | + } |
| 51 | + |
| 52 | + static internal func username(serviceName: String) -> String? { |
| 53 | + let query = [ |
| 54 | + kSecClass: kSecClassGenericPassword, |
| 55 | + kSecAttrService: serviceName, |
| 56 | + kSecAttrSynchronizable: kSecAttrSynchronizableAny, |
| 57 | + kSecMatchLimit: kSecMatchLimitOne, |
| 58 | + kSecReturnData: true, |
| 59 | + kSecAttrAccount: usernameStoreKey |
| 60 | + ] as CFDictionary |
| 61 | + |
| 62 | + var item: CFTypeRef? |
| 63 | + let status = SecItemCopyMatching(query, &item) |
| 64 | + guard status == errSecSuccess, let data = item as? Data else { |
| 65 | + return nil |
| 66 | + } |
| 67 | + |
| 68 | + return String(data: data, encoding: .utf8) |
| 69 | + } |
| 70 | + |
| 71 | + static internal func set(serviceName: String, |
| 72 | + accessToken: Data, |
| 73 | + username: Data, |
| 74 | + completion: (Result<Void, StudyplusLoginError>) -> Void) { |
| 75 | + // delete previous keys |
| 76 | + deleteAll(serviceName: serviceName) |
| 77 | + |
| 78 | + // add new token and username |
| 79 | + let statusAccessToken = SecItemAdd([ |
| 80 | + kSecClass: kSecClassGenericPassword, |
| 81 | + kSecAttrService: serviceName, |
| 82 | + kSecAttrSynchronizable: kSecAttrSynchronizableAny, |
| 83 | + kSecAttrAccount: accessTokenStoreKey, |
| 84 | + kSecValueData: accessToken |
| 85 | + ] as CFDictionary, nil) |
| 86 | + let statusUsername = SecItemAdd([ |
| 87 | + kSecClass: kSecClassGenericPassword, |
| 88 | + kSecAttrService: serviceName, |
| 89 | + kSecAttrSynchronizable: kSecAttrSynchronizableAny, |
| 90 | + kSecAttrAccount: usernameStoreKey, |
| 91 | + kSecValueData: username |
| 92 | + ] as CFDictionary, nil) |
| 93 | + |
| 94 | + if statusAccessToken != noErr || statusUsername != noErr { |
| 95 | + completion(.failure(.keychainError)) |
| 96 | + return |
| 97 | + } |
| 98 | + |
| 99 | + completion(.success(Void())) |
| 100 | + } |
| 101 | + |
| 102 | + static func deleteAll(serviceName: String) { |
| 103 | + SecItemDelete([ |
| 104 | + kSecClass: kSecClassGenericPassword, |
| 105 | + kSecAttrService: serviceName, |
| 106 | + kSecAttrSynchronizable: kSecAttrSynchronizableAny |
| 107 | + ] as CFDictionary) |
| 108 | + } |
| 109 | +} |
0 commit comments