Class: Mellon::Keychain
- Inherits:
-
Object
- Object
- Mellon::Keychain
- Defined in:
- lib/mellon/keychain.rb
Overview
Keychain provides simple methods for reading and storing keychain entries.
Instance Attribute Summary collapse
-
#name ⇒ String
readonly
Keychain name (without extension).
-
#path ⇒ String
readonly
Path to keychain.
Class Method Summary collapse
-
.default ⇒ Keychain
Default keychain.
-
.find(name) ⇒ Keychain
Find a keychain matching the given name.
-
.list ⇒ Array<Keychain>
All available keychains.
-
.search(key) ⇒ Keychain?
Find the first keychain that contains the key.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
True if the keychains have the same path.
-
#[](key) ⇒ String?
Contents of entry at key, or nil if not set.
-
#[]=(key, data) ⇒ Object
Write data to entry key, or updating existing one if it exists.
-
#eql?(other) ⇒ Boolean
True if the keychains have the same path.
-
#fetch(key, *args) { ... } ⇒ String
Retrieve a value, but if it does not exist return the default value, or call the provided block, or raise an error.
-
#hash ⇒ Object
A hash unique to keychains of the same path.
-
#initialize(path, ensure_exists: true) ⇒ Keychain
constructor
Initialize a keychain on the given path.
-
#keys ⇒ Array<String>
Retrieve all available keys.
Constructor Details
#initialize(path, ensure_exists: true) ⇒ Keychain
Initialize a keychain on the given path.
56 57 58 59 60 |
# File 'lib/mellon/keychain.rb', line 56 def initialize(path, ensure_exists: true) @path = path @name = File.basename(path, ".keychain") command "show-keychain-info" if ensure_exists end |
Instance Attribute Details
#name ⇒ String (readonly)
Returns keychain name (without extension).
66 67 68 |
# File 'lib/mellon/keychain.rb', line 66 def name @name end |
#path ⇒ String (readonly)
Returns path to keychain.
63 64 65 |
# File 'lib/mellon/keychain.rb', line 63 def path @path end |
Class Method Details
.default ⇒ Keychain
Returns default keychain.
39 40 41 42 |
# File 'lib/mellon/keychain.rb', line 39 def default keychain_path = ShellUtils.security("default-keychain")[KEYCHAIN_REGEXP, 1] new(keychain_path, ensure_exists: false) end |
.find(name) ⇒ Keychain
Find a keychain matching the given name.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/mellon/keychain.rb', line 23 def find(name) quoted = Regexp.quote(name) regexp = Regexp.new(quoted, Regexp::IGNORECASE) keychain = list.find do |keychain| keychain.path =~ regexp end if keychain.nil? raise KeyError, "Could not find keychain “#{name}” in #{list.map(&:name).join(", ")}" end keychain end |
.list ⇒ Array<Keychain>
Returns all available keychains.
45 46 47 48 49 |
# File 'lib/mellon/keychain.rb', line 45 def list ShellUtils.security("list-keychains").scan(KEYCHAIN_REGEXP).map do |(keychain_path)| new(keychain_path, ensure_exists: false) end end |
.search(key) ⇒ Keychain?
Find the first keychain that contains the key.
11 12 13 14 15 16 |
# File 'lib/mellon/keychain.rb', line 11 def search(key) output = ShellUtils.security("find-generic-password", "-l", key) new(output[/keychain: "(.+)"/i, 1], ensure_exists: false) rescue CommandError nil end |
Instance Method Details
#==(other) ⇒ Boolean
Returns true if the keychains have the same path.
124 125 126 127 128 129 130 |
# File 'lib/mellon/keychain.rb', line 124 def ==(other) if other.is_a?(Keychain) path == other.path else super end end |
#[](key) ⇒ String?
Returns contents of entry at key, or nil if not set.
82 83 84 85 |
# File 'lib/mellon/keychain.rb', line 82 def [](key) _, data = read(key) data end |
#[]=(key, data) ⇒ Object
Write data to entry key, or updating existing one if it exists.
91 92 93 94 95 96 97 98 99 100 |
# File 'lib/mellon/keychain.rb', line 91 def []=(key, data) info, _ = read(key) info ||= {} if data write(key, data, info) else delete(key, info) end end |
#eql?(other) ⇒ Boolean
Returns true if the keychains have the same path.
118 119 120 |
# File 'lib/mellon/keychain.rb', line 118 def eql?(other) self == other or super end |
#fetch(key, *args) { ... } ⇒ String
Retrieve a value, but if it does not exist return the default value, or call the provided block, or raise an error. See Hash#fetch.
76 77 78 |
# File 'lib/mellon/keychain.rb', line 76 def fetch(key, *args, &block) self[key] or {}.fetch(key, *args, &block) end |
#hash ⇒ Object
Returns a hash unique to keychains of the same path.
112 113 114 |
# File 'lib/mellon/keychain.rb', line 112 def hash path.hash end |
#keys ⇒ Array<String>
Retrieve all available keys.
105 106 107 108 109 |
# File 'lib/mellon/keychain.rb', line 105 def keys Utils.parse_dump(command "dump-keychain").map do |keychain, info| info[:label] end end |