Extract keychain fun

This commit is contained in:
John Barnette
2013-02-13 18:33:24 -08:00
parent efedab9628
commit 35893cd156

61
lib/boxen/keychain.rb Normal file
View File

@@ -0,0 +1,61 @@
require "shellwords"
module Boxen
class Keychain
# The keychain proxy we use to provide isolation and a friendly
# message in security prompts.
HELPER = File.expand_path "../../../script/Boxen", __FILE__
# The service name to use when loading/saving passwords.
PASSWORD_SERVICE = "GitHub Password"
# The service name to use when loading/saving API keys.
TOKEN_SERVICE = "GitHub API Token"
def initialize(login)
@login = login
end
def password
get PASSWORD_SERVICE
end
def password=(password)
set PASSWORD_SERVICE, password
end
def token
get TOKEN_SERVICE
end
def token=(token)
set TOKEN_SERVICE, token
end
protected
attr_reader :login
def get(service)
raw = [HELPER, service, login]
cmd = raw.map { |s| Shellwords.shellescape s }.join " "
result = `#{cmd}`.strip
$?.success? ? result : nil
end
def set(service, password)
cmd = [HELPER, service, login, password]
unless system *cmd
raise Boxen::Error, "Can't save #{service} in the keychain."
end
password
end
end
end