FIRTOTPMultiFactorGenerator.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright 2023 Google LLC
  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/Auth/FIRAuth_Internal.h"
  19. #import "FirebaseAuth/Sources/Backend/FIRAuthBackend+MultiFactor.h"
  20. #import "FirebaseAuth/Sources/Backend/FIRAuthBackend.h"
  21. #import "FirebaseAuth/Sources/Backend/RPC/MultiFactor/Enroll/FIRStartMFAEnrollmentRequest.h"
  22. #import "FirebaseAuth/Sources/Backend/RPC/MultiFactor/Enroll/FIRStartMFAEnrollmentResponse.h"
  23. #import "FirebaseAuth/Sources/Backend/RPC/Proto/TOTP/FIRAuthProtoStartMFATOTPEnrollmentResponseInfo.h"
  24. #import "FirebaseAuth/Sources/MultiFactor/FIRMultiFactorSession+Internal.h"
  25. #import "FirebaseAuth/Sources/MultiFactor/TOTP/FIRTOTPMultiFactorAssertion+Internal.h"
  26. #import "FirebaseAuth/Sources/MultiFactor/TOTP/FIRTOTPSecret+Internal.h"
  27. #import "FirebaseAuth/Sources/Public/FirebaseAuth/FIRTOTPMultiFactorAssertion.h"
  28. #import "FirebaseAuth/Sources/Public/FirebaseAuth/FIRTOTPMultiFactorGenerator.h"
  29. #import "FirebaseAuth/Sources/Public/FirebaseAuth/FIRTOTPSecret.h"
  30. @implementation FIRTOTPMultiFactorGenerator
  31. + (void)generateSecretWithMultiFactorSession:(FIRMultiFactorSession *)session
  32. completion:(void (^)(FIRTOTPSecret *_Nullable secret,
  33. NSError *_Nullable error))completion {
  34. if (session.IDToken) {
  35. FIRStartMFAEnrollmentRequest *request = [[FIRStartMFAEnrollmentRequest alloc]
  36. initWithIDToken:session.IDToken
  37. TOTPEnrollmentInfo:[[FIRAuthProtoStartMFATOTPEnrollmentRequestInfo alloc] init]
  38. requestConfiguration:session.currentUser.auth.requestConfiguration];
  39. [FIRAuthBackend
  40. startMultiFactorEnrollment:request
  41. callback:^(FIRStartMFAEnrollmentResponse *_Nullable response,
  42. NSError *_Nullable error) {
  43. if (error) {
  44. if (completion) {
  45. completion(nil, error);
  46. }
  47. } else if (response.TOTPSessionInfo) {
  48. FIRTOTPSecret *secret = [[FIRTOTPSecret alloc]
  49. initWithSecretKey:response.TOTPSessionInfo
  50. .sharedSecretKey
  51. hashingAlgorithm:response.TOTPSessionInfo
  52. .hashingAlgorithm
  53. codeLength:response.TOTPSessionInfo
  54. .verificationCodeLength
  55. codeIntervalSeconds:response.TOTPSessionInfo.periodSec
  56. enrollmentCompletionDeadline:response.TOTPSessionInfo
  57. .finalizeEnrollmentTime
  58. sessionInfo:response.TOTPSessionInfo
  59. .sessionInfo];
  60. if (completion) {
  61. completion(secret, nil);
  62. }
  63. } else {
  64. NSError *error =
  65. [NSError errorWithDomain:FIRAuthErrorDomain
  66. code:FIRAuthErrorCodeInternalError
  67. userInfo:@{
  68. NSLocalizedDescriptionKey :
  69. @"Error generating TOTP secret."
  70. }];
  71. if (completion) {
  72. completion(nil, error);
  73. }
  74. }
  75. }];
  76. } else {
  77. NSError *error = [NSError errorWithDomain:FIRAuthErrorDomain
  78. code:FIRAuthErrorCodeInternalError
  79. userInfo:@{NSLocalizedDescriptionKey : @"Invalid ID token."}];
  80. if (completion) {
  81. completion(nil, error);
  82. }
  83. }
  84. }
  85. + (FIRTOTPMultiFactorAssertion *)assertionForEnrollmentWithSecret:(FIRTOTPSecret *)secret
  86. oneTimePassword:(NSString *)oneTimePassword {
  87. return [[FIRTOTPMultiFactorAssertion alloc] initWithSecret:secret
  88. oneTimePassword:oneTimePassword];
  89. }
  90. + (FIRTOTPMultiFactorAssertion *)assertionForSignInWithEnrollmentID:(NSString *)enrollmentID
  91. oneTimePassword:(NSString *)oneTimePassword {
  92. return [[FIRTOTPMultiFactorAssertion alloc] initWithEnrollmentID:enrollmentID
  93. oneTimePassword:oneTimePassword];
  94. }
  95. @end
  96. #endif