Class: MacKeychain
Defined Under Namespace
Classes: Key
Instance Method Summary collapse
-
#create(username, password) ⇒ Object
Create Keychain item with username (Account of Keychain item) and password Returns creation status code, please see Keychain Services Result Codes.
-
#delete(username) ⇒ Object
Delete Keychain item by username (Account of Keychain item) Raises error when delete failed.
-
#find(username) ⇒ Object
Find Key by username (Account of Keychain item).
-
#initialize(service) ⇒ MacKeychain
constructor
:service => Name of keychain item, Application name or service name.
-
#save(username, password) ⇒ Object
Create Keychain item if it does not exist, otherwise update it.
-
#update(username, password) ⇒ Object
Update Keychain item by username with new password Returns update status code, please see Keychain Services Result Codes.
Constructor Details
#initialize(service) ⇒ MacKeychain
:service => Name of keychain item, Application name or service name
31 32 33 |
# File 'lib/mac_keychain.rb', line 31 def initialize(service) @service = service end |
Instance Method Details
#create(username, password) ⇒ Object
Create Keychain item with username (Account of Keychain item) and password Returns creation status code, please see Keychain Services Result Codes.
50 51 52 |
# File 'lib/mac_keychain.rb', line 50 def create(username, password) SecKeychainAddGenericPassword(nil, @service.length, @service, username.length, username, password.length, password, nil) end |
#delete(username) ⇒ Object
Delete Keychain item by username (Account of Keychain item) Raises error when delete failed
56 57 58 59 60 61 62 |
# File 'lib/mac_keychain.rb', line 56 def delete(username) if key = find(username) unless SecKeychainItemDelete(key.item) == 0 raise "Could not delete Keychain item by #{username} of #{@service}" end end end |
#find(username) ⇒ Object
Find Key by username (Account of Keychain item)
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/mac_keychain.rb', line 36 def find(username) status, *data = SecKeychainFindGenericPassword(nil, @service.length, @service, username.length, username) case status when ErrSecItemNotFound nil when 0 Key.create(data) else raise "Error when accessing Keychain looking for key: #{username} of #{@service}" end end |
#save(username, password) ⇒ Object
Create Keychain item if it does not exist, otherwise update it. Returns creation/update status code, please see Keychain Services Result Codes.
73 74 75 76 77 78 79 80 |
# File 'lib/mac_keychain.rb', line 73 def save(username, password) code = create(username, password) if ErrSecDuplicateItem == code update(username, password) else code end end |
#update(username, password) ⇒ Object
Update Keychain item by username with new password Returns update status code, please see Keychain Services Result Codes.
66 67 68 69 |
# File 'lib/mac_keychain.rb', line 66 def update(username, password) key = self.find(username) SecKeychainItemModifyContent(key.item, nil, password.length, password) end |