Module: CASServer::Authenticators::SQLEncrypted::EncryptedPassword

Included in:
CASUser
Defined in:
lib/casserver/authenticators/sql_encrypted.rb

Overview

Include this module into your application’s user model.

Your model must have an ‘encrypted_password’ column where the password will be stored, and an ‘encryption_salt’ column that will be populated with a random string before the user record is first created.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(mod) ⇒ Object



54
55
56
57
# File 'lib/casserver/authenticators/sql_encrypted.rb', line 54

def self.included(mod)
  raise "#{self} should be inclued in an ActiveRecord class!" unless mod.respond_to?(:before_save)
  mod.before_save :generate_encryption_salt
end

Instance Method Details

#encrypt(str) ⇒ Object



59
60
61
62
# File 'lib/casserver/authenticators/sql_encrypted.rb', line 59

def encrypt(str)
  generate_encryption_salt unless encryption_salt
  Digest::SHA256.hexdigest("#{encryption_salt}::#{str}")
end

#generate_encryption_saltObject



68
69
70
71
# File 'lib/casserver/authenticators/sql_encrypted.rb', line 68

def generate_encryption_salt
  self.encryption_salt = Digest::SHA1.hexdigest(Crypt::ISAAC.new.rand(2**31).to_s) unless
    encryption_salt
end

#password=(password) ⇒ Object



64
65
66
# File 'lib/casserver/authenticators/sql_encrypted.rb', line 64

def password=(password)
  self[:encrypted_password] = encrypt(password)
end