class Qwik::PasswordGenerator

Constants

DEFAULT_SITE_PASSWORD
GENERATION_FILE
PASSWORD_FILE

Public Class Methods

new(config) click to toggle source
# File vendor/qwik/lib/qwik/password.rb, line 24
def initialize(config)
  etc_path = config.etc_dir.path
  @site_password_file = etc_path + PASSWORD_FILE
  @site_password = DEFAULT_SITE_PASSWORD
  if @site_password_file.exist?
    @site_password = @site_password_file.read.to_s.chomp
  end
  @generation_file = etc_path + GENERATION_FILE
  @generation = PasswordGenerator.generation_get(@generation_file)
end

Private Class Methods

generate_md5hex(site_password, user, generation=0) click to toggle source
# File vendor/qwik/lib/qwik/password.rb, line 90
def self.generate_md5hex(site_password, user, generation=0)
  return "#{user}:#{site_password}:#{generation}".md5hex
end
generation_add(file, user, gen) click to toggle source
# File vendor/qwik/lib/qwik/password.rb, line 94
def self.generation_add(file, user, gen)
  file.add(",#{user},#{gen}\n")
end
generation_get(file) click to toggle source
# File vendor/qwik/lib/qwik/password.rb, line 78
def self.generation_get(file)
  file.write('') if ! file.exist?
  str = file.read
  generation = {}
  str.each {|line|
    next unless line[0] == ?,
    dummy, user, gen = line.chomp.split(',')
    generation[user] = gen.to_i
  }
  return generation
end
generation_store(file, generation) click to toggle source
# File vendor/qwik/lib/qwik/password.rb, line 98
def self.generation_store(file, generation)
  str = generation.map {|user, gen|
    [user, gen]
  }.sort.map {|user, gen|
    ",#{user},#{gen}\n"
  }.join
  file.write(str)
end

Public Instance Methods

generate(user) click to toggle source
# File vendor/qwik/lib/qwik/password.rb, line 35
def generate(user)
  generation = 0
  generation = @generation[user] if @generation[user]
  return PasswordGenerator.generate_md5hex(@site_password,
                                           user, generation).hex.to_s[-8, 8]
end
generate_hex(user) click to toggle source
# File vendor/qwik/lib/qwik/password.rb, line 42
def generate_hex(user)
  generation = 0
  generation = @generation[user] if @generation[user]
  return PasswordGenerator.generate_md5hex(@site_password,
                                           user, generation).upcase[0, 8]
end
generation_inc(user) click to toggle source
# File vendor/qwik/lib/qwik/password.rb, line 61
def generation_inc(user)
  generation = PasswordGenerator.generation_get(@generation_file)
  gen = 0
  gen = generation[user] if generation[user]
  gen += 1          # Increment the generation of the user.
  PasswordGenerator.generation_add(@generation_file, user, gen)
  @generation = PasswordGenerator.generation_get(@generation_file)
  return gen
end
generation_store() click to toggle source
# File vendor/qwik/lib/qwik/password.rb, line 71
def generation_store
  PasswordGenerator.generation_store(@generation_file, @generation)
  return nil
end
match?(user, pass) click to toggle source
# File vendor/qwik/lib/qwik/password.rb, line 49
def match?(user, pass)
  return false if user.nil? || user.empty?
  return false if pass.nil? || pass.empty?

  pa = generate(user)
  return true if pa == pass
  pa = generate_hex(user)
  return true if pa == pass
  return true if pa == pass.upcase
  return false
end