Sha256: 9cfd378e8c1685b4c5d9f20a330311fa06fea600f79d183e4871405f20cbc47b

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

require 'keychain'
require 'userinput'

module Keylime
  ##
  # 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)
    end

    def set(value)
      delete!
      keychain_segment.create(@options.merge(password: value))
    end

    def delete!
      get && keychain_segment.where(@options).first.delete
      nil
    end

    private

    def prompt(message)
      set UserInput.new(
        message: message,
        secret: true,
        attempts: 3,
        validation: /.+/
      ).ask
    end

    def keychain
      @keychain ||= if @options[:keychain]
                      Keychain.open(@options[:keychain])
                    else
                      Keychain
                    end
    end

    def key_type
      @options[:server] ? :internet_passwords : :generic_passwords
    end

    def keychain_segment
      @keychain_segment ||= keychain.send(key_type)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
keylime-0.0.3 lib/keylime/credential.rb