FIRMultiFactorResolver.m 4.6 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 <FirebaseAuth/FIRAdditionalUserInfo.h>
  19. #import <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/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. 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 = [[FIRFinalizeMFASignInRequest alloc]
  55. initWithMFAPendingCredential:self.MFAPendingCredential
  56. verificationInfo:finalizeMFAPhoneRequestInfo
  57. requestConfiguration:self.auth.requestConfiguration];
  58. [FIRAuthBackend
  59. 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
  68. completeSignInWithAccessToken:response.IDToken
  69. accessTokenExpirationDate:nil
  70. refreshToken:response.refreshToken
  71. anonymous:NO
  72. callback:^(FIRUser *_Nullable user,
  73. NSError *_Nullable error) {
  74. FIRAuthDataResult *result =
  75. [[FIRAuthDataResult alloc]
  76. initWithUser:user
  77. additionalUserInfo:nil];
  78. FIRAuthDataResultCallback decoratedCallback =
  79. [FIRAuth.auth
  80. signInFlowAuthDataResultCallbackByDecoratingCallback:
  81. completion];
  82. decoratedCallback(result, error);
  83. }];
  84. }
  85. }];
  86. #endif
  87. }
  88. @end
  89. NS_ASSUME_NONNULL_END
  90. #endif