NCKeychain.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. var typeFilterScanDocument: NCGlobal.TypeFilterScanDocument {
  28. get {
  29. if let rawValue = try? keychain.get("ScanDocumentTypeFilter"), let value = NCGlobal.TypeFilterScanDocument(rawValue: rawValue) {
  30. return value
  31. } else {
  32. return .original
  33. }
  34. }
  35. set {
  36. keychain["ScanDocumentTypeFilter"] = newValue.rawValue
  37. }
  38. }
  39. @objc var passcode: String? {
  40. get {
  41. migrate(key: "passcodeBlock")
  42. if let value = try? keychain.get("passcodeBlock") {
  43. return value
  44. }
  45. return nil
  46. }
  47. set {
  48. keychain["passcodeBlock"] = newValue
  49. }
  50. }
  51. @objc var requestPasscodeAtStart: Bool {
  52. get {
  53. let keychainOLD = Keychain(service: NCGlobal.shared.serviceShareKeyChain)
  54. if let value = keychainOLD["notPasscodeAtStart"], !value.isEmpty {
  55. if value == "true" {
  56. keychain["requestPasscodeAtStart"] = "false"
  57. } else if value == "false" {
  58. keychain["requestPasscodeAtStart"] = "true"
  59. }
  60. keychainOLD["notPasscodeAtStart"] = nil
  61. }
  62. if let value = try? keychain.get("requestPasscodeAtStart"), let result = Bool(value) {
  63. return result
  64. }
  65. return false
  66. }
  67. set {
  68. keychain["requestPasscodeAtStart"] = String(newValue)
  69. }
  70. }
  71. @objc var touchFaceID: Bool {
  72. get {
  73. migrate(key: "enableTouchFaceID")
  74. if let value = try? keychain.get("enableTouchFaceID"), let result = Bool(value) {
  75. return result
  76. }
  77. return false
  78. }
  79. set {
  80. keychain["enableTouchFaceID"] = String(newValue)
  81. }
  82. }
  83. var intro: Bool {
  84. get {
  85. migrate(key: "intro")
  86. if let value = try? keychain.get("intro"), let result = Bool(value) {
  87. return result
  88. }
  89. return false
  90. }
  91. set {
  92. keychain["intro"] = String(newValue)
  93. }
  94. }
  95. @objc var incrementalNumber: String {
  96. migrate(key: "incrementalnumber")
  97. var incrementalString = String(format: "%04ld", 0)
  98. if let value = try? keychain.get("incrementalnumber"), var result = Int(value) {
  99. result += 1
  100. incrementalString = String(format: "%04ld", result)
  101. }
  102. keychain["incrementalnumber"] = incrementalString
  103. return incrementalString
  104. }
  105. // MARK: -
  106. private func migrate(key: String) {
  107. let keychainOLD = Keychain(service: NCGlobal.shared.serviceShareKeyChain)
  108. if let value = keychainOLD[key], !value.isEmpty {
  109. keychain[key] = value
  110. keychainOLD[key] = nil
  111. }
  112. }
  113. @objc func removeAll() {
  114. try? keychain.removeAll()
  115. }
  116. @objc func getOriginalFileName(key: String) -> Bool {
  117. migrate(key: key)
  118. if let value = try? keychain.get(key), let result = Bool(value) {
  119. return result
  120. }
  121. return false
  122. }
  123. @objc func setOriginalFileName(key: String, value: Bool) {
  124. keychain[key] = String(value)
  125. }
  126. }