AuthAppCredentialManager.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright 2023 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #if !os(macOS)
  15. import Foundation
  16. // TODO: Nothing should be public
  17. /** @class FIRAuthAppCredentialManager
  18. @brief A class to manage app credentials backed by iOS Keychain.
  19. */
  20. @objc(FIRAuthAppCredentialManager) public class AuthAppCredentialManager: NSObject {
  21. let kKeychainDataKey = "app_credentials"
  22. let kFullCredentialKey = "full_credential"
  23. let kPendingReceiptsKey = "pending_receipts"
  24. /** @property credential
  25. @brief The full credential (which has a secret) to be used by the app, if one is available.
  26. */
  27. @objc public var credential: AuthAppCredential?
  28. /** @property maximumNumberOfPendingReceipts
  29. @brief The maximum (but not necessarily the minimum) number of pending receipts to be kept.
  30. @remarks Only tests should access this property.
  31. */
  32. @objc public let maximumNumberOfPendingReceipts = 32
  33. init(withKeychain keychain: AuthStorage) {
  34. keychainServices = keychain
  35. if let encodedData = try? keychain.data(forKey: kKeychainDataKey),
  36. let unarchiver = try? NSKeyedUnarchiver(forReadingFrom: encodedData) {
  37. if let credential = unarchiver.decodeObject(of: AuthAppCredential.self,
  38. forKey: kFullCredentialKey) {
  39. self.credential = credential
  40. }
  41. if let pendingReceipts = unarchiver.decodeObject(
  42. of: [NSString.self, NSArray.self],
  43. forKey: kPendingReceiptsKey
  44. ) as? [String] {
  45. self.pendingReceipts = pendingReceipts
  46. }
  47. }
  48. }
  49. @objc public func didStartVerification(withReceipt receipt: String,
  50. timeout: TimeInterval,
  51. callback: @escaping (AuthAppCredential) -> Void) {
  52. pendingReceipts = pendingReceipts.filter { $0 != receipt }
  53. if pendingReceipts.count >= maximumNumberOfPendingReceipts {
  54. pendingReceipts.remove(at: 0)
  55. }
  56. pendingReceipts.append(receipt)
  57. callbacksByReceipt[receipt] = callback
  58. saveData()
  59. kAuthGlobalWorkQueue.asyncAfter(deadline: .now() + timeout) {
  60. self.callbackWithReceipt(receipt)
  61. }
  62. }
  63. @objc public func canFinishVerification(withReceipt receipt: String, secret: String) -> Bool {
  64. guard pendingReceipts.contains(receipt) else {
  65. return false
  66. }
  67. pendingReceipts = pendingReceipts.filter { $0 != receipt }
  68. credential = AuthAppCredential(receipt: receipt, secret: secret)
  69. saveData()
  70. callbackWithReceipt(receipt)
  71. return true
  72. }
  73. @objc public func clearCredential() {
  74. credential = nil
  75. saveData()
  76. }
  77. // MARK: Internal methods
  78. private func saveData() {
  79. let archiveData = NSMutableData()
  80. let archiver = NSKeyedArchiver(forWritingWith: archiveData)
  81. archiver.encode(credential, forKey: kFullCredentialKey)
  82. archiver.encode(pendingReceipts, forKey: kPendingReceiptsKey)
  83. archiver.finishEncoding()
  84. try? keychainServices.setData(archiveData as Data, forKey: kKeychainDataKey)
  85. }
  86. private func callbackWithReceipt(_ receipt: String) {
  87. guard let callback = callbacksByReceipt[receipt] else {
  88. return
  89. }
  90. callbacksByReceipt.removeValue(forKey: receipt)
  91. if let fakeCredential {
  92. callback(fakeCredential)
  93. } else if let credential {
  94. callback(credential)
  95. } else {
  96. callback(AuthAppCredential(receipt: receipt, secret: nil))
  97. }
  98. }
  99. /** @var _keychainServices
  100. @brief The keychain for app credentials to load from and to save to.
  101. */
  102. private let keychainServices: AuthStorage
  103. /** @var pendingReceipts
  104. @brief A list of pending receipts sorted in the order they were recorded.
  105. */
  106. private var pendingReceipts: [String] = []
  107. /** @var callbacksByReceipt
  108. @brief A map from pending receipts to callbacks.
  109. */
  110. private var callbacksByReceipt: [String: (AuthAppCredential) -> Void] = [:]
  111. // Only for testing.
  112. var fakeCredential: AuthAppCredential?
  113. }
  114. #endif