FIRMultiFactorResolver.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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/MultiFactor/FIRMultiFactorResolver+Internal.h"
  26. #import "FirebaseAuth/Sources/MultiFactor/FIRMultiFactorSession+Internal.h"
  27. #if TARGET_OS_IOS
  28. #import "FirebaseAuth/Sources/Public/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. @implementation FIRMultiFactorResolver
  34. - (instancetype)initWithMFAPendingCredential:(NSString *_Nullable)MFAPendingCredential
  35. hints:(NSArray<FIRMultiFactorInfo *> *)hints
  36. auth:(FIRAuth *)auth {
  37. self = [super init];
  38. if (self) {
  39. _MFAPendingCredential = MFAPendingCredential;
  40. _hints = hints;
  41. _auth = auth;
  42. _session = [[FIRMultiFactorSession alloc] init];
  43. _session.MFAPendingCredential = MFAPendingCredential;
  44. }
  45. return self;
  46. }
  47. - (void)resolveSignInWithAssertion:(nonnull FIRMultiFactorAssertion *)assertion
  48. completion:(nullable FIRAuthDataResultCallback)completion {
  49. #if TARGET_OS_IOS
  50. FIRPhoneMultiFactorAssertion *phoneAssertion = (FIRPhoneMultiFactorAssertion *)assertion;
  51. FIRAuthProtoFinalizeMFAPhoneRequestInfo *finalizeMFAPhoneRequestInfo =
  52. [[FIRAuthProtoFinalizeMFAPhoneRequestInfo alloc]
  53. initWithSessionInfo:phoneAssertion.authCredential.verificationID
  54. verificationCode:phoneAssertion.authCredential.verificationCode];
  55. FIRFinalizeMFASignInRequest *request = [[FIRFinalizeMFASignInRequest alloc]
  56. initWithMFAPendingCredential: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