summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorZack Hobson2013-12-02 12:47:17 -0800
committerZack Hobson2013-12-02 12:47:17 -0800
commit9bed66d535c008bba6e0c4c8f5d0c9c1453b422b (patch)
treebc851f4f247f2b3f09e7825a04ad7f00a2a1b3c7
parent3fabe23a82b287686e1a420ec5db6d005ca55443 (diff)
downloadhcl-9bed66d535c008bba6e0c4c8f5d0c9c1453b422b.tar.bz2
MacOS X: use keychain to save password
-rw-r--r--lib/hcl/app.rb31
1 files changed, 29 insertions, 2 deletions
diff --git a/lib/hcl/app.rb b/lib/hcl/app.rb
index 4185a67..f987eb9 100644
--- a/lib/hcl/app.rb
+++ b/lib/hcl/app.rb
@@ -135,7 +135,10 @@ EOM
def read_config force=false
if File.exists? CONFIG_FILE
- config = YAML::load File.read(CONFIG_FILE)
+ config = YAML::load(File.read(CONFIG_FILE)) || {}
+ if has_security_command
+ load_password config
+ end
TimesheetResource.configure config
elsif File.exists? OLD_CONFIG_FILE
config = YAML::load File.read(OLD_CONFIG_FILE)
@@ -159,10 +162,13 @@ EOM
def write_config config
puts "Writing configuration to #{CONFIG_FILE}."
+ if has_security_command
+ save_password config
+ end
File.open(CONFIG_FILE, 'w') do |f|
f.write config.to_yaml
end
- FileUtils.chmod 0400, CONFIG_FILE
+ FileUtils.chmod 0600, CONFIG_FILE
end
def read_settings
@@ -182,6 +188,27 @@ EOM
end
nil
end
+
+ def has_security_command
+ File.exists?('/usr/bin/security')
+ end
+
+ def load_password config
+ cmd = "security find-internet-password -l hcl -a '%s' -s '%s.harvestapp.com' -w" % [
+ config['login'],
+ config['subdomain'],
+ ]
+ password = `#{cmd}`
+ config.update('password'=>password.chomp) if $?.success?
+ end
+
+ def save_password config
+ if system("security add-internet-password -U -l hcl -a '%s' -s '%s.harvestapp.com' -w '%s'" % [
+ config['login'],
+ config['subdomain'],
+ config['password'],
+ ]) then config.delete('password') end
+ end
end
end