Class: Trocla
- Inherits:
-
Object
- Object
- Trocla
- Defined in:
- lib/trocla.rb,
lib/trocla/util.rb,
lib/trocla/hooks.rb,
lib/trocla/version.rb
Overview
Trocla class
Defined Under Namespace
Modules: Hooks Classes: Encryptions, Formats, Store, Stores, Util, VERSION
Class Method Summary collapse
Instance Method Summary collapse
- #available_format(key, options = {}) ⇒ Object
- #close ⇒ Object
- #config ⇒ Object
- #delete_password(key, format = nil, options = {}) ⇒ Object
- #encryption ⇒ Object
- #formats(format) ⇒ Object
- #get_password(key, format, options = {}) ⇒ Object
-
#initialize(config_file = nil) ⇒ Trocla
constructor
A new instance of Trocla.
- #password(key, format, options = {}) ⇒ Object
- #reset_password(key, format, options = {}) ⇒ Object
- #search_key(key, options = {}) ⇒ Object
- #set_password(key, format, password, options = {}) ⇒ Object
Constructor Details
#initialize(config_file = nil) ⇒ Trocla
Returns a new instance of Trocla.
10 11 12 13 14 15 16 |
# File 'lib/trocla.rb', line 10 def initialize(config_file = nil) if config_file @config_file = File.(config_file) elsif File.exist?(def_config_file = File.('~/.troclarc.yaml')) || File.exist?(def_config_file = File.('/etc/troclarc.yaml')) @config_file = def_config_file end end |
Class Method Details
Instance Method Details
#available_format(key, options = {}) ⇒ Object
87 88 89 |
# File 'lib/trocla.rb', line 87 def available_format(key, = {}) render(false, store.formats(key), ) end |
#close ⇒ Object
107 108 109 |
# File 'lib/trocla.rb', line 107 def close store.close end |
#config ⇒ Object
103 104 105 |
# File 'lib/trocla.rb', line 103 def config @config ||= read_config end |
#delete_password(key, format = nil, options = {}) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/trocla.rb', line 69 def delete_password(key, format = nil, = {}) v = store.delete(key, format) hooks_runner.run('delete', key, format, ) if v.is_a?(Hash) Hash[*v.map do |f, encrypted_value| [f, render(format, decrypt(encrypted_value), )] end.flatten] else render(format, decrypt(v), ) end end |
#encryption ⇒ Object
99 100 101 |
# File 'lib/trocla.rb', line 99 def encryption @encryption ||= Trocla::Encryptions[config['encryption']].new(config['encryption_options'], self) end |
#formats(format) ⇒ Object
95 96 97 |
# File 'lib/trocla.rb', line 95 def formats(format) (@format_cache ||= {})[format] ||= Trocla::Formats[format].new(self) end |
#get_password(key, format, options = {}) ⇒ Object
60 61 62 |
# File 'lib/trocla.rb', line 60 def get_password(key, format, = {}) render(format, decrypt(store.get(key, format)), ) end |
#password(key, format, options = {}) ⇒ Object
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 58 |
# File 'lib/trocla.rb', line 29 def password(key, format, ={}) # respect a default profile, but let the # profiles win over the default options ['profiles'] ||= config['options']['profiles'] = merge_profiles(['profiles']).merge() if ['profiles'] = config['options'].merge() 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 ['random'] && plain_pwd.nil? plain_pwd = Trocla::Util.random_str(['length'].to_i, ['charset']) set_password(key, 'plain', plain_pwd, ) unless format == 'plain' elsif !['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, ) # 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, ) || set_password(key, format, pwd, ) else set_password(key, format, pwd, ) end end |
#reset_password(key, format, options = {}) ⇒ Object
64 65 66 67 |
# File 'lib/trocla.rb', line 64 def reset_password(key, format, = {}) delete_password(key, format) password(key, format, ) end |
#search_key(key, options = {}) ⇒ Object
91 92 93 |
# File 'lib/trocla.rb', line 91 def search_key(key, = {}) render(false, store.search(key), ) end |
#set_password(key, format, password, options = {}) ⇒ Object
81 82 83 84 85 |
# File 'lib/trocla.rb', line 81 def set_password(key, format, password, = {}) store.set(key, format, encrypt(password), ) hooks_runner.run('set', key, format, ) render(format, password, ) end |