TOTPSecret.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2023 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import Foundation
  15. #if COCOAPODS
  16. internal import GoogleUtilities
  17. #else
  18. internal import GoogleUtilities_Environment
  19. #endif
  20. #if os(iOS)
  21. import UIKit
  22. /// The subclass of base class MultiFactorAssertion, used to assert ownership of a TOTP
  23. /// (Time-based One Time Password) second factor.
  24. ///
  25. /// This class is available on iOS only.
  26. @objc(FIRTOTPSecret) open class TOTPSecret: NSObject {
  27. /// Returns the shared secret key/seed used to generate time-based one-time passwords.
  28. @objc open func sharedSecretKey() -> String {
  29. return secretKey
  30. }
  31. /// Returns a QRCode URL as described in
  32. /// https://github.com/google/google-authenticator/wiki/Key-Uri-Format.
  33. ///
  34. /// This can be displayed to the user as a QRCode to be scanned into a TOTP app like Google
  35. /// Authenticator.
  36. /// - Parameter accountName: The name of the account/app.
  37. /// - Parameter issuer: Issuer of the TOTP(likely the app name).
  38. /// - Returns: A QRCode URL string.
  39. @objc(generateQRCodeURLWithAccountName:issuer:)
  40. open func generateQRCodeURL(withAccountName accountName: String,
  41. issuer: String) -> String {
  42. guard let hashingAlgorithm, codeLength > 0 else {
  43. return ""
  44. }
  45. return "otpauth://totp/\(issuer):\(accountName)?secret=\(secretKey)&issuer=\(issuer)" +
  46. "&algorithm=%\(hashingAlgorithm)&digits=\(codeLength)"
  47. }
  48. /// Opens the specified QR Code URL in a password manager like iCloud Keychain.
  49. ///
  50. /// See more details
  51. /// [here](https://developer.apple.com/documentation/authenticationservices/securing_logins_with_icloud_keychain_verification_codes)
  52. @objc(openInOTPAppWithQRCodeURL:)
  53. open func openInOTPApp(withQRCodeURL qrCodeURL: String) {
  54. if GULAppEnvironmentUtil.isAppExtension() {
  55. // iOS App extensions should not call [UIApplication sharedApplication], even if
  56. // UIApplication responds to it.
  57. return
  58. }
  59. // Using reflection here to avoid build errors in extensions.
  60. let sel = NSSelectorFromString("sharedApplication")
  61. guard UIApplication.responds(to: sel),
  62. let rawApplication = UIApplication.perform(sel),
  63. let application = rawApplication.takeUnretainedValue() as? UIApplication else {
  64. return
  65. }
  66. if let url = URL(string: qrCodeURL), application.canOpenURL(url) {
  67. application.open(url, options: [:], completionHandler: nil)
  68. } else {
  69. AuthLog.logError(code: "I-AUT000019",
  70. message: "URL: \(qrCodeURL) cannot be opened")
  71. }
  72. }
  73. /// Shared secret key/seed used for enrolling in TOTP MFA and generating OTPs.
  74. private let secretKey: String
  75. /// Hashing algorithm used.
  76. private let hashingAlgorithm: String?
  77. /// Length of the one-time passwords to be generated.
  78. private let codeLength: Int
  79. /// The interval (in seconds) when the OTP codes should change.
  80. private let codeIntervalSeconds: Int
  81. /// The timestamp by which TOTP enrollment should be completed. This can be used by callers to
  82. /// show a countdown of when to enter OTP code by.
  83. private let enrollmentCompletionDeadline: Date?
  84. /// Additional session information.
  85. let sessionInfo: String?
  86. init(secretKey: String, hashingAlgorithm: String?, codeLength: Int, codeIntervalSeconds: Int,
  87. enrollmentCompletionDeadline: Date?, sessionInfo: String?) {
  88. self.secretKey = secretKey
  89. self.hashingAlgorithm = hashingAlgorithm
  90. self.codeLength = codeLength
  91. self.codeIntervalSeconds = codeIntervalSeconds
  92. self.enrollmentCompletionDeadline = enrollmentCompletionDeadline
  93. self.sessionInfo = sessionInfo
  94. }
  95. }
  96. #endif