Sha256: d280a8346c736e34528085954a7375a7b3c6fe347e7323ed05ca553b683f9981

Contents?: true

Size: 997 Bytes

Versions: 7

Compression:

Stored size: 997 Bytes

Contents

%w{
csv
digest/md5

bcrypt
}.each { |m| require m }

module Murlsh

  # Interface to authentication file. Format of authentication file:
  #
  # username,MD5 hash of email address,bcrypted password
  #
  # Authentication is done using password only to make adding easier and
  # because there will be a small number of trusted users.
  #
  # See Rakefile for user maintenance tasks.
  class Auth

    def initialize(file); @file = file; end

    # Authenticate a user by password. Return their name and email if correct.
    def auth(password)
      CSV::Reader.parse(open(@file)) do |row|
        return { :name => row[0], :email => row[1] } if
          BCrypt::Password.new(row[2]) == password
      end
    end

    # Add a user to the authentication file.
    def add_user(username, email, password)
      Murlsh::openlock(@file, 'a') do |f|
        f.write("#{[username, Digest::MD5.hexdigest(email),
          BCrypt::Password.create(password)].join(',')}\n")
      end
    end

  end

end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
murlsh-1.0.0 lib/murlsh/auth.rb
murlsh-0.11.0 lib/murlsh/auth.rb
murlsh-0.10.0 lib/murlsh/auth.rb
murlsh-0.9.0 lib/murlsh/auth.rb
murlsh-0.8.1 lib/murlsh/auth.rb
murlsh-0.8.0 lib/murlsh/auth.rb
murlsh-0.7.0 lib/murlsh/auth.rb