«
»

Rails

Persistent Cookie Authentication Generator

07.06.08 | 4 Comments

Rails

This is a Rails gem that I wrote, or rather extracted from the codebase from hushyhushy.com. OK, what is PersistentCookie Authentication Generator?

This gem is a code generator.  This generator creates an authentication system with persistent cookie management.

Feature include

  • a model which uses SHA1 encryption and salted hashes for passwords
  • a controller with signup, login, welcome and logoff actions
  • gmail smtp server integration
  • account creation that requires account verification via email
  • supports forgotten and changed passwords
  • a mixin which lets you easily add advanced authentication features to your abstract base controller
  • extensive unit and functional test cases to make sure nothing breaks.
  • token based authentication
  • persistent cookie management that allows anonymous users to be authenticated via cookies

Actually the code is heavily modified from another well known gem: salted hash login generator

To put it simply, it is

persistent_cookie_authentication = salted_hash_loginlocalization + persistent_cookie_management

The code is written with an emphasis on simplicity. No effort is made from me to write clever or DRY(don’t repeat yourself) code. The intention is to dumb it down so that people using it could understand it. As Joel says, abstraction is bad.

kiss

Here’s the installation instructions:

To install, run this line on the shell

gem install persistent_cookie_authentication_generator

From the directory of your Rails app, run this from the shell

ruby script/generate persistent_cookie_authentication

Change your config/database.yml settings. Then update your database by running

rake db:migrate

From app/controllers/application.rb, add this within application.rb

include UserSystem

Example application.rb

From config/environment.rb, add this at the end of the file

require 'environments/user_environment'
require 'smtp_tls'

Example environment.rb

From config/environments/, add this to the end of both development.rb and production.rb

ActionMailer::Base.smtp_settings = {:address => "smtp.gmail.com",
:port => "587",
:domain => "localhost.localdomain",
:authentication => :plain,
:user_name => "yourgmailusername",
:password => "yourgmailpassword"
}

Example development.rb

From config/initializers/inflections.rb, modify your inflections.rb to look like this

Inflector.inflections do |inflect|#   inflect.plural /^(ox)$/i, '1en'
#   inflect.singular /^(ox)en/i, '1'
#   inflect.irregular 'person', 'people'
#   inflect.uncountable %w( fish sheep )
inflect.irregular 'LoginCookie', 'LoginCookies'
end

To test, run this from your shell

rake db:test:clone
rake test

From config/user_environment.rb, change the name of your Rails app to your liking

That’s it. Another post will cover how to use persistent login cookies. Bug reports please contact me directly at my email address.

*Update: Here’s the slides I’ve presented at the Singapore Ruby Meeting

4 Comments

have your say

Add your comment below, or trackback from your own site. Subscribe to these comments.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

:

:


«
»