FIRMultiFactorResolver.m 4.4 KB

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