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.



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/rubeepass/entry.rb', line 185

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



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/rubeepass/entry.rb', line 204

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.



7
8
9
# File 'lib/rubeepass/entry.rb', line 7

def group
  @group
end

#keepassObject

Returns the value of attribute keepass.



8
9
10
# File 'lib/rubeepass/entry.rb', line 8

def keepass
  @keepass
end

#notesObject

Returns the value of attribute notes.



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

def notes
  @notes
end

#passwordObject

Returns the value of attribute password.



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

def password
  @password
end

#pathObject

Returns the value of attribute path.



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

def path
  @path
end

#titleObject

Returns the value of attribute title.



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

def title
  @title
end

#urlObject

Returns the value of attribute url.



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

def url
  @url
end

#usernameObject

Returns the value of attribute username.



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

def username
  @username
end

#uuidObject

Returns the value of attribute uuid.



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

def uuid
  @uuid
end

Class Method Details

.from_xml(keepass, parent, xml) ⇒ Object



106
107
108
109
110
111
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
# File 'lib/rubeepass/entry.rb', line 106

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



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/rubeepass/entry.rb', line 144

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 Error::InvalidProtectedData.new
    end
    raise Error::InvalidProtectedData.new if (data.nil?)

    return keepass.protected_decryptor.add_to_stream(data)
end

Instance Method Details

#<=>(other) ⇒ Object



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

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

#==(other) ⇒ Object



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

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

#additional_attributesObject



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

def additional_attributes
    return attributes.select do |key, value|
        key.match(/^(notes|password|title|url|username)$/i).nil?
    end
end

#attachment(name) ⇒ Object



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

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

#attachmentsObject



39
40
41
42
43
44
45
46
47
# File 'lib/rubeepass/entry.rb', line 39

def attachments
    attachments = Hash.new

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

    return attachments
end

#attribute(attr) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rubeepass/entry.rb', line 49

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



62
63
64
65
66
67
68
69
70
71
# File 'lib/rubeepass/entry.rb', line 62

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



73
74
75
76
77
78
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
# File 'lib/rubeepass/entry.rb', line 73

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)


161
162
163
164
165
# File 'lib/rubeepass/entry.rb', line 161

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

#has_attribute?(attr) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#to_sObject



238
239
240
# File 'lib/rubeepass/entry.rb', line 238

def to_s
    return details
end