FIRMultiFactor.m 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright 2019 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/LICENSE2.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/FIRMultiFactor.h"
  19. #import "FirebaseAuth/Sources/Auth/FIRAuth_Internal.h"
  20. #import "FirebaseAuth/Sources/MultiFactor/FIRMultiFactor+Internal.h"
  21. #import "FirebaseAuth-Swift.h"
  22. NS_ASSUME_NONNULL_BEGIN
  23. static NSString *kEnrolledFactorsCodingKey = @"enrolledFactors";
  24. static NSString *kUserCodingKey = @"user";
  25. @implementation FIRMultiFactor
  26. - (void)getSessionWithCompletion:(nullable FIRMultiFactorSessionCallback)completion {
  27. FIRMultiFactorSession *session = [FIRMultiFactorSession sessionForCurrentUser];
  28. if (completion) {
  29. completion(session, nil);
  30. }
  31. }
  32. - (void)enrollWithAssertion:(FIRMultiFactorAssertion *)assertion
  33. displayName:(nullable NSString *)displayName
  34. completion:(nullable FIRAuthVoidErrorCallback)completion {
  35. #if TARGET_OS_IOS
  36. FIRPhoneMultiFactorAssertion *phoneAssertion = (FIRPhoneMultiFactorAssertion *)assertion;
  37. FIRAuthProtoFinalizeMFAPhoneRequestInfo *finalizeMFAPhoneRequestInfo =
  38. [[FIRAuthProtoFinalizeMFAPhoneRequestInfo alloc]
  39. initWithSessionInfo:phoneAssertion.authCredential.verificationID
  40. verificationCode:phoneAssertion.authCredential.verificationCode];
  41. FIRFinalizeMFAEnrollmentRequest *request =
  42. [[FIRFinalizeMFAEnrollmentRequest alloc] initWithIDToken:self.user.tokenService.accessToken
  43. displayName:displayName
  44. verificationInfo:finalizeMFAPhoneRequestInfo
  45. requestConfiguration:self.user.requestConfiguration];
  46. [FIRAuthBackend2
  47. postWithRequest:request
  48. callback:^(FIRFinalizeMFAEnrollmentResponse *_Nullable response,
  49. NSError *_Nullable error) {
  50. if (error) {
  51. if (completion) {
  52. completion(error);
  53. }
  54. } else {
  55. [FIRAuth.auth
  56. completeSignInWithAccessToken:response.IDToken
  57. accessTokenExpirationDate:nil
  58. refreshToken:response.refreshToken
  59. anonymous:NO
  60. callback:^(FIRUser *_Nullable user,
  61. NSError *_Nullable error) {
  62. FIRAuthDataResult *result =
  63. [[FIRAuthDataResult alloc] initWithUser:user
  64. additionalUserInfo:nil
  65. credential:nil];
  66. FIRAuthDataResultCallback decoratedCallback = [FIRAuth
  67. .auth
  68. signInFlowAuthDataResultCallbackByDecoratingCallback:
  69. ^(FIRAuthDataResult *_Nullable authResult,
  70. NSError *_Nullable error) {
  71. if (completion) {
  72. completion(error);
  73. }
  74. }];
  75. decoratedCallback(result, error);
  76. }];
  77. }
  78. }];
  79. #endif
  80. }
  81. - (void)unenrollWithInfo:(FIRMultiFactorInfo *)factorInfo
  82. completion:(nullable FIRAuthVoidErrorCallback)completion {
  83. [self unenrollWithFactorUID:factorInfo.UID completion:completion];
  84. }
  85. - (void)unenrollWithFactorUID:(NSString *)factorUID
  86. completion:(nullable FIRAuthVoidErrorCallback)completion {
  87. FIRWithdrawMFARequest *request =
  88. [[FIRWithdrawMFARequest alloc] initWithIDToken:self.user.tokenService.accessToken
  89. MFAEnrollmentID:factorUID
  90. requestConfiguration:self.user.requestConfiguration];
  91. [FIRAuthBackend2
  92. postWithRequest:request
  93. callback:^(FIRWithdrawMFAResponse *_Nullable response, NSError *_Nullable error) {
  94. if (error) {
  95. if (completion) {
  96. completion(error);
  97. }
  98. } else {
  99. [FIRAuth.auth
  100. completeSignInWithAccessToken:response.IDToken
  101. accessTokenExpirationDate:nil
  102. refreshToken:response.refreshToken
  103. anonymous:NO
  104. callback:^(FIRUser *_Nullable user,
  105. NSError *_Nullable error) {
  106. FIRAuthDataResult *result =
  107. [[FIRAuthDataResult alloc] initWithUser:user
  108. additionalUserInfo:nil
  109. credential:nil];
  110. FIRAuthDataResultCallback decoratedCallback = [FIRAuth
  111. .auth
  112. signInFlowAuthDataResultCallbackByDecoratingCallback:
  113. ^(FIRAuthDataResult *_Nullable authResult,
  114. NSError *_Nullable error) {
  115. if (error) {
  116. [[FIRAuth auth] signOut:NULL];
  117. }
  118. if (completion) {
  119. completion(error);
  120. }
  121. }];
  122. decoratedCallback(result, error);
  123. }];
  124. }
  125. }];
  126. }
  127. #pragma mark - Internal
  128. - (instancetype)initWithMFAEnrollments:(NSArray<FIRAuthProtoMFAEnrollment *> *)MFAEnrollments {
  129. self = [super init];
  130. if (self) {
  131. NSMutableArray<FIRMultiFactorInfo *> *multiFactorInfoArray = [[NSMutableArray alloc] init];
  132. for (FIRAuthProtoMFAEnrollment *MFAEnrollment in MFAEnrollments) {
  133. if (MFAEnrollment.phoneInfo) {
  134. FIRMultiFactorInfo *multiFactorInfo =
  135. [[FIRPhoneMultiFactorInfo alloc] initWithProto:MFAEnrollment];
  136. [multiFactorInfoArray addObject:multiFactorInfo];
  137. }
  138. }
  139. _enrolledFactors = [multiFactorInfoArray copy];
  140. }
  141. return self;
  142. }
  143. #pragma mark - NSSecureCoding
  144. + (BOOL)supportsSecureCoding {
  145. return YES;
  146. }
  147. - (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
  148. self = [self init];
  149. if (self) {
  150. NSSet *enrolledFactorsClasses = [NSSet setWithArray:@[
  151. [NSArray class], [FIRMultiFactorInfo class], [FIRPhoneMultiFactorInfo class]
  152. ]];
  153. NSArray<FIRMultiFactorInfo *> *enrolledFactors =
  154. [aDecoder decodeObjectOfClasses:enrolledFactorsClasses forKey:kEnrolledFactorsCodingKey];
  155. _enrolledFactors = enrolledFactors;
  156. // Do not decode `user` weak property.
  157. }
  158. return self;
  159. }
  160. - (void)encodeWithCoder:(NSCoder *)aCoder {
  161. [aCoder encodeObject:_enrolledFactors forKey:kEnrolledFactorsCodingKey];
  162. // Do not encode `user` weak property.
  163. }
  164. @end
  165. NS_ASSUME_NONNULL_END
  166. #endif