Advertisement
Guest User

Untitled

a guest
Jul 31st, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. require 'digest/sha1'
  2.  
  3. # this model expects a certain database layout and its based on the name/login pattern.
  4.  
  5. module AuthenticatedUser
  6.  
  7. SALT = "s@xx%^&omemealsdfj234lL@#23-= _4ksdsdlasdf9asd0f9"
  8. DELAYED_DELETE_DAYS = 3
  9. SECURITY_TOKEN_LIFE_HOURS = 1024
  10. USE_EMAIL_NOTIFICATION = true
  11. EMAIL_FROM = "info@whatever.com"
  12. APP_NAME = "Whatever"
  13.  
  14. def self.included(base)
  15. base.extend(ClassMethods)
  16. end
  17.  
  18. # This module contains class methods
  19. module ClassMethods
  20. def authenticate(login, pass)
  21. user = find(:first, :conditions => ["login = ? AND verified = 1 AND deleted = 0", login])
  22. return nil if user.nil?
  23.  
  24. user = find(:first, :conditions => ["login = ? AND salted_password = ? AND verified = 1", login, AuthenticatedUser.salted_password(user.salt, AuthenticatedUser.hashed(pass))])
  25. return nil if user.nil?
  26.  
  27. user.update_attribute(:logged_in_at, Time.now)
  28. logger.debug("Authenticated by login and pass user: #{user.id} at #{user.logged_in_at}")
  29. return user
  30. end
  31.  
  32. def hashed(str)
  33. # check if a salt has been set...
  34. if SALT == nil
  35. raise "SALT cannot be nil"
  36. end
  37.  
  38. return Digest::SHA1.hexdigest("#{SALT}--#{str}--}")[0..39]
  39. end
  40.  
  41. def salted_password(salt, hashed_password)
  42. hashed(salt hashed_password)
  43. end
  44.  
  45. def acts_as_authenticated
  46. include InstanceMethods
  47.  
  48. attr_accessor :new_password
  49. attr_accessor :password, :password_confirmation
  50.  
  51. validates_presence_of :login
  52. validates_length_of :login, :within => 3..40
  53. validates_uniqueness_of :login
  54. validates_uniqueness_of :email
  55. validates_format_of :email, :with => /^[^@] @. $/
  56. validates_presence_of :password, :if => :validate_password?
  57. validates_confirmation_of :password, :if => :validate_password?
  58. validates_length_of :password, { :minimum => 5, :if => :validate_password? }
  59. validates_length_of :password, { :maximum => 40, :if => :validate_password? }
  60.  
  61. after_validation :crypt_password
  62.  
  63. after_save :falsify_new_password
  64. end
  65. end
  66.  
  67. # This module contains instance methods
  68. module InstanceMethods
  69. def initialize(attributes = nil)
  70. super
  71. @new_password = false
  72. end
  73.  
  74. def change_password(pass, confirm = nil)
  75. self.password = pass
  76. self.password_confirmation = confirm.nil? ? pass : confirm
  77. @new_password = true
  78. end
  79.  
  80.  
  81. protected
  82. def validate_password?
  83. @new_password
  84. end
  85.  
  86. def crypt_password
  87. if @new_password
  88. self.salt = AuthenticatedUser.hashed("salt-#{Time.now}")
  89. self.salted_password = AuthenticatedUser.salted_password(salt, AuthenticatedUser.hashed(@password))
  90. end
  91. end
  92.  
  93. def falsify_new_password
  94. @new_password = false
  95. true
  96. end
  97. end
  98.  
  99.  
  100. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement