KDBX.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 CompositeKey: KDBX.CompositeKey { }
  24. extension KDBX.File: Database {
  25. public var root: Element { database.document.root.KeePassFile.Root }
  26. }
  27. extension Element {
  28. private var this: XML.Element { self }
  29. }
  30. extension XML.Element {
  31. convenience init(_ field: Field) {
  32. self.init(name: field.name, value: field.value, attributes: [:])
  33. }
  34. }
  35. extension Field {
  36. init?(_ element: XML.Element) {
  37. guard let key = element.Key.value else { return nil }
  38. name = key
  39. value = element.Value.value
  40. isProtected = element.Value.attributes["Protected"] == "True"
  41. }
  42. }
  43. extension XML.Element: Entry {
  44. public var times: Timestamp { self.Times }
  45. public var fields: [Field] {
  46. allDescendants(where: { $0.name == "String" }).compactMap { Field($0) }
  47. }
  48. public func set(_ field: Field) {
  49. allDescendants(where: { $0.name == field.name })
  50. .forEach { $0.removeFromParent() }
  51. addChild( XML.Element(field) )
  52. }
  53. }
  54. extension XML.Element: Timestamp {
  55. public var creationDate: Date {
  56. self.CreationTime.date(formatter: DateFormatter) ?? .distantPast
  57. }
  58. public var lastModifiedDate: Date {
  59. get { self.LastModificationTime.date(formatter: DateFormatter) ?? .distantPast }
  60. set { self.LastModificationTime.value = DateFormatter.string(from: newValue) }
  61. }
  62. public var lastAccessDate: Date {
  63. get { self.LastAccessTime.date(formatter: DateFormatter) ?? .distantPast }
  64. set { self.LastAccessTime.value = DateFormatter.string(from: newValue) }
  65. }
  66. public var expirationDate: Date? {
  67. get { self.ExpiryTime.date(formatter: DateFormatter) }
  68. set {
  69. if let value = newValue {
  70. self.ExpiryTime.value = DateFormatter.string(from: value)
  71. addChild(name: "Expires", value: "True")
  72. } else {
  73. self.ExpiryTime.value = DateFormatter.string(from: Date.distantFuture)
  74. addChild(name: "Expires", value: "False")
  75. }
  76. }
  77. }
  78. }
  79. extension XML.Element: Group {
  80. public var title: String {
  81. get { attributes["Title"] ?? "" }
  82. set { attributes["Title"] = newValue }
  83. }
  84. public var icon: Int {
  85. get {
  86. guard let attr = attributes["Icon"], let icon = Int(attr) else { return 0 }
  87. return icon
  88. }
  89. set { attributes["Icon"] = "\(newValue)" }
  90. }
  91. public var entries: [Element] {
  92. allDescendants(where: { $0.name == "Entry" })
  93. }
  94. public var groups: [Element] {
  95. allDescendants(where: { $0.name == "Group" })
  96. }
  97. }