Class: Sinatra::DataMapperAuth::User

Inherits:
Object
  • Object
show all
Includes:
DataMapper::Resource
Defined in:
lib/sinatra/datamapperauth.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.authenticate(login, pass) ⇒ Object



31
32
33
34
35
36
# File 'lib/sinatra/datamapperauth.rb', line 31

def self.authenticate(, pass)
  u = User.first(:login => )
  return nil if u.nil?
  return u if User.encrypt(pass, u.salt) == u.hashed_password
  nil
end

.encrypt(pass, salt) ⇒ Object



27
28
29
# File 'lib/sinatra/datamapperauth.rb', line 27

def self.encrypt(pass, salt)
  Digest::SHA1.hexdigest(pass + salt)
end

.random_string(len) ⇒ Object



38
39
40
41
42
43
# File 'lib/sinatra/datamapperauth.rb', line 38

def self.random_string(len)
  chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
  str = ""
  1.upto(len) { |i| str << chars[rand(chars.size-1)] }
  return str
end

Instance Method Details

#password=(pass) ⇒ Object



21
22
23
24
25
# File 'lib/sinatra/datamapperauth.rb', line 21

def password=(pass)
  @password = pass
  self.salt = User.random_string(10) unless self.salt
  self.hashed_password = User.encrypt(@password, self.salt)
end