NCKeychain.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. @objc var showHiddenFiles: Bool {
  106. get {
  107. migrate(key: "showHiddenFiles")
  108. if let value = try? keychain.get("showHiddenFiles"), let result = Bool(value) {
  109. return result
  110. }
  111. return false
  112. }
  113. set {
  114. keychain["showHiddenFiles"] = String(newValue)
  115. }
  116. }
  117. // MARK: -
  118. private func migrate(key: String) {
  119. let keychainOLD = Keychain(service: NCGlobal.shared.serviceShareKeyChain)
  120. if let value = keychainOLD[key], !value.isEmpty {
  121. keychain[key] = value
  122. keychainOLD[key] = nil
  123. }
  124. }
  125. @objc func removeAll() {
  126. try? keychain.removeAll()
  127. }
  128. @objc func getOriginalFileName(key: String) -> Bool {
  129. migrate(key: key)
  130. if let value = try? keychain.get(key), let result = Bool(value) {
  131. return result
  132. }
  133. return false
  134. }
  135. @objc func setOriginalFileName(key: String, value: Bool) {
  136. keychain[key] = String(value)
  137. }
  138. @objc func getFileNameMask(key: String) -> String {
  139. migrate(key: key)
  140. if let value = try? keychain.get(key) {
  141. return value
  142. } else {
  143. return ""
  144. }
  145. }
  146. @objc func setFileNameMask(key: String, mask: String?) {
  147. keychain[key] = mask
  148. }
  149. @objc func getFileNameType(key: String) -> Bool {
  150. migrate(key: key)
  151. if let value = try? keychain.get(key), let result = Bool(value) {
  152. return result
  153. } else {
  154. return false
  155. }
  156. }
  157. @objc func setFileNameType(key: String, prefix: Bool) {
  158. keychain[key] = String(prefix)
  159. }
  160. }