FIRTOTPSecret.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 <GoogleUtilities/GULAppEnvironmentUtil.h>
  19. #import <UIKit/UIKit.h>
  20. #import "FirebaseAuth/Sources/Auth/FIRAuth_Internal.h"
  21. #import "FirebaseAuth/Sources/MultiFactor/TOTP/FIRTOTPSecret+Internal.h"
  22. #import "FirebaseAuth/Sources/Public/FirebaseAuth/FIRTOTPSecret.h"
  23. #import "FirebaseCore/Extension/FirebaseCoreInternal.h"
  24. NS_ASSUME_NONNULL_BEGIN
  25. @implementation FIRTOTPSecret
  26. - (instancetype)initWithSecretKey:(NSString *)secretKey
  27. hashingAlgorithm:(NSString *)hashingAlgorithm
  28. codeLength:(NSInteger)codeLength
  29. codeIntervalSeconds:(NSInteger)codeIntervalSeconds
  30. enrollmentCompletionDeadline:(NSDate *)enrollmentCompletionDeadline
  31. sessionInfo:(NSString *)sessionInfo {
  32. self = [super init];
  33. if (self) {
  34. _secretKey = secretKey;
  35. _hashingAlgorithm = hashingAlgorithm;
  36. _codeLength = codeLength;
  37. _codeIntervalSeconds = codeIntervalSeconds;
  38. _enrollmentCompletionDeadline = [enrollmentCompletionDeadline copy];
  39. _sessionInfo = sessionInfo;
  40. }
  41. return self;
  42. }
  43. - (NSString *)sharedSecretKey {
  44. return _secretKey;
  45. }
  46. - (NSString *)generateQRCodeURLWithAccountName:(NSString *)accountName issuer:(NSString *)issuer {
  47. if (!accountName || !self.secretKey || !self.hashingAlgorithm || !self.codeLength) {
  48. return @"";
  49. }
  50. NSString *urlString = [NSString
  51. stringWithFormat:@"otpauth://totp/%@:%@?secret=%@&issuer=%@&algorithm=%@&digits=%ld", issuer,
  52. accountName, self.secretKey, issuer, self.hashingAlgorithm, self.codeLength];
  53. return urlString;
  54. }
  55. - (void)openInOTPAppWithQRCodeURL:(NSString *)QRCodeURL {
  56. NSURL *url = [NSURL URLWithString:QRCodeURL];
  57. static Class applicationClass = nil;
  58. // iOS App extensions should not call [UIApplication sharedApplication], even if UIApplication
  59. // responds to it.
  60. if (![GULAppEnvironmentUtil isAppExtension]) {
  61. Class cls = NSClassFromString(@"UIApplication");
  62. if (cls && [cls respondsToSelector:@selector(sharedApplication)]) {
  63. applicationClass = cls;
  64. }
  65. }
  66. UIApplication *application = [applicationClass sharedApplication];
  67. if (application) {
  68. if ([application canOpenURL:url]) {
  69. SEL selector = @selector(openURL:options:completionHandler:);
  70. if ([application respondsToSelector:selector]) {
  71. IMP imp = [application methodForSelector:selector];
  72. void (*func)(id, SEL, NSURL *, NSDictionary *, void (^)(BOOL)) = (void *)imp;
  73. func(application, selector, url, @{}, nil);
  74. } else {
  75. FIRLogError(kFIRLoggerAuth, @"I-AUT000023",
  76. @"Cannot access openURL:options:completionHandler: method");
  77. }
  78. } else {
  79. FIRLogError(kFIRLoggerAuth, @"I-AUT000024", @"URL cannot be opened");
  80. }
  81. } else {
  82. FIRLogError(kFIRLoggerAuth, @"I-AUT000025", @"sharedApplication cannot be accessed");
  83. }
  84. }
  85. @end
  86. NS_ASSUME_NONNULL_END
  87. #endif