Class: Maguro::Keychain

Inherits:
Object
  • Object
show all
Defined in:
lib/maguro/keychain.rb

Class Method Summary collapse

Class Method Details

.add_account(server, username, password) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/maguro/keychain.rb', line 7

def (server, username, password)
  puts "Keychain: adding credentials with username #{username} for server #{server}"
  server = custom_server(server)

  # Try to delete existing password, in case it already exists
  (server)

  # Add the new password
  r = %x[security add-internet-password -a #{Shellwords::escape(username)} -s #{Shellwords::escape(server)} -w #{Shellwords::escape(password)} > /dev/null 2>&1]
end

.delete_account(server) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/maguro/keychain.rb', line 18

def (server)
  puts "Keychain: attempting to remove credentials for #{server}"
  server = custom_server(server)

  # Suppress output, since command will expectedly fail if credentials have not yet been saved for the server
  r = %x[security delete-internet-password -s #{Shellwords::escape(server)} > /dev/null 2>&1]
end

.retrieve_account(server) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/maguro/keychain.rb', line 26

def (server)
  puts "Keychain: attempting to retrieve credentials for #{server}"
  server = custom_server(server)

  # Retrieve the password from the keychain, but do not print it to STDOUT
  output = %x[security  2>&1 >/dev/null find-internet-password -gs #{Shellwords::escape(server)}]
  password = output[/^password: "(.*)"$/, 1]
  return nil if password.nil?

  output = %x[security find-internet-password -s #{Shellwords::escape(server)}]
  username = output[/"acct"<blob>="(.*)"$/, 1]
  return nil if username.nil?
  
  { username: username, password: password }
end