FIRMultiFactorResolver.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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/FIRAdditionalUserInfo.h"
  19. #import "FirebaseAuth/Sources/Public/FirebaseAuth/FIRMultiFactorResolver.h"
  20. #import "FirebaseAuth/Sources/Auth/FIRAuthDataResult_Internal.h"
  21. #import "FirebaseAuth/Sources/Auth/FIRAuth_Internal.h"
  22. #import "FirebaseAuth/Sources/Backend/FIRAuthBackend+MultiFactor.h"
  23. #import "FirebaseAuth/Sources/Backend/RPC/MultiFactor/SignIn/FIRFinalizeMFASignInRequest.h"
  24. #import "FirebaseAuth/Sources/Backend/RPC/Proto/Phone/FIRAuthProtoFinalizeMFAPhoneRequestInfo.h"
  25. #import "FirebaseAuth/Sources/Backend/RPC/Proto/TOTP/FIRAuthProtoFinalizeMFATOTPSignInRequestInfo.h"
  26. #import "FirebaseAuth/Sources/MultiFactor/FIRMultiFactorResolver+Internal.h"
  27. #import "FirebaseAuth/Sources/MultiFactor/FIRMultiFactorSession+Internal.h"
  28. #if TARGET_OS_IOS
  29. #import "FirebaseAuth/Sources/Public/FirebaseAuth/FIRPhoneMultiFactorAssertion.h"
  30. #import "FirebaseAuth/Sources/Public/FirebaseAuth/FIRTOTPMultiFactorAssertion.h"
  31. #import "FirebaseAuth/Sources/AuthProvider/Phone/FIRPhoneAuthCredential_Internal.h"
  32. #import "FirebaseAuth/Sources/MultiFactor/Phone/FIRPhoneMultiFactorAssertion+Internal.h"
  33. #import "FirebaseAuth/Sources/MultiFactor/TOTP/FIRTOTPMultiFactorAssertion+Internal.h"
  34. #endif
  35. NS_ASSUME_NONNULL_BEGIN
  36. @implementation FIRMultiFactorResolver
  37. - (instancetype)initWithMFAPendingCredential:(NSString *_Nullable)MFAPendingCredential
  38. hints:(NSArray<FIRMultiFactorInfo *> *)hints
  39. auth:(FIRAuth *)auth {
  40. self = [super init];
  41. if (self) {
  42. _MFAPendingCredential = MFAPendingCredential;
  43. _hints = hints;
  44. _auth = auth;
  45. _session = [[FIRMultiFactorSession alloc] init];
  46. _session.MFAPendingCredential = MFAPendingCredential;
  47. }
  48. return self;
  49. }
  50. - (void)resolveSignInWithAssertion:(nonnull FIRMultiFactorAssertion *)assertion
  51. completion:(nullable FIRAuthDataResultCallback)completion {
  52. #if TARGET_OS_IOS
  53. NSObject<FIRAuthProto> *finalizedMFARequestInfo;
  54. if ([assertion isKindOfClass:[FIRPhoneMultiFactorAssertion class]]) {
  55. FIRPhoneMultiFactorAssertion *phoneAssertion = (FIRPhoneMultiFactorAssertion *)assertion;
  56. finalizedMFARequestInfo = [[FIRAuthProtoFinalizeMFAPhoneRequestInfo alloc]
  57. initWithSessionInfo:phoneAssertion.authCredential.verificationID
  58. verificationCode:phoneAssertion.authCredential.verificationCode];
  59. } else if ([assertion isKindOfClass:[FIRTOTPMultiFactorAssertion class]]) {
  60. FIRTOTPMultiFactorAssertion *totpAssertion = (FIRTOTPMultiFactorAssertion *)assertion;
  61. finalizedMFARequestInfo = [[FIRAuthProtoFinalizeMFATOTPSignInRequestInfo alloc]
  62. initWithMfaEnrollmentID:totpAssertion.enrollmentID
  63. verificationCode:totpAssertion.oneTimePassword];
  64. }
  65. if (finalizedMFARequestInfo != nil) {
  66. FIRFinalizeMFASignInRequest *request = [[FIRFinalizeMFASignInRequest alloc]
  67. initWithMFAPendingCredential:self.MFAPendingCredential
  68. verificationInfo:finalizedMFARequestInfo
  69. requestConfiguration:self.auth.requestConfiguration];
  70. [FIRAuthBackend
  71. finalizeMultiFactorSignIn:request
  72. callback:^(FIRFinalizeMFASignInResponse *_Nullable response,
  73. NSError *_Nullable error) {
  74. if (error) {
  75. if (completion) {
  76. completion(nil, error);
  77. }
  78. } else {
  79. [self.auth
  80. completeSignInWithAccessToken:response.IDToken
  81. accessTokenExpirationDate:nil
  82. refreshToken:response.refreshToken
  83. anonymous:NO
  84. callback:^(FIRUser *_Nullable user,
  85. NSError *_Nullable error) {
  86. FIRAuthDataResult *result =
  87. [[FIRAuthDataResult alloc]
  88. initWithUser:user
  89. additionalUserInfo:nil];
  90. FIRAuthDataResultCallback
  91. decoratedCallback = [self.auth
  92. signInFlowAuthDataResultCallbackByDecoratingCallback:
  93. completion];
  94. decoratedCallback(result, error);
  95. }];
  96. }
  97. }];
  98. }
  99. #endif
  100. }
  101. @end
  102. NS_ASSUME_NONNULL_END
  103. #endif