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



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

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



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

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

#generate_encryption_saltObject



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

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



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

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