Class: SecretService::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/secret_service/collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service, name = DEFAULT_COLLECTION, path = nil) ⇒ Collection

Returns a new instance of Collection.



5
6
7
8
9
10
# File 'lib/secret_service/collection.rb', line 5

def initialize(service, name = DEFAULT_COLLECTION, path=nil)
  @service = service
  @name = name
  @path = path || "#{COLLECTION_PREFIX}#{name}"
  @proxy = @service.get_proxy(@path, IFACE[:collection])
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/secret_service/collection.rb', line 3

def name
  @name
end

#pathObject

Returns the value of attribute path.



3
4
5
# File 'lib/secret_service/collection.rb', line 3

def path
  @path
end

#serviceObject

Returns the value of attribute service.



3
4
5
# File 'lib/secret_service/collection.rb', line 3

def service
  @service
end

Instance Method Details

#all_itemsObject



80
81
82
83
84
# File 'lib/secret_service/collection.rb', line 80

def all_items
  @proxy.Get(IFACE[:collection], Items).map { |item_path|
    SecretService::Item.new(self, item_path)
  }
end

#create_item(name, secret, properties = nil, replace = true) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/secret_service/collection.rb', line 54

def create_item name, secret, properties=nil, replace=true
  if properties.nil? 
    # ruby-dbus's type inference system doesn't handle recursion for
    # vaguely complicated structs, yet the protocol requires
    # explicit type annotation.  Consequently, nontrivial structs
    # require the user to provide their own annotation
    attrs = ["a{ss}", {"name" => name.to_s }]

    properties =
      {"#{SS_PREFIX}Item.Label" => name.to_s,
      "#{SS_PREFIX}Item.Attributes" => attrs
    }
  end
  result = @proxy.CreateItem(properties, secret_encode(secret), replace)
  new_item_path = result[0]
  Item.new(self, new_item_path)
end

#get_item(name) ⇒ Object



72
73
74
# File 'lib/secret_service/collection.rb', line 72

def get_item name
  (unlocked_items({"name" => name})[0])
end

#get_property(name) ⇒ Object



38
39
40
# File 'lib/secret_service/collection.rb', line 38

def get_property name
  @proxy.Get(IFACE[:collection], name.to_s.downcase.capitalize)[0]
end

#get_secret(name) ⇒ Object



76
77
78
# File 'lib/secret_service/collection.rb', line 76

def get_secret name
  get_item(name).get_secret
end

#lock!Object



12
13
14
15
16
17
18
19
# File 'lib/secret_service/collection.rb', line 12

def lock!
  locked_objs, prompt_path = @service.proxy.Lock [@path]
  return true if prompt_path == "/" and locked_objs.include?(@path)

  # need to do the prompt dance
  @service.prompt!(prompt_path)
  locked?
end

#locked?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/secret_service/collection.rb', line 30

def locked?
  get_property(:locked)
end

#locked_items(search_pref = {}) ⇒ Object



50
51
52
# File 'lib/secret_service/collection.rb', line 50

def locked_items(search_pref = {})
  @proxy.SearchItems(search_pred)[1].map {|path| Item.new self, path }
end

#secret_encode(secret_string) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/secret_service/collection.rb', line 86

def secret_encode secret_string
  mime_type = "application/octet-stream"

  if(secret_string.respond_to? "encoding" and
     secret_string.encoding.to_s != "ASCII-8BIT")
    secret_string.encode! "UTF-8"
    #mime_type = "text/plain; charset=utf8"
  end

  [session[1], [], secret_string.bytes.to_a, mime_type]
end

#sessionObject



42
43
44
# File 'lib/secret_service/collection.rb', line 42

def session
  @service.session
end

#set_property(name, new_val) ⇒ Object



34
35
36
# File 'lib/secret_service/collection.rb', line 34

def set_property name, new_val
  @proxy.Set(IFACE[:item], name.to_s.downcase.capitalize, new_val)
end

#unlock!Object



21
22
23
24
25
26
27
28
# File 'lib/secret_service/collection.rb', line 21

def unlock!
  unlocked_objs, prompt_path = @service.proxy.Unlock [@path]
  return true if prompt_path == "/" and unlocked_objs.include?(@path)

  #nuts, stupid prompt
  @service.prompt!(prompt_path)
  ! locked?
end

#unlocked_items(search_pred = {}) ⇒ Object



46
47
48
# File 'lib/secret_service/collection.rb', line 46

def unlocked_items(search_pred = {})
  @proxy.SearchItems(search_pred)[0].map {|path| Item.new self, path }
end