class Trocla

Public Class Methods

new(config_file=nil) click to toggle source
# File lib/trocla.rb, line 8
def initialize(config_file=nil)
  if config_file
    @config_file = File.expand_path(config_file)
  elsif File.exists?(def_config_file=File.expand_path('~/.troclarc.yaml')) || File.exists?(def_config_file=File.expand_path('/etc/troclarc.yaml'))
    @config_file = def_config_file
  end
end

Public Instance Methods

config() click to toggle source
# File lib/trocla.rb, line 73
def config
  @config ||= read_config
end
delete_password(key,format=nil) click to toggle source
# File lib/trocla.rb, line 43
def delete_password(key,format=nil)
  if format.nil?
    decrypt(cache.delete(key))
  else
    old_val = (h = cache.fetch(key,{})).delete(format)
    h.empty? ? cache.delete(key) : cache[key] = h
    decrypt(old_val)
  end
end
encryption() click to toggle source
# File lib/trocla.rb, line 66
def encryption
  enc = config['encryption']
  enc ||= :none
  @encryption ||= Trocla::Encryptions[enc].new(self)
  @encryption
end
formats(format) click to toggle source
# File lib/trocla.rb, line 62
def formats(format)
  (@format_cache||={})[format] ||= Trocla::Formats[format].new(self)
end
get_password(key, format) click to toggle source
# File lib/trocla.rb, line 34
def get_password(key, format)
  decrypt(cache.fetch(key, {})[format])
end
password(key,format,options={}) click to toggle source
# File lib/trocla.rb, line 16
def password(key,format,options={})
  options = config['options'].merge(options)
  raise "Format #{format} is not supported! Supported formats: #{Trocla::Formats.all.join(', ')}" unless Trocla::Formats::available?(format)

  unless (password=get_password(key,format)).nil?
    return password
  end

  plain_pwd = get_password(key,'plain')
  if options['random'] && plain_pwd.nil?
    plain_pwd = Trocla::Util.random_str(options['length'].to_i,options['charset'])
    set_password(key,'plain',plain_pwd) unless format == 'plain'
  elsif !options['random'] && plain_pwd.nil?
    raise "Password must be present as plaintext if you don't want a random password"
  end
  set_password(key,format,self.formats(format).format(plain_pwd,options))
end
reset_password(key,format,options={}) click to toggle source
# File lib/trocla.rb, line 38
def reset_password(key,format,options={})
  set_password(key,format,nil)
  password(key,format,options)
end
set_password(key,format,password) click to toggle source
# File lib/trocla.rb, line 53
def set_password(key,format,password)
  if (format == 'plain')
    h = (cache[key] = { 'plain' => encrypt(password) })
  else
    h = (cache[key] = cache.fetch(key,{}).merge({ format => encrypt(password) }))
  end
  decrypt h[format]
end

Private Instance Methods

build_cache() click to toggle source
# File lib/trocla.rb, line 82
def build_cache
  require 'moneta'
  lconfig = config
  Moneta.new(lconfig['adapter'], lconfig['adapter_options']||{})
end
cache() click to toggle source
# File lib/trocla.rb, line 78
def cache
  @cache ||= build_cache
end
decrypt(value) click to toggle source
# File lib/trocla.rb, line 101
def decrypt(value)
  return nil if value.nil?
  encryption.decrypt(value)
end
default_config() click to toggle source
# File lib/trocla.rb, line 106
def default_config
  require 'yaml'
  YAML.load(File.read(File.expand_path(File.join(File.dirname(__FILE__),'trocla','default_config.yaml'))))
end
encrypt(value) click to toggle source
# File lib/trocla.rb, line 97
def encrypt(value)
  encryption.encrypt(value)
end
read_config() click to toggle source
# File lib/trocla.rb, line 88
def read_config
  if @config_file.nil?
    default_config
  else
    raise "Configfile #{@config_file} does not exist!" unless File.exists?(@config_file)
    default_config.merge(YAML.load(File.read(@config_file)))
  end
end