class Authentication
Public Class Methods
new(password_file)
click to toggle source
# File lib/quartz_flow/authentication.rb, line 18 def initialize(password_file) @password_file = password_file @accounts = {} load_password_file(password_file) end
Public Instance Methods
add_account(login, unhashed_password)
click to toggle source
# File lib/quartz_flow/authentication.rb, line 24 def add_account(login, unhashed_password) if @accounts.has_key?login raise "The account #{login} already exists" end raise "Password cannot be empty" if unhashed_password.nil? add_account_internal(login, unhashed_password) end
authenticate(login, password)
click to toggle source
Returns true on success, false if the user cannot be authenticated
# File lib/quartz_flow/authentication.rb, line 40 def authenticate(login, password) # Reload the password file in case users were added/deleted acct = @accounts[login] return false if ! acct hashed = hash_password(password, acct.salt) hashed == acct.password_hash end
del_account(login)
click to toggle source
# File lib/quartz_flow/authentication.rb, line 32 def del_account(login) if ! @accounts.has_key?(login) raise "The account #{login} does not exist" end del_account_internal(login) end
Private Instance Methods
add_account_internal(login, unhashed_password)
click to toggle source
# File lib/quartz_flow/authentication.rb, line 63 def add_account_internal(login, unhashed_password) salt = RandString.make_random_string(10) acct = AccountInfo.new(login, hash_password(unhashed_password, salt), salt) File.open(@password_file, "a") do |file| file.puts "#{login}:#{acct.password_hash}:#{salt}" end @accounts[login] = acct end
del_account_internal(login)
click to toggle source
# File lib/quartz_flow/authentication.rb, line 76 def del_account_internal(login) tmpfile = "#{@password_file}.new" File.open(tmpfile, "w") do |outfile| File.open(@password_file, "r") do |infile| infile.each_line do |line| outfile.print line if line !~ /^#{login}:/ end end end FileUtils.mv tmpfile, @password_file end
hash_password(pass, salt)
click to toggle source
# File lib/quartz_flow/authentication.rb, line 72 def hash_password(pass, salt) Digest::SHA256.hexdigest(pass + salt) end
load_password_file(filename)
click to toggle source
# File lib/quartz_flow/authentication.rb, line 50 def load_password_file(filename) if File.exists? filename File.open(filename, "r") do |file| @accounts.clear file.each_line do |line| if line =~ /([^:]+):(.*):(.*)/ @accounts[$1] = AccountInfo.new($1,$2,$3) end end end end end