NCKeychain.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //
  2. // NCKeychain.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 23/10/23.
  6. // Copyright © 2023 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import Foundation
  24. import KeychainAccess
  25. @objc class NCKeychain: NSObject {
  26. let keychain = Keychain(service: "com.nextcloud.keychain")
  27. private func migrate(key: String) {
  28. let keychainOLD = Keychain(service: NCGlobal.shared.serviceShareKeyChain)
  29. if let value = keychainOLD[key], !value.isEmpty {
  30. keychain[key] = value
  31. keychainOLD[key] = nil
  32. }
  33. }
  34. @objc func removeAll() {
  35. try? keychain.removeAll()
  36. }
  37. var typeFilterScanDocument: NCGlobal.TypeFilterScanDocument {
  38. get {
  39. if let rawValue = try? keychain.get("ScanDocumentTypeFilter"), let value = NCGlobal.TypeFilterScanDocument(rawValue: rawValue) {
  40. return value
  41. } else {
  42. return .original
  43. }
  44. }
  45. set {
  46. keychain["ScanDocumentTypeFilter"] = newValue.rawValue
  47. }
  48. }
  49. @objc var passcode: String? {
  50. get {
  51. migrate(key: "passcodeBlock")
  52. if let value = try? keychain.get("passcodeBlock") {
  53. return value
  54. }
  55. return nil
  56. }
  57. set {
  58. keychain["passcodeBlock"] = newValue
  59. }
  60. }
  61. @objc var requestPasscodeAtStart: Bool {
  62. get {
  63. let keychainOLD = Keychain(service: NCGlobal.shared.serviceShareKeyChain)
  64. if let value = keychainOLD["notPasscodeAtStart"], !value.isEmpty {
  65. if value == "true" {
  66. keychain["requestPasscodeAtStart"] = "false"
  67. } else if value == "false" {
  68. keychain["requestPasscodeAtStart"] = "true"
  69. }
  70. keychainOLD["notPasscodeAtStart"] = nil
  71. }
  72. if let value = try? keychain.get("requestPasscodeAtStart"), let result = Bool(value) {
  73. return result
  74. }
  75. return false
  76. }
  77. set {
  78. keychain["requestPasscodeAtStart"] = String(newValue)
  79. }
  80. }
  81. @objc var touchFaceID: Bool {
  82. get {
  83. migrate(key: "enableTouchFaceID")
  84. if let value = try? keychain.get("enableTouchFaceID"), let result = Bool(value) {
  85. return result
  86. }
  87. return false
  88. }
  89. set {
  90. keychain["enableTouchFaceID"] = String(newValue)
  91. }
  92. }
  93. var intro: Bool {
  94. get {
  95. migrate(key: "intro")
  96. if let value = try? keychain.get("intro"), let result = Bool(value) {
  97. return result
  98. }
  99. return false
  100. }
  101. set {
  102. keychain["intro"] = String(newValue)
  103. }
  104. }
  105. @objc var incrementalNumber: String {
  106. migrate(key: "incrementalnumber")
  107. var incrementalString = String(format: "%04ld", 0)
  108. if let value = try? keychain.get("incrementalnumber"), var result = Int(value) {
  109. result += 1
  110. incrementalString = String(format: "%04ld", result)
  111. }
  112. keychain["incrementalnumber"] = incrementalString
  113. return incrementalString
  114. }
  115. }