I’m weighing the pros and cons of using “Authentication from Scratch” (as implemented in this Railscast) vs. using Devise.
I’m using a custom datastore, so using Devise isn’t as simple as just following the README. It would require writing a custom ORM adaptor, which is far from trivial.
Given all this, the Railscast Auth from scratch seems much easier to implement.
So how secure is it?
Update: I should have pointed out that I’m using Parse.com as my datastore. This means they take care of hashing passwords and constraining uniqueness of usernames.
They both work by using bcrypt to generate a salted hash of the password, the only difference is that devise (by default) uses a higher cost for the hash (i.e. it would take more cpu cycles to brute force it), but of course you could easily add that to the railscast code, so roughly equivalent in that respect.
The railscast version does seem to be vulnerable to timing attacks as just doing == won’t give you a constant time compare operation. In a nutshell a timing attack works because a password where the hash was completely wrong would take less time for == to reject than a password where first half of the bytes were correct (and so == had to consider more bytes before bailing). It may seem that any such difference would be erased by noise from variations in network latency and so on but people have mounted real attacks to recover keys using these approaches.
You could obviously borrow the secure compare bit from devise though, but it does go to show that there are non obvious issues involved.
Obviously devise gives you a lot more than just authentication!