PhoneAuthCredential.swift 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. import Foundation
  15. /// Implementation of AuthCredential for Phone Auth credentials.
  16. ///
  17. /// This class is available on iOS only.
  18. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  19. @objc(FIRPhoneAuthCredential) open class PhoneAuthCredential: AuthCredential, NSSecureCoding,
  20. @unchecked Sendable {
  21. enum CredentialKind {
  22. case phoneNumber(_ phoneNumber: String, _ temporaryProof: String)
  23. case verification(_ id: String, _ code: String)
  24. }
  25. let credentialKind: CredentialKind
  26. init(withTemporaryProof temporaryProof: String, phoneNumber: String,
  27. providerID: String) {
  28. credentialKind = .phoneNumber(phoneNumber, temporaryProof)
  29. super.init(provider: providerID)
  30. }
  31. init(withProviderID providerID: String, verificationID: String, verificationCode: String) {
  32. credentialKind = .verification(verificationID, verificationCode)
  33. super.init(provider: providerID)
  34. }
  35. // MARK: Secure Coding
  36. public static let supportsSecureCoding = true
  37. public func encode(with coder: NSCoder) {
  38. switch credentialKind {
  39. case let .phoneNumber(phoneNumber, temporaryProof):
  40. coder.encode(phoneNumber, forKey: "phoneNumber")
  41. coder.encode(temporaryProof, forKey: "temporaryProof")
  42. case let .verification(id, code):
  43. coder.encode(id, forKey: "verificationID")
  44. coder.encode(code, forKey: "verificationCode")
  45. }
  46. }
  47. public required init?(coder: NSCoder) {
  48. if let verificationID = coder.decodeObject(
  49. of: NSString.self,
  50. forKey: "verificationID"
  51. ) as? String,
  52. let verificationCode = coder.decodeObject(of: NSString.self, forKey: "verificationCode")
  53. as? String {
  54. credentialKind = .verification(verificationID, verificationCode)
  55. super.init(provider: PhoneAuthProvider.id)
  56. } else if let temporaryProof = coder.decodeObject(of: NSString.self, forKey: "temporaryProof"),
  57. let phoneNumber = coder.decodeObject(of: NSString.self, forKey: "phoneNumber") {
  58. credentialKind = .phoneNumber(phoneNumber as String, temporaryProof as String)
  59. super.init(provider: PhoneAuthProvider.id)
  60. } else {
  61. return nil
  62. }
  63. }
  64. }