Created
March 1, 2012 21:52
-
-
Save nicolai86/1953479 to your computer and use it in GitHub Desktop.
Custom authenticator to be used by rubycas-server
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
# encoding: UTF-8 | |
require 'casserver/authenticators/sql' | |
require 'devise/encryptors/base' | |
require 'devise/encryptors/sha1' | |
class CustomAuthenticator < CASServer::Authenticators::SQL | |
# snip from devise lib | |
def secure_compare(a, b) | |
return false unless a.present? && b.present? | |
return false unless a.bytesize == b.bytesize | |
l = a.unpack "C#{a.bytesize}" | |
res = 0 | |
b.each_byte { |byte| res |= byte ^ l.shift } | |
res == 0 | |
end | |
# copied from devise.rb initializer | |
DEVISE_STRETCHES = 7 | |
DEVISE_PEPPER = 'my-devise-pepper' | |
def password_digest(password, password_salt) | |
Devise::Encryptors::Sha1.digest(password, DEVISE_STRETCHES, password_salt, DEVISE_PEPPER) | |
end | |
def valid_for_authentication?(user, incoming_password) | |
secure_compare(password_digest(incoming_password,user.password_salt), user.encrypted_password) | |
end | |
def validate(credentials) | |
read_standard_credentials(credentials) | |
raise_if_not_configured | |
user_model = self.class.user_model | |
username_column = @options[:username_column] || "username" | |
$LOG.debug "#{self.class}: [#{user_model}] " + "Connection pool size: #{user_model.connection_pool.instance_variable_get(:@checked_out).length}/#{user_model.connection_pool.instance_variable_get(:@connections).length}" | |
results = user_model.find(:all, :conditions => ["#{username_column} = ?", @username]) | |
user_model.connection_pool.checkin(user_model.connection) | |
if results.size > 0 | |
$LOG.warn("Multiple matches found for user '#{@username}'") if results.size > 1 | |
user = results.first | |
unless @options[:extra_attributes].blank? | |
if results.size > 1 | |
$LOG.warn("#{self.class}: Unable to extract extra_attributes because multiple matches were found for #{@username.inspect}") | |
else | |
extract_extra(user) | |
log_extra | |
end | |
end | |
return valid_for_authentication? user, @password | |
else | |
return false | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment