FIRMultiFactorResolver.m 3.9 KB

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