KDBX.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // KDBX.swift
  2. // This file is part of KeePass.
  3. //
  4. // Copyright © 2019 Maxime Epain. All rights reserved.
  5. //
  6. // KeePass is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // KeePass is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // along with KeePass. If not, see <https://www.gnu.org/licenses/>.
  18. import Foundation
  19. import Binary
  20. import XML
  21. import KDBX
  22. let DateFormatter = ISO8601DateFormatter()
  23. extension KDBX.File: Database {
  24. public var root: Element { database.document.root.KeePassFile.Root }
  25. }
  26. extension Element {
  27. private var this: XML.Element { self as XML.Element }
  28. }
  29. extension XML.Element {
  30. convenience init(_ field: Field) {
  31. self.init(name: field.name, value: field.value, attributes: [:])
  32. }
  33. }
  34. extension Field {
  35. init?(_ element: XML.Element) {
  36. guard let key = element.Key.value else { return nil}
  37. name = key
  38. value = element.Value.value
  39. isProtected = element.Value.attributes["Protected"] == "True"
  40. }
  41. }
  42. extension XML.Element: Entry {
  43. public var times: Timestamp { self.Times }
  44. public var fields: [Field] {
  45. allDescendants(where: { $0.name == "String" }).compactMap { Field($0) }
  46. }
  47. public func set(_ field: Field) {
  48. allDescendants(where: { $0.name == field.name })
  49. .forEach { $0.removeFromParent() }
  50. addChild( XML.Element(field) )
  51. }
  52. }
  53. extension XML.Element: Timestamp {
  54. public var creationDate: Date {
  55. self.CreationTime.date(formatter: DateFormatter) ?? .distantPast
  56. }
  57. public var lastModifiedDate: Date {
  58. get { self.LastModificationTime.date(formatter: DateFormatter) ?? .distantPast }
  59. set { self.LastModificationTime.value = DateFormatter.string(from: newValue) }
  60. }
  61. public var lastAccessDate: Date {
  62. get { self.LastAccessTime.date(formatter: DateFormatter) ?? .distantPast }
  63. set { self.LastAccessTime.value = DateFormatter.string(from: newValue) }
  64. }
  65. public var expirationDate: Date? {
  66. get { self.ExpiryTime.date(formatter: DateFormatter) }
  67. set {
  68. if let value = newValue {
  69. self.ExpiryTime.value = DateFormatter.string(from: value)
  70. addChild(name: "Expires", value: "True")
  71. } else {
  72. self.ExpiryTime.value = DateFormatter.string(from: Date.distantFuture)
  73. addChild(name: "Expires", value: "False")
  74. }
  75. }
  76. }
  77. }
  78. extension XML.Element: Group {
  79. public var title: String {
  80. get { attributes["Title"] ?? "" }
  81. set { attributes["Title"] = newValue }
  82. }
  83. public var icon: Int {
  84. get {
  85. guard let attr = attributes["Icon"], let icon = Int(attr) else { return 0 }
  86. return icon
  87. }
  88. set { attributes["Icon"] = "\(newValue)" }
  89. }
  90. public var entries: [Element] {
  91. allDescendants(where: { $0.name == "Entry" })
  92. }
  93. public var groups: [Element] {
  94. allDescendants(where: { $0.name == "Group" })
  95. }
  96. }