FIRMultiFactorResolver.m 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 finalizeMultiFactorSignIn:request
  60. callback:^(FIRFinalizeMFASignInResponse * _Nullable response,
  61. NSError * _Nullable error) {
  62. if (error) {
  63. if (completion) {
  64. completion(nil, error);
  65. }
  66. } else {
  67. [FIRAuth.auth completeSignInWithAccessToken:response.IDToken
  68. accessTokenExpirationDate:nil
  69. refreshToken:response.refreshToken
  70. anonymous:NO
  71. callback:^(FIRUser *_Nullable user, NSError *_Nullable error) {
  72. FIRAuthDataResult* result =
  73. [[FIRAuthDataResult alloc] initWithUser:user additionalUserInfo:nil];
  74. FIRAuthDataResultCallback decoratedCallback =
  75. [FIRAuth.auth signInFlowAuthDataResultCallbackByDecoratingCallback:completion];
  76. decoratedCallback(result, error);
  77. }];
  78. }
  79. }];
  80. #endif
  81. }
  82. @end
  83. NS_ASSUME_NONNULL_END
  84. #endif