FIRMultiFactor.m 9.2 KB

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