PhoneAuthCredential.swift 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. /** @class PhoneAuthCredential
  16. @brief Implementation of FIRAuthCredential for Phone Auth credentials.
  17. This class is available on iOS only.
  18. */
  19. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  20. @objc(FIRPhoneAuthCredential) public class PhoneAuthCredential: AuthCredential, NSSecureCoding {
  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. 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(forKey: "temporaryProof") as? String,
  56. let phoneNumber = coder.decodeObject(forKey: "phoneNumber") as? String {
  57. credentialKind = .phoneNumber(phoneNumber, temporaryProof)
  58. super.init(provider: PhoneAuthProvider.id)
  59. } else {
  60. return nil
  61. }
  62. }
  63. }