PhoneAuthCredential.swift 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. enum CredentialKind {
  21. case phoneNumber(_ phoneNumber: String, _ temporaryProof: String)
  22. case verification(_ id: String, _ code: String)
  23. }
  24. let credentialKind: CredentialKind
  25. init(withTemporaryProof temporaryProof: String, phoneNumber: String,
  26. providerID: String) {
  27. credentialKind = .phoneNumber(phoneNumber, temporaryProof)
  28. super.init(provider: providerID)
  29. }
  30. init(withProviderID providerID: String, verificationID: String, verificationCode: String) {
  31. credentialKind = .verification(verificationID, verificationCode)
  32. super.init(provider: providerID)
  33. }
  34. // MARK: Secure Coding
  35. public static var supportsSecureCoding = true
  36. public func encode(with coder: NSCoder) {
  37. switch credentialKind {
  38. case let .phoneNumber(phoneNumber, temporaryProof):
  39. coder.encode(phoneNumber, forKey: "phoneNumber")
  40. coder.encode(temporaryProof, forKey: "temporaryProof")
  41. case let .verification(id, code):
  42. coder.encode(id, forKey: "verificationID")
  43. coder.encode(code, forKey: "verificationCode")
  44. }
  45. }
  46. public required init?(coder: NSCoder) {
  47. if let verificationID = coder.decodeObject(
  48. of: NSString.self,
  49. forKey: "verificationID"
  50. ) as? String,
  51. let verificationCode = coder.decodeObject(of: NSString.self, forKey: "verificationCode")
  52. as? String {
  53. credentialKind = .verification(verificationID, verificationCode)
  54. super.init(provider: PhoneAuthProvider.id)
  55. } else if let temporaryProof = coder.decodeObject(of: NSString.self, forKey: "temporaryProof"),
  56. let phoneNumber = coder.decodeObject(of: NSString.self, forKey: "phoneNumber") {
  57. credentialKind = .phoneNumber(phoneNumber as String, temporaryProof as String)
  58. super.init(provider: PhoneAuthProvider.id)
  59. } else {
  60. return nil
  61. }
  62. }
  63. }