public
Last active

Warden + md5

  • Download Gist
hkshop.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
%w[rubygems digest/md5 sinatra haml sass warden rack/flash ./db/database].each{ |gem| require gem }
 
require "sinatra/reloader" if development?
 
set :root, File.dirname(__FILE__)
set :views, "views"
 
get '/main.css' do
header 'Content-Type' => 'text/css; charset=utf-8'
sass :main
end
 
class User < ActiveRecord::Base
def self.authenticate(name, password)
user = self.find_by_name(name)
user if user && ::Digest::MD5.hexdigest(::Digest::MD5.hexdigest(password)) == user.password
end
end
 
get '/' do
@users = User.all
haml :index
end
 
 
 
builder = Rack::Builder.new do
Warden::Manager.serialize_into_session{|user| user.id }
Warden::Manager.serialize_from_session{|id| User[id] }
 
Warden::Strategies.add(:password) do
def valid?
params['user'] && params['user']['name'] && params['user']['password']
end
 
def authenticate!
user = User.find_by_name(['name'])
if user && user.authenticate(['password'])
success! user
else
fail "invalid"
end
user.nil? ? fail!('Could not log in') : success!(user, 'Successfully logged in')
end
end
 
use Rack::MethodOverride
use Rack::Session::Cookie
use Rack::Flash, accessorize: [:error, :success]
use Warden::Manager do |config|
config.default_strategies :password
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.