Sha256: f9004aeecfc8cf906c82a68b6cc53d47e8383ba40b0d8eb40a0af190d12dc69f

Contents?: true

Size: 1 KB

Versions: 6

Compression:

Stored size: 1 KB

Contents

require 'rubygems'
require 'bcrypt'

require 'csv'
require 'digest/md5'

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

6 entries across 6 versions & 1 rubygems

Version Path
murlsh-0.6.1 lib/murlsh/auth.rb
murlsh-0.6.0 lib/murlsh/auth.rb
murlsh-0.5.2 lib/murlsh/auth.rb
murlsh-0.5.1 lib/murlsh/auth.rb
murlsh-0.5.0 lib/murlsh/auth.rb
murlsh-0.4.0 lib/murlsh/auth.rb