Migrating from Laravel to Rails
Apr 10, 2015
Internship at a startup is pretty awesome. Not only do you learn a lot, you also get to make many decisions and take on projects that you’d be unlikely to receive at a large company. For example, I was recently given the task of migrating many features of the existing Laravel project into Rails. Which other company will let you create their Rails application from scratch? Anyways, there were quite a few interesting problems that I faced during the process. I’ll document some as I come across them. Hopefully, they will entertain you.
User authentication was the first challenge. Laravel and Rails both supported bcrypt, but their versions were different. There was however a pattern to the password hashes Laravel generated. They all began with
$2y$. I tried reading through Laravel source to find why, but was unable to find exactly how a password is hashed. Luckily, I came across this in the bcrypt wikipedia article:
The prefix “$2a$” or “2y” in a hash string in a shadow password file indicates that hash string is a bcrypt hash…1
And this code snippet:
laravel_pass = '$2y$10$IxjHaRRbEAZO1UwMvHPvduC1nSXWdSEQSRU7ZingMiwOkJr.A6Xnu' ruby_pass = BCrypt::Password.new laravel_pass.gsub('$2y$','$2a$') ruby_pass.is_password? 'xxxxxxxx'
This allowed me to write a custom password validation function.
class User < ActiveRecord::Base def valid_password?(password) laravel_pass = self.password ruby_pass = BCrypt::Password.new laravel_pass.gsub('$2y$','$2a$') ruby_pass.is_password? password end end