FIRMultiFactorResolver.m 4.8 KB

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