mirror of
https://github.com/zhigang1992/boxen.git
synced 2026-04-30 04:54:53 +08:00
Extract keychain fun
This commit is contained in:
61
lib/boxen/keychain.rb
Normal file
61
lib/boxen/keychain.rb
Normal 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
|
||||
Reference in New Issue
Block a user