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

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



21
22
23
24
# File 'lib/casserver/authenticators/sql_encrypted.rb', line 21

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



26
27
28
29
# File 'lib/casserver/authenticators/sql_encrypted.rb', line 26

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

#generate_encryption_saltObject



35
36
37
38
# File 'lib/casserver/authenticators/sql_encrypted.rb', line 35

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



31
32
33
# File 'lib/casserver/authenticators/sql_encrypted.rb', line 31

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