Created
March 21, 2011 21:10
-
-
Save leedo/880223 to your computer and use it in GitHub Desktop.
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
require 'em/mysql' | |
require 'sequel/async' | |
require 'digest/sha1' | |
module Hector | |
# Identity adapters must implement the following public methods: | |
# - authenticate(username, password) | |
# - remember(username, password) | |
# - forget(username) | |
# - normalize(username) | |
# | |
class MysqlIdentityAdapter | |
def initialize(options, salt) | |
EventedMysql.settings.update :host => options[:host], | |
:port => options[:port], | |
:database => options[:database], | |
:user => options[:user], | |
:password => options[:password] | |
@db = Sequel.connect :database => options[:database], :adapter => "mysql" | |
@salt = salt | |
end | |
def authenticate(username, password) | |
@db[:users].where(:username => username, :password => hash(password)).async_count do |c| | |
yield c > 0 | |
end | |
end | |
def remember(username, password) | |
@db[:users].insert(:username => username, :password => hash(password)) | |
end | |
def forget(username) | |
@db[:users].where(:username => username).delete | |
end | |
def normalize(username) | |
username | |
end | |
protected | |
def hash(password) | |
Digest::SHA1.hexdigest(password + @salt) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment