FIRMultiFactor.m 9.4 KB

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