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.
Constant Summary collapse
- ENTRY_MISSING =
/SecKeychainSearchCopyNext/.freeze
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.
60 61 62 63 64 |
# File 'lib/mellon/keychain.rb', line 60 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).
70 71 72 |
# File 'lib/mellon/keychain.rb', line 70 def name @name end |
#path ⇒ String (readonly)
Returns path to keychain.
67 68 69 |
# File 'lib/mellon/keychain.rb', line 67 def path @path end |
Class Method Details
.default ⇒ Keychain
Returns default keychain.
43 44 45 46 |
# File 'lib/mellon/keychain.rb', line 43 def default keychain_path = Utils.security("default-keychain")[KEYCHAIN_REGEXP, 1] new(keychain_path, ensure_exists: false) end |
.find(name) ⇒ Keychain
Find a keychain matching the given name.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/mellon/keychain.rb', line 26 def find(name) quoted = Regexp.quote(name) regexp = Regexp.new(quoted, Regexp::IGNORECASE) keychains = list keychain = keychains.find do |keychain| keychain.name =~ regexp end if keychain.nil? raise KeyError, "Could not find keychain “#{name}” in #{keychains.map(&:name).join(", ")}" end keychain end |
.list ⇒ Array<Keychain>
Returns all available keychains.
49 50 51 52 53 |
# File 'lib/mellon/keychain.rb', line 49 def list Utils.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.
13 14 15 16 17 18 19 |
# File 'lib/mellon/keychain.rb', line 13 def search(key) output = Utils.security("find-generic-password", "-l", key) new(output[/keychain: "(.+)"/i, 1], ensure_exists: false) rescue CommandError => e raise unless e. =~ ENTRY_MISSING nil end |
Instance Method Details
#==(other) ⇒ Boolean
Returns true if the keychains have the same path.
128 129 130 131 132 133 134 |
# File 'lib/mellon/keychain.rb', line 128 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.
86 87 88 89 |
# File 'lib/mellon/keychain.rb', line 86 def [](key) _, data = read(key) data end |
#[]=(key, data) ⇒ Object
Write data to entry key, or updating existing one if it exists.
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/mellon/keychain.rb', line 95 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.
122 123 124 |
# File 'lib/mellon/keychain.rb', line 122 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.
80 81 82 |
# File 'lib/mellon/keychain.rb', line 80 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.
116 117 118 |
# File 'lib/mellon/keychain.rb', line 116 def hash path.hash end |
#keys ⇒ Array<String>
Retrieve all available keys.
109 110 111 112 113 |
# File 'lib/mellon/keychain.rb', line 109 def keys Utils.parse_dump(command "dump-keychain").map do |keychain, info| info[:label] end end |