Created
August 22, 2012 19:04
-
-
Save tugboat/3428437 to your computer and use it in GitHub Desktop.
Had it working once like this
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # pakyow-auth.rb | |
| module Pakyow | |
| module Auth | |
| def self.included(obj) | |
| if defined?(DataMapper) | |
| require 'pakyow-auth/orm/datamapper/user' | |
| require 'pakyow-auth/orm/datamapper/session' | |
| # obj.send(:include, DataMapper::Resource) | |
| # obj.class.extend Pakyow::Auth::UserMethods if obj.name == "User" | |
| else | |
| puts "YIKES!! We don't have Datamapper!!!!" | |
| end | |
| end | |
| end | |
| end | |
| user.rb | |
| class User | |
| include DataMapper::Resource | |
| class << self | |
| @@login_field = :email | |
| def login_field | |
| @@login_field | |
| end | |
| end | |
| attr_accessor :password, :password_confirmation | |
| storage_names[:default] = "users" | |
| property :id, Serial | |
| property :crypted_password, String | |
| property :salt, String | |
| property :password_reset_token, String | |
| property :password_reset_token_expiration, DateTime | |
| def create_password_reset_token | |
| self.password_reset_token = rand(36**64).to_s(36) | |
| self.password_reset_token_expiration = Date.today + 5 | |
| end | |
| def password=(p) | |
| return if p.nil? || p.empty? | |
| @password = p | |
| self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{User.login_field}--") | |
| self.crypted_password = encrypt(p) | |
| end | |
| # Authenticates a user by their login name and unencrypted password. Returns the user or nil. | |
| def self.authenticate(session) | |
| u = self.first(self.login_field => session.login) # need to get the salt | |
| if u && u.authenticated?(session.password) | |
| return u | |
| else | |
| return false | |
| end | |
| end | |
| def authenticated?(password) | |
| true if self.crypted_password == encrypt(password) | |
| end | |
| private | |
| # Encrypts the password with the user salt | |
| def encrypt(password) | |
| self.encrypt(password, salt) | |
| end | |
| # Encrypts some data with the salt. | |
| def self.encrypt(password, salt) | |
| Digest::SHA1.hexdigest("--#{salt}--#{password}--") | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment