diff options
author | mh <mh@immerda.ch> | 2014-06-27 18:59:02 +0200 |
---|---|---|
committer | mh <mh@immerda.ch> | 2014-06-27 18:59:02 +0200 |
commit | 91b2a47d44069e23192e326890c87c9c9f92dae0 (patch) | |
tree | f1d0205dbd02675c95f148263e4f7f54c4d2bd38 | |
parent | 20de208ee827cb451e60705180909ce81eae0127 (diff) | |
parent | eabd41b10fa6da986e7e5ee2e3d93d3f19100c49 (diff) |
Merge branch 'charset_option' of https://github.com/tilya/trocla into tilya-charset_option
& simplify charset selection
Conflicts:
lib/trocla.rb
lib/trocla/util.rb
-rw-r--r-- | lib/trocla.rb | 32 | ||||
-rw-r--r-- | lib/trocla/default_config.yaml | 1 | ||||
-rw-r--r-- | lib/trocla/util.rb | 32 |
3 files changed, 36 insertions, 29 deletions
diff --git a/lib/trocla.rb b/lib/trocla.rb index 74825aa..a239be8 100644 --- a/lib/trocla.rb +++ b/lib/trocla.rb @@ -3,11 +3,11 @@ require 'trocla/util' require 'trocla/formats' class Trocla - + 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')) + 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 @@ -20,27 +20,27 @@ class Trocla return password end - plain_pwd = get_password(key,'plain') + plain_pwd = get_password(key,'plain') if options['random'] && plain_pwd.nil? - plain_pwd = Trocla::Util.random_str(options['length'].to_i,options['shellsafe']) - set_password(key,'plain',plain_pwd) unless format == 'plain' + 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,Trocla::Formats[format].format(plain_pwd,options)) end - + def get_password(key,format) cache.fetch(key,{})[format] end - + def reset_password(key,format,options={}) set_password(key,format,nil) password(key,format,options) end - + def delete_password(key,format=nil) - if format.nil? + if format.nil? cache.delete(key) else old_val = (h = cache.fetch(key,{})).delete(format) @@ -48,7 +48,7 @@ class Trocla old_val end end - + def set_password(key,format,password) if (format == 'plain') h = (cache[key] = { 'plain' => password }) @@ -57,22 +57,22 @@ class Trocla end h[format] end - + private def cache @cache ||= build_cache end - + def build_cache require 'moneta' lconfig = config Moneta.new(lconfig['adapter'], lconfig['adapter_options']||{}) end - + def config @config ||= read_config end - + def read_config if @config_file.nil? default_config @@ -81,10 +81,10 @@ class Trocla default_config.merge(YAML.load(File.read(@config_file))) end end - + def default_config require 'yaml' YAML.load(File.read(File.expand_path(File.join(File.dirname(__FILE__),'trocla','default_config.yaml')))) end - + end diff --git a/lib/trocla/default_config.yaml b/lib/trocla/default_config.yaml index f46568f..d4037fd 100644 --- a/lib/trocla/default_config.yaml +++ b/lib/trocla/default_config.yaml @@ -2,6 +2,7 @@ options: random: true length: 12 + charset: default adapter: :YAML adapter_options: :file: '/tmp/trocla.yaml' diff --git a/lib/trocla/util.rb b/lib/trocla/util.rb index ff7e3ce..78462f5 100644 --- a/lib/trocla/util.rb +++ b/lib/trocla/util.rb @@ -2,30 +2,36 @@ require 'securerandom' class Trocla class Util class << self - def random_str(length=12,shellsafe=:undef) - if shellsafe - (1..length).collect{|a| safechars[SecureRandom.random_number(safechars.size)] }.join.to_s - else - (1..length).collect{|a| chars[SecureRandom.random_number(chars.size)] }.join.to_s - end + def random_str(length=12, charset='default') + _charsets = charsets[charset] + (1..length).collect{|a| _charsets[SecureRandom.random_number(_charsets.size)] }.join.to_s end def salt(length=8) - (1..length).collect{|a| normal_chars[SecureRandom.random_number(normal_chars.size)] }.join.to_s + (1..length).collect{|a| alphanumeric[SecureRandom.random_number(alphanumeric.size)] }.join.to_s end private + + def charsets + @charsets ||= { + 'default' => chars, + 'alphanumeric' => alphanumeric, + 'shellsafe' => shellsafe, + } + end + def chars - @chars ||= normal_chars + special_chars + @chars ||= shellsafe + special_chars end - def safechars - @chars ||= normal_chars + shellsafe_chars + def shellsafe + @chars ||= alphanumeric + shellsafe_chars end - def normal_chars - @normal_chars ||= ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a + def alphanumeric + @alphanumeric ||= ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a end def special_chars - @special_chars ||= "+*%/()@&=?![]{}-_.,;:".split(//) + @special_chars ||= "*()&![]{}-".split(//) end def shellsafe_chars @shellsafe_chars ||= "+%/@=?_.,:".split(//) |