Class: RubeePass::Entry

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/rubeepass/entry.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(group, keepass, attributes, attachments, uuid) ⇒ Entry

Returns a new instance of Entry.



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/rubeepass/entry.rb', line 201

def initialize(group, keepass, attributes, attachments, uuid)
    @group = group
    @keepass = keepass
    @attributes = attributes
    @attachments = attachments

    @notes = attribute("Notes")
    @password = attribute("Password")
    @title = attribute("Title")
    @url = attribute("URL")
    @username = attribute("UserName")

    @uuid = uuid

    @path = @title
    @path = "#{@group.path}/#{@title}" if (@group)
    @path.gsub!(%r{^//}, "/")
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/rubeepass/entry.rb', line 231

def method_missing(method_name, *args)
    super if (@keepass.nil?)

    if (method_name.to_s.match(/^copy_.+_to_clipboard$/))
        method_name = method_name.to_s.gsub(
            /^copy_(.+)_to_clipboard$/,
            "\\1"
        )
        case method_name
        when "password"
            @keepass.copy_to_clipboard(@password)
        when "url"
            @keepass.copy_to_clipboard(@url)
        when "username"
            @keepass.copy_to_clipboard(@username)
        else
            super
        end
    elsif (method_name.match(/^echo_.+$/))
        case method_name.to_s.gsub(/^echo_/, "")
        when "password"
            puts @password
        when "url"
            puts @url
        when "username"
            puts @username
        else
            super
        end
    else
        super
    end
end

Instance Attribute Details

#groupObject

Returns the value of attribute group.



10
11
12
# File 'lib/rubeepass/entry.rb', line 10

def group
  @group
end

#keepassObject

Returns the value of attribute keepass.



11
12
13
# File 'lib/rubeepass/entry.rb', line 11

def keepass
  @keepass
end

#notesObject

Returns the value of attribute notes.



12
13
14
# File 'lib/rubeepass/entry.rb', line 12

def notes
  @notes
end

#passwordObject

Returns the value of attribute password.



13
14
15
# File 'lib/rubeepass/entry.rb', line 13

def password
  @password
end

#pathObject

Returns the value of attribute path.



14
15
16
# File 'lib/rubeepass/entry.rb', line 14

def path
  @path
end

#titleObject

Returns the value of attribute title.



15
16
17
# File 'lib/rubeepass/entry.rb', line 15

def title
  @title
end

#urlObject

Returns the value of attribute url.



16
17
18
# File 'lib/rubeepass/entry.rb', line 16

def url
  @url
end

#usernameObject

Returns the value of attribute username.



17
18
19
# File 'lib/rubeepass/entry.rb', line 17

def username
  @username
end

#uuidObject

Returns the value of attribute uuid.



18
19
20
# File 'lib/rubeepass/entry.rb', line 18

def uuid
  @uuid
end

Class Method Details

.from_xml(keepass, parent, xml) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/rubeepass/entry.rb', line 112

def self.from_xml(keepass, parent, xml)
    attrs = Hash.new
    attachs = Hash.new

    uuid = xml.elements["UUID"].text || ""

    xml.elements.each("String") do |elem|
        key = elem.elements["Key"].text
        value = elem.elements["Value"]

        attrs[key] = value.text || ""
        if (value.attributes["Protected"] == "True")
            attrs[key] = handle_protected(keepass, value.text)
        end
    end

    # Handle protected data from history
    xml.elements.each("History/Entry/String/Value") do |value|
        if (value.attributes["Protected"] == "True")
            handle_protected(keepass, value.text)
        end
    end

    xml.elements.each("Binary") do |elem|
        key = elem.elements["Key"].text
        value = elem.elements["Value"].attributes["Ref"]
        attachs[key] = value
    end

    return RubeePass::Entry.new(
        parent,
        keepass,
        attrs,
        attachs,
        uuid
    )
end

.handle_protected(keepass, base64) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/rubeepass/entry.rb', line 150

def self.handle_protected(keepass, base64)
    return nil if (base64.nil?)

    data = nil
    begin
        data = base64.unpack("m*")[0]
        if (data.length != data.bytesize)
            data = data.unpack("H*").pack("H*")
        end
    rescue ArgumentError
        raise RubeePass::Error::InvalidProtectedData.new
    end
    if (data.nil?)
        raise RubeePass::Error::InvalidProtectedData.new
    end

    return keepass.protected_decryptor.add_to_stream(data)
end

Instance Method Details

#<=>(other) ⇒ Object



24
25
26
27
28
29
# File 'lib/rubeepass/entry.rb', line 24

def <=>(other)
    if (self.title.downcase == other.title.downcase)
        return (self.uuid <=> other.uuid)
    end
    return (self.title.downcase <=> other.title.downcase)
end

#==(other) ⇒ Object



20
21
22
# File 'lib/rubeepass/entry.rb', line 20

def ==(other)
    return (self.uuid == other.uuid)
end

#additional_attributesObject



31
32
33
34
35
# File 'lib/rubeepass/entry.rb', line 31

def additional_attributes
    return attributes.select do |key, value|
        key.match(/^(Notes|Password|Title|URL|UserName)$/).nil?
    end
end

#attachment(name) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/rubeepass/entry.rb', line 37

def attachment(name)
    return nil if (@keepass.nil?)
    return nil if (!has_attachment?(name))
    return @keepass.attachment_decoder.get_attachment(
        @attachments[name]
    )
end

#attachmentsObject



45
46
47
48
49
50
51
52
53
# File 'lib/rubeepass/entry.rb', line 45

def attachments
    attachments = Hash.new

    @attachments.each do |key, value|
        attachments[key] = attachment(key)
    end

    return attachments
end

#attribute(attr) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rubeepass/entry.rb', line 55

def attribute(attr)
    return nil if (@keepass.nil?)
    return "" if (!has_attribute?(attr))

    begin
        return @keepass.protected_decryptor.decrypt(
            @attributes[attr]
        )
    rescue
        return @attributes[attr]
    end
end

#attributesObject



68
69
70
71
72
73
74
75
76
77
# File 'lib/rubeepass/entry.rb', line 68

def attributes
    return nil if (@keepass.nil?)

    attrs = Hash.new
    @attributes.each do |key, value|
        attrs[key] = attribute(key)
    end

    return attrs
end

#details(level = 0, show_passwd = false) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/rubeepass/entry.rb', line 79

def details(level = 0, show_passwd = false)
    lvl = "  " * level

    r = Array.new
    r.push("#{lvl}#{hilight_attr("Title:")} #{@title}")
    # r.push("#{lvl}#{hilight_attr("UUID:")} #{@uuid}")
    r.push("#{lvl}#{hilight_attr("Username:")} #{@username}")
    if (show_passwd)
        r.push(
            "#{lvl}#{hilight_password("Password:")} #{@password}"
        )
    end
    r.push("#{lvl}#{hilight_attr("URL:")} #{@url}")

    r.push("#{lvl}#{hilight_attr("Notes:")}") if (!@notes.empty?)
    @notes.each_line do |line|
        r.push("#{lvl}  #{line.strip}")
    end

    additional_attributes.each do |k, v|
        r.push("#{lvl}#{hilight_attr("#{k}:")} #{v}")
    end

    if (!@attachments.empty?)
        r.push("#{lvl}#{hilight_attr("Attachments:")}")
    end
    @attachments.keys.each do |name|
        r.push("#{lvl}  #{name}")
    end

    return r.join("\n")
end

#has_attachment?(name) ⇒ Boolean

Returns:

  • (Boolean)


169
170
171
# File 'lib/rubeepass/entry.rb', line 169

def has_attachment?(name)
    return !@attachments[name].nil?
end

#has_attachment_like?(name) ⇒ Boolean

Returns:

  • (Boolean)


173
174
175
176
177
# File 'lib/rubeepass/entry.rb', line 173

def has_attachment_like?(name)
    return @attachments.any? do |k, v|
        k.downcase == name.downcase
    end
end

#has_attribute?(attr) ⇒ Boolean

Returns:

  • (Boolean)


179
180
181
# File 'lib/rubeepass/entry.rb', line 179

def has_attribute?(attr)
    return !@attributes[attr].nil?
end

#has_attribute_like?(attr) ⇒ Boolean

Returns:

  • (Boolean)


183
184
185
186
187
# File 'lib/rubeepass/entry.rb', line 183

def has_attribute_like?(attr)
    return @attributes.any? do |k, v|
        k.downcase == attr.downcase
    end
end

#is_pwned?Boolean

Returns:

  • (Boolean)


220
221
222
223
224
225
226
227
228
229
# File 'lib/rubeepass/entry.rb', line 220

def is_pwned?
    sha = Digest::SHA1.hexdigest(@password).upcase
    url = URI("https://api.pwnedpasswords.com/range/#{sha[0..4]}")
    http = Net::HTTP.new(url.host, url.port)
    http.use_ssl = true
    res = http.request(Net::HTTP::Get.new(url))

    return true if (res.body.match(/#{sha[5..-1]}/))
    return false
end

#to_sObject



265
266
267
# File 'lib/rubeepass/entry.rb', line 265

def to_s
    return details
end