lib/keylime/credential.rb in keylime-0.0.3 vs lib/keylime/credential.rb in keylime-0.1.0
- old
+ new
@@ -1,19 +1,24 @@
-require 'keychain'
require 'userinput'
module Keylime
+ ENABLED = begin
+ require 'keychain'
+ true
+ rescue LoadError
+ raise if RUBY_PLATFORM =~ /darwin/
+ false
+ end
+
##
# Easy wrapper around getting and setting secrets
class Credential
def initialize(params = {})
@options = params
- @enabled = RUBY_PLATFORM =~ /darwin/ || ENV['CI']
end
def get
- return unless @enabled
keychain_segment.where(@options).first
end
def get!(message)
get || prompt(message)
@@ -39,21 +44,40 @@
validation: /.+/
).ask
end
def keychain
- @keychain ||= if @options[:keychain]
- Keychain.open(@options[:keychain])
- else
- Keychain
- end
+ return @keychain if @keychain
+ @keychain = StubKeychain.new unless Keylime::ENABLED
+ @keychain ||= Keychain.open(@options[:keychain]) if @options[:keychain]
+ @keychain ||= Keychain
end
def key_type
@options[:server] ? :internet_passwords : :generic_passwords
end
def keychain_segment
@keychain_segment ||= keychain.send(key_type)
end
+ end
+
+ ##
+ # Stub keychain for if keylime is running on a non-Mac
+ class StubKeychain
+ def segment
+ StubKeychainSegment.new
+ end
+ alias internet_passwords segment
+ alias generic_passwords segment
+ end
+
+ ##
+ # Stub segment for if keylime is running on a non-Mac
+ class StubKeychainSegment
+ def where
+ []
+ end
+
+ def create() end
end
end