Class: Sym::App::KeyChain

Inherits:
Object show all
Defined in:
lib/sym/app/keychain.rb

Overview

This class forms and shells several commands that wrap Mac OS-X security command. They provide access to storing generic passwords in the KeyChain Access.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key_name, opts = {}) ⇒ KeyChain

Returns a new instance of KeyChain.



38
39
40
41
42
43
# File 'lib/sym/app/keychain.rb', line 38

def initialize(key_name, opts = {})
  self.key_name = key_name
  self.opts     = opts
  self.class.validate!
  opts[:trace] ? stderr_on : stderr_off
end

Class Attribute Details

.kindObject

Returns the value of attribute kind.



14
15
16
# File 'lib/sym/app/keychain.rb', line 14

def kind
  @kind
end

.sub_sectionObject

Returns the value of attribute sub_section.



14
15
16
# File 'lib/sym/app/keychain.rb', line 14

def sub_section
  @sub_section
end

.userObject

Returns the value of attribute user.



14
15
16
# File 'lib/sym/app/keychain.rb', line 14

def user
  @user
end

Instance Attribute Details

#key_nameObject

Returns the value of attribute key_name.



36
37
38
# File 'lib/sym/app/keychain.rb', line 36

def key_name
  @key_name
end

#optsObject

Returns the value of attribute opts.



36
37
38
# File 'lib/sym/app/keychain.rb', line 36

def opts
  @opts
end

#stderr_disabledObject

Returns the value of attribute stderr_disabled.



36
37
38
# File 'lib/sym/app/keychain.rb', line 36

def stderr_disabled
  @stderr_disabled
end

Class Method Details

.configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



20
21
22
# File 'lib/sym/app/keychain.rb', line 20

def configure
  yield self
end

.get(value) ⇒ Object



16
17
18
# File 'lib/sym/app/keychain.rb', line 16

def get(value)
  self.new(value).find
end

.validate!Object

Raises:

  • (ArgumentError)


24
25
26
27
# File 'lib/sym/app/keychain.rb', line 24

def validate!
  raise ArgumentError.new(
    'User is not defined. Either set $USER in environment, or directly on the class.') unless self.user
end

Instance Method Details

#add(password) ⇒ Object



45
46
47
48
49
# File 'lib/sym/app/keychain.rb', line 45

def add(password)
  delete rescue nil
  sleep 0.1
  execute command(:add, " -T /usr/bin/security -w '#{password}' ")
end

#deleteObject



55
56
57
# File 'lib/sym/app/keychain.rb', line 55

def delete
  execute command(:delete)
end

#execute(command) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/sym/app/keychain.rb', line 59

def execute(command)
  command += ' 2>/dev/null' if stderr_disabled
  puts "> #{command.yellow}" if opts[:verbose]
  output = `#{command}`
  result = $?
  unless result.success?
    warn "> ERROR running command:\n>   $ #{output.red}" if !stderr_disabled && opts[:verbose]
    raise Sym::Errors::KeyChainCommandError.new("Command error: #{result}, command: #{command}")
  end

  output.chomp
rescue Errno::ENOENT => e
  raise Sym::Errors::KeyChainCommandError.new("Command error: #{e.message}, command: #{command}")
end

#findObject



51
52
53
# File 'lib/sym/app/keychain.rb', line 51

def find
  execute command(:find, ' -g -w ')
end

#stderr_offObject



74
75
76
# File 'lib/sym/app/keychain.rb', line 74

def stderr_off
  self.stderr_disabled = true
end

#stderr_onObject



78
79
80
# File 'lib/sym/app/keychain.rb', line 78

def stderr_on
  self.stderr_disabled = false
end