-
-
Save mphalliday/1007890 to your computer and use it in GitHub Desktop.
Migrating to a new password encryption in devise, coming from authlogic
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
class MigrateUserToDevise < ActiveRecord::Migration | |
def self.up | |
change_table :users do |t| | |
t.string :encrypted_password, :null => false, :limit => 128 | |
# ... | |
end | |
end | |
def self.down | |
end | |
end |
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
module Devise | |
module PasswordEncryptionMigration | |
def valid_password?(incoming_password) | |
if using_authlogic_validation? | |
Devise.secure_compare(authlogic_password_digest(incoming_password), self.crypted_password).tap do |validated| | |
if validated | |
self.password = incoming_password | |
self.crypted_password = nil | |
self.save(:validate => false) | |
end | |
end | |
else | |
Devise.secure_compare(password_digest(incoming_password), self.encrypted_password) | |
end | |
end | |
def using_authlogic_validation? | |
self.encrypted_password.blank? | |
end | |
def authlogic_password_digest(password) | |
if self.password_salt.present? | |
Devise::Encryptors::AuthlogicSha512.digest(password, 20, self.password_salt, nil) | |
end | |
end | |
end | |
end |
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
class User < ActiveRecord::Base | |
# devise ... | |
include Devise::PasswordEncryptionMigration | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment