FIRPhoneAuthCredential.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <TargetConditionals.h>
  17. #if TARGET_OS_IOS
  18. #import "FirebaseAuth/Sources/Public/FirebaseAuth/FIRPhoneAuthCredential.h"
  19. #import "FirebaseAuth/Sources/AuthProvider/FIRAuthCredential_Internal.h"
  20. #import "FirebaseAuth/Sources/Backend/RPC/FIRVerifyAssertionRequest.h"
  21. #import "FirebaseAuth/Sources/Utilities/FIRAuthExceptionUtils.h"
  22. #if TARGET_OS_IOS
  23. #import "FirebaseAuth/Sources/AuthProvider/Phone/FIRPhoneAuthCredential_Internal.h"
  24. #endif
  25. NS_ASSUME_NONNULL_BEGIN
  26. @interface FIRPhoneAuthCredential ()
  27. - (nullable instancetype)initWithProvider:(NSString *)provider NS_UNAVAILABLE;
  28. @end
  29. @implementation FIRPhoneAuthCredential
  30. - (instancetype)initWithTemporaryProof:(NSString *)temporaryProof
  31. phoneNumber:(NSString *)phoneNumber
  32. providerID:(NSString *)providerID {
  33. self = [super initWithProvider:providerID];
  34. if (self) {
  35. _temporaryProof = [temporaryProof copy];
  36. _phoneNumber = [phoneNumber copy];
  37. }
  38. return self;
  39. }
  40. - (nullable instancetype)initWithProvider:(NSString *)provider {
  41. [FIRAuthExceptionUtils
  42. raiseMethodNotImplementedExceptionWithReason:@"Please call the designated initializer."];
  43. return nil;
  44. }
  45. - (instancetype)initWithProviderID:(NSString *)providerID
  46. verificationID:(NSString *)verificationID
  47. verificationCode:(NSString *)verificationCode {
  48. self = [super initWithProvider:providerID];
  49. if (self) {
  50. _verificationID = [verificationID copy];
  51. _verificationCode = [verificationCode copy];
  52. }
  53. return self;
  54. }
  55. #pragma mark - NSSecureCoding
  56. + (BOOL)supportsSecureCoding {
  57. return YES;
  58. }
  59. - (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
  60. NSString *verificationID = [aDecoder decodeObjectOfClass:[NSString class]
  61. forKey:@"verificationID"];
  62. NSString *verificationCode = [aDecoder decodeObjectOfClass:[NSString class]
  63. forKey:@"verificationCode"];
  64. NSString *temporaryProof = [aDecoder decodeObjectOfClass:[NSString class]
  65. forKey:@"temporaryProof"];
  66. NSString *phoneNumber = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"phoneNumber"];
  67. if (temporaryProof.length && phoneNumber.length) {
  68. self = [self initWithTemporaryProof:temporaryProof
  69. phoneNumber:phoneNumber
  70. providerID:self.provider];
  71. } else if (verificationID.length && verificationCode.length) {
  72. self = [self initWithProviderID:self.provider
  73. verificationID:verificationID
  74. verificationCode:verificationCode];
  75. } else {
  76. self = nil;
  77. }
  78. return self;
  79. }
  80. - (void)encodeWithCoder:(NSCoder *)aCoder {
  81. [aCoder encodeObject:self.verificationID forKey:@"verificationID"];
  82. [aCoder encodeObject:self.verificationCode forKey:@"verificationCode"];
  83. [aCoder encodeObject:self.temporaryProof forKey:@"temporaryProof"];
  84. [aCoder encodeObject:self.phoneNumber forKey:@"phoneNumber"];
  85. }
  86. @end
  87. NS_ASSUME_NONNULL_END
  88. #endif