Class: Keyring::Backend::MacosxKeychain

Inherits:
Keyring::Backend show all
Defined in:
lib/keyring/backends/macosx_keychain.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Keyring::Backend

create, register_implementation

Constructor Details

#initializeMacosxKeychain

Returns a new instance of MacosxKeychain.



17
18
19
# File 'lib/keyring/backends/macosx_keychain.rb', line 17

def initialize
  @security = '/usr/bin/security'
end

Instance Attribute Details

#securityObject

Returns the value of attribute security.



16
17
18
# File 'lib/keyring/backends/macosx_keychain.rb', line 16

def security
  @security
end

Instance Method Details

#delete_password(service, username) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/keyring/backends/macosx_keychain.rb', line 77

def delete_password(service, username)
  cmd = [
    @security,
    security_command('delete'),
    '-s', service,
    '-a', username,
  ]
  Open3.popen3(*cmd) do |stdin, stdout, stderr, wait_thr|
    case wait_thr.value.exitstatus
    when 0
      return true
    when 44
      return false
    else
      raise
    end
  end
end

#get_password(service, username) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/keyring/backends/macosx_keychain.rb', line 46

def get_password(service, username)
  password = nil
  cmd = [
    @security,
    security_command('find'),
    '-s', service,
    '-a', username,
    '-g',
    # '-w',
  ]
  Open3.popen3(*cmd) do |stdin, stdout, stderr, wait_thr|
    stderr.each do |line|
      if line =~ /\Apassword: (.*)/
        pw = $1
        if pw == ''
          password = pw
        elsif pw =~ /\A"(.*)"\z/
          password = $1
        elsif pw =~ /\A0x(\h+)/
          password = [$1].pack("H*")
        end
      end
    end
    # security exits with 44 if the entry does not exist.  We just want to return
    # nil rather than raise an exception in that case.
    if ![0,44].include?(wait_thr.value.exitstatus)
      raise
    end
  end
  password
end

#priorityObject



23
24
25
# File 'lib/keyring/backends/macosx_keychain.rb', line 23

def priority
  1
end

#security_command(operation) ⇒ Object



27
28
29
# File 'lib/keyring/backends/macosx_keychain.rb', line 27

def security_command(operation)
  "#{operation}-generic-password"
end

#set_password(service, username, password) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/keyring/backends/macosx_keychain.rb', line 31

def set_password(service, username, password)
  cmd = [
    @security,
    security_command('add'),
    '-s', service,
    '-a', username,
    # FIXME: password in command line, sad panda!
    '-w', password,
    '-U',
  ]
  system(*cmd)
  if !$?.success?
    raise
  end
end

#supported?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/keyring/backends/macosx_keychain.rb', line 20

def supported?
  File.exist?(@security) && `#{@security} -h`.include?('find-generic-password')
end