Skip to content

Instantly share code, notes, and snippets.

@estum
Last active December 21, 2015 05:49
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save estum/6259863 to your computer and use it in GitHub Desktop.
Save estum/6259863 to your computer and use it in GitHub Desktop.
Migrate passwords from legacy systems to Devise
# updated variant of older solution:
# http://www.davidverhasselt.com/2012/05/13/how-to-migrate-passwords-from-legacy-systems-to-devise
class User < ActiveRecord::Base
# ...
def valid_password?(password)
if legacy_password?
# Use Devise's secure_compare to avoid timing attacks
return false unless super User.legacy_password(password)
self.attributes = { password: password, password_confirmation: password, legacy_password: false }
self.save!
end
super password
end
# Put your legacy password hashing method here
def self.legacy_password(password)
return Digest::MD5.hexdigest("#{password}-salty-herring");
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment