NCKeychain.swift 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. // swiftlint:disable identifier_name
  28. let E2E_certificate = "EndToEndCertificate_"
  29. let E2E_PrivateKey = "EndToEndPrivateKey_"
  30. let E2E_Passphrase = "EndToEndPassphrase_"
  31. let E2E_PublicKey = "EndToEndPublicKeyServer_"
  32. // swiftlint:enable identifier_name
  33. var typeFilterScanDocument: NCGlobal.TypeFilterScanDocument {
  34. get {
  35. if let rawValue = try? keychain.get("ScanDocumentTypeFilter"), let value = NCGlobal.TypeFilterScanDocument(rawValue: rawValue) {
  36. return value
  37. } else {
  38. return .original
  39. }
  40. }
  41. set {
  42. keychain["ScanDocumentTypeFilter"] = newValue.rawValue
  43. }
  44. }
  45. @objc var passcode: String? {
  46. get {
  47. migrate(key: "passcodeBlock")
  48. if let value = try? keychain.get("passcodeBlock") {
  49. return value
  50. }
  51. return nil
  52. }
  53. set {
  54. keychain["passcodeBlock"] = newValue
  55. }
  56. }
  57. @objc var requestPasscodeAtStart: Bool {
  58. get {
  59. let keychainOLD = Keychain(service: NCGlobal.shared.serviceShareKeyChain)
  60. if let value = keychainOLD["notPasscodeAtStart"], !value.isEmpty {
  61. if value == "true" {
  62. keychain["requestPasscodeAtStart"] = "false"
  63. } else if value == "false" {
  64. keychain["requestPasscodeAtStart"] = "true"
  65. }
  66. keychainOLD["notPasscodeAtStart"] = nil
  67. }
  68. if let value = try? keychain.get("requestPasscodeAtStart"), let result = Bool(value) {
  69. return result
  70. }
  71. return false
  72. }
  73. set {
  74. keychain["requestPasscodeAtStart"] = String(newValue)
  75. }
  76. }
  77. @objc var touchFaceID: Bool {
  78. get {
  79. migrate(key: "enableTouchFaceID")
  80. if let value = try? keychain.get("enableTouchFaceID"), let result = Bool(value) {
  81. return result
  82. }
  83. return false
  84. }
  85. set {
  86. keychain["enableTouchFaceID"] = String(newValue)
  87. }
  88. }
  89. var intro: Bool {
  90. get {
  91. migrate(key: "intro")
  92. if let value = try? keychain.get("intro"), let result = Bool(value) {
  93. return result
  94. }
  95. return false
  96. }
  97. set {
  98. keychain["intro"] = String(newValue)
  99. }
  100. }
  101. @objc var incrementalNumber: String {
  102. migrate(key: "incrementalnumber")
  103. var incrementalString = String(format: "%04ld", 0)
  104. if let value = try? keychain.get("incrementalnumber"), var result = Int(value) {
  105. result += 1
  106. incrementalString = String(format: "%04ld", result)
  107. }
  108. keychain["incrementalnumber"] = incrementalString
  109. return incrementalString
  110. }
  111. @objc var showHiddenFiles: Bool {
  112. get {
  113. migrate(key: "showHiddenFiles")
  114. if let value = try? keychain.get("showHiddenFiles"), let result = Bool(value) {
  115. return result
  116. }
  117. return false
  118. }
  119. set {
  120. keychain["showHiddenFiles"] = String(newValue)
  121. }
  122. }
  123. @objc var formatCompatibility: Bool {
  124. get {
  125. migrate(key: "formatCompatibility")
  126. if let value = try? keychain.get("formatCompatibility"), let result = Bool(value) {
  127. return result
  128. }
  129. return true
  130. }
  131. set {
  132. keychain["formatCompatibility"] = String(newValue)
  133. }
  134. }
  135. // MARK: -
  136. private func migrate(key: String) {
  137. let keychainOLD = Keychain(service: NCGlobal.shared.serviceShareKeyChain)
  138. if let value = keychainOLD[key], !value.isEmpty {
  139. keychain[key] = value
  140. keychainOLD[key] = nil
  141. }
  142. }
  143. @objc func removeAll() {
  144. try? keychain.removeAll()
  145. }
  146. @objc func getOriginalFileName(key: String) -> Bool {
  147. migrate(key: key)
  148. if let value = try? keychain.get(key), let result = Bool(value) {
  149. return result
  150. }
  151. return false
  152. }
  153. @objc func setOriginalFileName(key: String, value: Bool) {
  154. keychain[key] = String(value)
  155. }
  156. @objc func getFileNameMask(key: String) -> String {
  157. migrate(key: key)
  158. if let value = try? keychain.get(key) {
  159. return value
  160. } else {
  161. return ""
  162. }
  163. }
  164. @objc func setFileNameMask(key: String, mask: String?) {
  165. keychain[key] = mask
  166. }
  167. @objc func getFileNameType(key: String) -> Bool {
  168. migrate(key: key)
  169. if let value = try? keychain.get(key), let result = Bool(value) {
  170. return result
  171. } else {
  172. return false
  173. }
  174. }
  175. @objc func setFileNameType(key: String, prefix: Bool) {
  176. keychain[key] = String(prefix)
  177. }
  178. func getEndToEndCertificate(account: String) -> String? {
  179. let key = E2E_certificate + account
  180. migrate(key: key)
  181. let certificate = try? keychain.get(key)
  182. return certificate
  183. }
  184. func setEndToEndCertificate(account: String, certificate: String) {
  185. let key = E2E_certificate + account
  186. keychain[key] = certificate
  187. }
  188. }