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. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  21. @objc(FIRAuthAppCredentialManager) public class AuthAppCredentialManager: NSObject {
  22. let kKeychainDataKey = "app_credentials"
  23. let kFullCredentialKey = "full_credential"
  24. let kPendingReceiptsKey = "pending_receipts"
  25. /** @property credential
  26. @brief The full credential (which has a secret) to be used by the app, if one is available.
  27. */
  28. @objc public var credential: AuthAppCredential?
  29. /** @property maximumNumberOfPendingReceipts
  30. @brief The maximum (but not necessarily the minimum) number of pending receipts to be kept.
  31. @remarks Only tests should access this property.
  32. */
  33. @objc public let maximumNumberOfPendingReceipts = 32
  34. init(withKeychain keychain: AuthKeychainServices) {
  35. keychainServices = keychain
  36. if let encodedData = try? keychain.data(forKey: kKeychainDataKey),
  37. let unarchiver = try? NSKeyedUnarchiver(forReadingFrom: encodedData) {
  38. if let credential = unarchiver.decodeObject(of: AuthAppCredential.self,
  39. forKey: kFullCredentialKey) {
  40. self.credential = credential
  41. }
  42. if let pendingReceipts = unarchiver.decodeObject(
  43. of: [NSString.self, NSArray.self],
  44. forKey: kPendingReceiptsKey
  45. ) as? [String] {
  46. self.pendingReceipts = pendingReceipts
  47. }
  48. }
  49. }
  50. @objc public func didStartVerification(withReceipt receipt: String,
  51. timeout: TimeInterval,
  52. callback: @escaping (AuthAppCredential) -> Void) {
  53. pendingReceipts = pendingReceipts.filter { $0 != receipt }
  54. if pendingReceipts.count >= maximumNumberOfPendingReceipts {
  55. pendingReceipts.remove(at: 0)
  56. }
  57. pendingReceipts.append(receipt)
  58. callbacksByReceipt[receipt] = callback
  59. saveData()
  60. kAuthGlobalWorkQueue.asyncAfter(deadline: .now() + timeout) {
  61. self.callbackWithReceipt(receipt)
  62. }
  63. }
  64. @objc public func canFinishVerification(withReceipt receipt: String, secret: String) -> Bool {
  65. guard pendingReceipts.contains(receipt) else {
  66. return false
  67. }
  68. pendingReceipts = pendingReceipts.filter { $0 != receipt }
  69. credential = AuthAppCredential(receipt: receipt, secret: secret)
  70. saveData()
  71. callbackWithReceipt(receipt)
  72. return true
  73. }
  74. @objc public func clearCredential() {
  75. credential = nil
  76. saveData()
  77. }
  78. // MARK: Internal methods
  79. private func saveData() {
  80. let archiver = NSKeyedArchiver(requiringSecureCoding: true)
  81. archiver.encode(credential, forKey: kFullCredentialKey)
  82. archiver.encode(pendingReceipts, forKey: kPendingReceiptsKey)
  83. archiver.finishEncoding()
  84. try? keychainServices.setData(archiver.encodedData, 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: AuthKeychainServices
  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