FIRPhoneAuthCredential.m 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. #include <TargetConditionals.h>
  17. #if !TARGET_OS_OSX && !TARGET_OS_TV
  18. #import "FIRPhoneAuthCredential.h"
  19. #import "FIRPhoneAuthCredential_Internal.h"
  20. #import "FIRAuthCredential_Internal.h"
  21. #import "FIRAuthExceptionUtils.h"
  22. #import "FIRVerifyAssertionRequest.h"
  23. NS_ASSUME_NONNULL_BEGIN
  24. @interface FIRPhoneAuthCredential ()
  25. - (nullable instancetype)initWithProvider:(NSString *)provider NS_UNAVAILABLE;
  26. @end
  27. @implementation FIRPhoneAuthCredential
  28. - (instancetype)initWithTemporaryProof:(NSString *)temporaryProof
  29. phoneNumber:(NSString *)phoneNumber
  30. providerID:(NSString *)providerID {
  31. self = [super initWithProvider:providerID];
  32. if (self) {
  33. _temporaryProof = [temporaryProof copy];
  34. _phoneNumber = [phoneNumber copy];
  35. }
  36. return self;
  37. }
  38. - (nullable instancetype)initWithProvider:(NSString *)provider {
  39. [FIRAuthExceptionUtils raiseMethodNotImplementedExceptionWithReason:
  40. @"Please call the designated initializer."];
  41. return nil;
  42. }
  43. - (instancetype)initWithProviderID:(NSString *)providerID
  44. verificationID:(NSString *)verificationID
  45. verificationCode:(NSString *)verificationCode {
  46. self = [super initWithProvider:providerID];
  47. if (self) {
  48. _verificationID = [verificationID copy];
  49. _verificationCode = [verificationCode copy];
  50. }
  51. return self;
  52. }
  53. #pragma mark - NSSecureCoding
  54. + (BOOL)supportsSecureCoding {
  55. return YES;
  56. }
  57. - (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
  58. NSString *verificationID = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"verificationID"];
  59. NSString *verificationCode = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"verificationCode"];
  60. NSString *temporaryProof = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"temporaryProof"];
  61. NSString *phoneNumber = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"phoneNumber"];
  62. if (temporaryProof.length && phoneNumber.length) {
  63. self = [self initWithTemporaryProof:temporaryProof phoneNumber:phoneNumber providerID:self.provider];
  64. } else if (verificationID.length && verificationCode.length) {
  65. self = [self initWithProviderID:self.provider verificationID:verificationID verificationCode:verificationCode];
  66. } else {
  67. self = nil;
  68. }
  69. return self;
  70. }
  71. - (void)encodeWithCoder:(NSCoder *)aCoder {
  72. [aCoder encodeObject:self.verificationID forKey:@"verificationID"];
  73. [aCoder encodeObject:self.verificationCode forKey:@"verificationCode"];
  74. [aCoder encodeObject:self.temporaryProof forKey:@"temporaryProof"];
  75. [aCoder encodeObject:self.phoneNumber forKey:@"phoneNumber"];
  76. }
  77. @end
  78. NS_ASSUME_NONNULL_END
  79. #endif