NCKeychain.swift 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. var typeFilterScanDocument: NCGlobal.TypeFilterScanDocument {
  35. get {
  36. if let rawValue = try? keychain.get("ScanDocumentTypeFilter"), let value = NCGlobal.TypeFilterScanDocument(rawValue: rawValue) {
  37. return value
  38. } else {
  39. return .original
  40. }
  41. }
  42. set {
  43. keychain["ScanDocumentTypeFilter"] = newValue.rawValue
  44. }
  45. }
  46. @objc var passcode: String? {
  47. get {
  48. migrate(key: "passcodeBlock")
  49. if let value = try? keychain.get("passcodeBlock") {
  50. return value
  51. }
  52. return nil
  53. }
  54. set {
  55. keychain["passcodeBlock"] = newValue
  56. }
  57. }
  58. @objc var requestPasscodeAtStart: Bool {
  59. get {
  60. let keychainOLD = Keychain(service: NCGlobal.shared.serviceShareKeyChain)
  61. if let value = keychainOLD["notPasscodeAtStart"], !value.isEmpty {
  62. if value == "true" {
  63. keychain["requestPasscodeAtStart"] = "false"
  64. } else if value == "false" {
  65. keychain["requestPasscodeAtStart"] = "true"
  66. }
  67. keychainOLD["notPasscodeAtStart"] = nil
  68. }
  69. if let value = try? keychain.get("requestPasscodeAtStart"), let result = Bool(value) {
  70. return result
  71. }
  72. return false
  73. }
  74. set {
  75. keychain["requestPasscodeAtStart"] = String(newValue)
  76. }
  77. }
  78. }