Class: Keyring::Backend::MacosxKeychain

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

Overview

This is a keyring backend for the Apple Keychain en.wikipedia.org/wiki/Keychain_(Apple)

Instance Method Summary collapse

Methods inherited from Keyring::Backend

create, register_implementation

Constructor Details

#initializeMacosxKeychain

Returns a new instance of MacosxKeychain.



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

def initialize
  require 'keychain'
rescue LoadError
end

Instance Method Details

#delete_password(service, username) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/keyring/backends/macosx_keychain.rb', line 59

def delete_password(service, username)

  scope = Keychain.generic_passwords.where(
    :service  => service,
    :account  => username,
  )

  return false if scope.nil?
  return false if scope.first.nil?

  scope.first.delete

end

#get_password(service, username) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/keyring/backends/macosx_keychain.rb', line 45

def get_password(service, username)

  scope = Keychain.generic_passwords.where( 
    :service  => service, 
    :account  => username,
  )

  return false if scope.nil?
  return false if scope.first.nil?

  scope.first.password

end

#priorityObject



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

def priority
  1
end

#set_password(service, username, password) ⇒ Object

NB: Uses default keychain for everything. This is consistent with the GnomeKeyring implementation No mechanism is provided in the API to support alternative keyring files.



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

def set_password(service, username, password)

  Keychain.generic_passwords.create(
    :service  => service,
    :password => password,
    :account  => username,
  )

  return true

rescue Keychain::Error
  return false
rescue KeychainDuplicateItemError
  return true
end

#supported?Boolean

Returns:

  • (Boolean)


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

def supported?
  defined?(::Keychain) && true
end