FIRTOTPSecret.m 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. NSLog(@"Cannot access openURL:options:completionHandler: method");
  76. }
  77. } else {
  78. NSLog(@"URL cannot be opened");
  79. }
  80. } else {
  81. NSLog(@"sharedApplication cannot be accessed");
  82. }
  83. }
  84. @end
  85. NS_ASSUME_NONNULL_END
  86. #endif