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.



13
14
15
# File 'lib/keyring/backends/macosx_keychain.rb', line 13

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

Instance Attribute Details

#securityObject

Returns the value of attribute security.



12
13
14
# File 'lib/keyring/backends/macosx_keychain.rb', line 12

def security
  @security
end

Instance Method Details

#delete_password(service, username) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/keyring/backends/macosx_keychain.rb', line 73

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



42
43
44
45
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
# File 'lib/keyring/backends/macosx_keychain.rb', line 42

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



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

def priority
  1
end

#security_command(operation) ⇒ Object



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

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

#set_password(service, username, password) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/keyring/backends/macosx_keychain.rb', line 27

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)


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

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