FIRTOTPSecret.m 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 <UIKit/UIKit.h>
  19. #import "FirebaseAuth/Sources/Auth/FIRAuth_Internal.h"
  20. #import "FirebaseAuth/Sources/MultiFactor/TOTP/FIRTOTPSecret+Internal.h"
  21. #import "FirebaseAuth/Sources/Public/FirebaseAuth/FIRTOTPSecret.h"
  22. #import "FirebaseCore/Extension/FirebaseCoreInternal.h"
  23. NS_ASSUME_NONNULL_BEGIN
  24. @implementation FIRTOTPSecret
  25. - (instancetype)initWithSecretKey:(NSString *)secretKey
  26. hashingAlgorithm:(NSString *)hashingAlgorithm
  27. codeLength:(NSInteger)codeLength
  28. codeIntervalSeconds:(NSInteger)codeIntervalSeconds
  29. enrollmentCompletionDeadline:(NSDate *)enrollmentCompletionDeadline
  30. sessionInfo:(NSString *)sessionInfo {
  31. self = [super init];
  32. if (self) {
  33. _secretKey = secretKey;
  34. _hashingAlgorithm = hashingAlgorithm;
  35. _codeLength = codeLength;
  36. _codeIntervalSeconds = codeIntervalSeconds;
  37. _enrollmentCompletionDeadline = [enrollmentCompletionDeadline copy];
  38. _sessionInfo = sessionInfo;
  39. }
  40. return self;
  41. }
  42. - (NSString *)sharedSecretKey {
  43. return _secretKey;
  44. }
  45. - (NSString *)generateQRCodeURLWithAccountName:(NSString *)accountName issuer:(NSString *)issuer {
  46. if (!accountName || !self.secretKey || !self.hashingAlgorithm || !self.codeLength) {
  47. return @"";
  48. }
  49. NSString *urlString = [NSString
  50. stringWithFormat:@"otpauth://totp/%@:%@?secret=%@&issuer=%@&algorithm=%@&digits=%ld", issuer,
  51. accountName, self.secretKey, issuer, self.hashingAlgorithm, self.codeLength];
  52. return urlString;
  53. }
  54. - (void)openInOTPAppWithQRCodeURL:(NSString *)QRCodeURL {
  55. NSURL *url = [NSURL URLWithString:QRCodeURL];
  56. if ([[UIApplication sharedApplication] canOpenURL:url]) {
  57. [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
  58. } else {
  59. FIRLogError(kFIRLoggerAuth, @"I-AUT000019", @"URL cannot be opened");
  60. }
  61. }
  62. @end
  63. NS_ASSUME_NONNULL_END
  64. #endif