Class: Trocla

Inherits:
Object
  • Object
show all
Defined in:
lib/trocla.rb,
lib/trocla/util.rb,
lib/trocla/version.rb

Overview

Trocla class

Defined Under Namespace

Classes: Encryptions, Formats, Store, Stores, Util, VERSION

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_file = nil) ⇒ Trocla

Returns a new instance of Trocla.



9
10
11
12
13
14
15
# File 'lib/trocla.rb', line 9

def initialize(config_file = nil)
  if config_file
    @config_file = File.expand_path(config_file)
  elsif File.exist?(def_config_file = File.expand_path('~/.troclarc.yaml')) || File.exist?(def_config_file = File.expand_path('/etc/troclarc.yaml'))
    @config_file = def_config_file
  end
end

Class Method Details

.open(config_file = nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/trocla.rb', line 17

def self.open(config_file = nil)
  trocla = Trocla.new(config_file)

  if block_given?
    yield trocla
    trocla.close
  else
    trocla
  end
end

Instance Method Details

#available_format(key, options = {}) ⇒ Object



84
85
86
# File 'lib/trocla.rb', line 84

def available_format(key, options = {})
  render(false, store.formats(key), options)
end

#closeObject



104
105
106
# File 'lib/trocla.rb', line 104

def close
  store.close
end

#configObject



100
101
102
# File 'lib/trocla.rb', line 100

def config
  @config ||= read_config
end

#delete_password(key, format = nil, options = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/trocla.rb', line 68

def delete_password(key, format = nil, options = {})
  v = store.delete(key, format)
  if v.is_a?(Hash)
    Hash[*v.map do |f, encrypted_value|
      [f, render(format, decrypt(encrypted_value), options)]
    end.flatten]
  else
    render(format, decrypt(v), options)
  end
end

#encryptionObject



96
97
98
# File 'lib/trocla.rb', line 96

def encryption
  @encryption ||= Trocla::Encryptions[config['encryption']].new(config['encryption_options'], self)
end

#formats(format) ⇒ Object



92
93
94
# File 'lib/trocla.rb', line 92

def formats(format)
  (@format_cache ||= {})[format] ||= Trocla::Formats[format].new(self)
end

#get_password(key, format, options = {}) ⇒ Object



59
60
61
# File 'lib/trocla.rb', line 59

def get_password(key, format, options = {})
  render(format, decrypt(store.get(key, format)), options)
end

#password(key, format, options = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/trocla.rb', line 28

def password(key, format, options={})
  # respect a default profile, but let the
  # profiles win over the default options
  options['profiles'] ||= config['options']['profiles']
  options = merge_profiles(options['profiles']).merge(options) if options['profiles']
  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, options)).nil?
    return password
  end

  plain_pwd = get_password(key, 'plain', options)
  if options['random'] && plain_pwd.nil?
    plain_pwd = Trocla::Util.random_str(options['length'].to_i, options['charset'])
    set_password(key, 'plain', plain_pwd, options) 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
  pwd = self.formats(format).format(plain_pwd, options)
  # it's possible that meanwhile another thread/process was faster in
  # formating the password. But we want todo that second lookup
  # only for expensive formats
  if self.formats(format).expensive?
    get_password(key, format, options) || set_password(key, format, pwd, options)
  else
    set_password(key, format, pwd, options)
  end
end

#reset_password(key, format, options = {}) ⇒ Object



63
64
65
66
# File 'lib/trocla.rb', line 63

def reset_password(key, format, options = {})
  delete_password(key, format)
  password(key, format, options)
end

#search_key(key, options = {}) ⇒ Object



88
89
90
# File 'lib/trocla.rb', line 88

def search_key(key, options = {})
  render(false, store.search(key), options)
end

#set_password(key, format, password, options = {}) ⇒ Object



79
80
81
82
# File 'lib/trocla.rb', line 79

def set_password(key, format, password, options = {})
  store.set(key, format, encrypt(password), options)
  render(format, password, options)
end