Screencast - RestfulAuthentication - 1
Restful Authentication: With an empty project
(Screencasts at the bottom of the page!!)
- Try out the plugin in a plain “Hello, World!” app before you adapt your own code. So . . .
rails _2.1.1_ ra
(the “_2.1.1_” is to force the use of Rails 2.1.1, since I now have Rails 2.2.2 on my system.)
- Create a test “welcome” controller and edit the index template so we can see something; also remove public/index.html
cd ra script/generate controller welcome index rm public/index.html
(and edit app/views/welcome/index.html.erb)
- Try out your app: script/server, and then browse to http://localhost:3000/welcome — If you like, change the routing so that the default route points to this controller/action.
-
Now you need to install the plugin. Two ways:
- Use script/plugin:
script/plugin install git://github.com/technoweenie/restful-authentication.git
If you get the error message “Plugin not found” AFTER some obvious status messages suggesting that stuff is getting installed, and if you do get files created in vendor/plugins/restful-authentication, ignore that error message! If figure out out to avoid the error message, let me know!
- Download the plugin as a ZIP from here:
http://github.com/technoweenie/restful-authentication/tree/master
Now unzip the ZIP download and put a copy in vendor/plugins in your app.
- Use script/plugin:
-
Ensure that the plugin name is restful_authentication
mv vendor/plugins/restful-authentication vendor/plugins/restful_authenticationOnce you have done this, there should be a directory restful_authentication in vendor/plugins, and that directory should have files such as CHANGELOG, TODO, and directories such as lib/, generators/, etc.
-
So that we can set members into “states” (e.g., “suspended”), we are also going to use a new gem called aasm. To get this:
gem sources -a http://gems.github.com gem install rubyist-aasm
-
Additionally, we will need a plugin so that we can use GMail has our outgoing mail server. The plugin we are going to use tweaks SMTP to support TLS (HTTPS security). Here’s the command:
script/plugin install http://code.openrain.com/rails/action_mailer_tls/
- From here on out, the “last word” is the installation instructions on the restful_authentication page. Note that many of the details here are taken directly from that page (for instance, the directive to make sure that the plugin directory is called restful_authentication with an underscore).
-
Make sure you are in the root of your directory, and do:
script/generate authenticated User Session --aasm
-
Review the output carefully! You will see some instructions for adding lines to your config/routes.rb file. Some of these lines will be added for you. In my cases, after adding the appropriate lines, the start of config/routes.rb looks like this:
map.logout '/logout', :controller => 'sessions', :action => 'destroy' map.login '/login', :controller => 'sessions', :action => 'new' map.register '/register', :controller => 'users', :action => 'create' map.signup '/signup', :controller => 'users', :action => 'new' map.activate '/activate/:activation_code', :controller => 'users', :action => 'activate', :activation_code => nil map.resources :users, :member => { :suspend => :put, :unsuspend => :put, :purge => :delete } map.resource :session -
Inside the “Rails::Initializer.run do |config|” block in config/environment.rb add
config.active_record.observers = :user_observer config.gem 'rubyist-aasm', :lib => 'aasm', :source => 'http://gems.github.com'
-
Also, we will use GMail for our outgoing e-mail. Add this in the same place:
config.action_mailer.smtp_settings = { :address => 'smtp.gmail.com', :port => 587, :authentication => :plain, :user_name => 'john.g.norman', :password => 'PASSWORD' } -
In config/environments/development.rb, make sure the e-mail setting are like so:
config.action_mailer.raise_delivery_errors = true config.action_mailer.delivery_method = :smtp
-
You should now be able to do
rake db:migrate
-
Review the code that has been created for you - in particular, study the controllers/ and models/ directory. When it comes time to adapt an existing application, we will be refactoring our code so that it will play nice with the generated controller and model for users.
It is hard to underestimate how important it is to go through these files. For example, if you review controllers/session_controller.rb, you will see this comment and line:
# Be sure to include AuthenticationSystem in Application Controller instead include AuthenticatedSystem
(Yes, for the sharp-eyed, it is true that the comment says one thing and the include statement says something else; the include is correct)
- Indeed. The authentication system will not be triggered unless you do this! So comment-out the “include” line in SessionController and move the line to ApplicationController (so all of your controllers will get the plugin).
-
Now, since it says “include AuthenticationSystem” — where does that come from? Look in lib/authenticated_system.rb
This provides many useful methods to your controllers and views. Review them! They are incredibly useful!
Notice the comments before login_required — this tells you how to force a login before actions. So we want to add:
before_filter :login_required
to our welcome controller:
class WelcomeController < ApplicationController before_filter :login_required def index end end -
Now look at models/user_mailer.rb
Where you see YOURSITE replace with localhost:3000
Replace ADMINEMAIL with your address, e.g., john@7fff.com
-
Restart your server, and browse to http://localhost:3000/welcome
Now we are redirected to login! Great. But we need a link to the registration page. Let’s add that. To views/session/new.html.erb add at the bottom:
<%= link_to 'Sign up', :signup %>
-
Try it again . . . you should be able to go through the whole
signup/login process
Screencast, Part I
Screencast, Part II
blog comments powered by Disqus
Add New Comment
Viewing 27 Comments
Thanks. Your comment is awaiting approval by a moderator.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Net::SMTPSyntaxError in UsersController#create
501 Syntax error in parameters or arguments
RAILS_ROOT: c:/development/class-ruby/sandbox/test-rest-auth
Application Trace | Framework Trace | Full Trace
c:/ruby/lib/ruby/1.8/net/smtp.rb:680:in `check_response'
c:/ruby/lib/ruby/1.8/net/smtp.rb:653:in `getok'
c:/ruby/lib/ruby/1.8/net/smtp.rb:623:in `helo'
vendor/plugins/action_mailer_tls/lib/smtp_tls.rb:42:in `do_helo'
vendor/plugins/action_mailer_tls/lib/smtp_tls.rb:15:in `do_start'
c:/ruby/lib/ruby/1.8/net/smtp.rb:378:in `start'
c:/ruby/lib/ruby/1.8/net/smtp.rb:316:in `start'
c:/ruby/lib/ruby/gems/1.8/gems/actionmailer-2.1.1/lib/action_mailer/base.rb:628:in `perform_delivery_smtp'
c:/ruby/lib/ruby/gems/1.8/gems/actionmailer-2.1.1/lib/action_mailer/base.rb:508:in `__send__'
c:/ruby/lib/ruby/gems/1.8/gems/actionmailer-2.1.1/lib/action_mailer/base.rb:508:in `deliver!'
c:/ruby/lib/ruby/gems/1.8/gems/actionmailer-2.1.1/lib/action_mailer/base.rb:383:in `method_missing'
app/models/user_observer.rb:3:in `after_create'
app/controllers/users_controller.rb:18:in `create'
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
config.action_mailer.smtp_settings = {
:address => 'smtp.gmail.com',
:port => 587,
:authentication => :plain,
:user_name => 'GMAILUSERNAME',
:password => 'GMAILPASSWORD'
}
Additonally, for GMail, you need to support TLS security. To do this, you want to use the right plugin:
script/plugin install http://code.openrain.com/rails/action_mailer_tls/
If you are NOT using GMail, note that the standard (non-secured) port for SMTP is: 25. I.e., instead of 587 above. Also, if you're using regular SMTP (not GMail), look again in AWDR.
Do you already have an account? Log in and claim this comment.
require 'action_mailer'
require 'vendor\plugins\action_mailer_tls\lib\smtp_tls'
class MyMailer < ActionMailer::Base
def message()
from 'viewsnortheast@gmail.com'
recipients 'viewsnortheast@gmail.com'
subject 'this is a test message'
body 'this is the body'
end
end
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => 'smtp.gmail.com',
:port => 587,
:authentication => :plain,
:user_name => MYEMAIL,
:password => MYPASSWORD
}
puts MyMailer.create_message()
MyMailer.deliver_message()
Do you already have an account? Log in and claim this comment.
Could you paste in the exception trace for the sample program?
Incidentally, you are showing something that works but that I couldn't find in the documentation: Where did you read that you should sent a String to body? Docs always use a Hash and a template.
Do you already have an account? Log in and claim this comment.
Here's the output (I added require 'rubygems' at the start):
$ ruby mytestmail.rb
From: viewsnortheast@gmail.com
To: viewsnortheast@gmail.com
Subject: this is a test message
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
this is the body
c:/ruby/lib/ruby/1.8/net/smtp.rb:680:in `check_response': 501 Syntax error in parameters or arguments (Net::SMTPSyntaxEr
ror)
from c:/ruby/lib/ruby/1.8/net/smtp.rb:653:in `getok'
from c:/ruby/lib/ruby/1.8/net/smtp.rb:623:in `helo'
from ./vendor\plugins\action_mailer_tls\lib\smtp_tls.rb:42:in `do_helo'
from ./vendor\plugins\action_mailer_tls\lib\smtp_tls.rb:15:in `do_start'
from c:/ruby/lib/ruby/1.8/net/smtp.rb:378:in `start'
from c:/ruby/lib/ruby/1.8/net/smtp.rb:316:in `start'
from c:/ruby/lib/ruby/gems/1.8/gems/actionmailer-2.1.1/lib/action_mailer/base.rb:628:in `perform_delivery_smtp'
from c:/ruby/lib/ruby/gems/1.8/gems/actionmailer-2.1.1/lib/action_mailer/base.rb:508:in `__send__'
from c:/ruby/lib/ruby/gems/1.8/gems/actionmailer-2.1.1/lib/action_mailer/base.rb:508:in `deliver!'
from c:/ruby/lib/ruby/gems/1.8/gems/actionmailer-2.1.1/lib/action_mailer/base.rb:383:in `method_missing'
from mytestmail.rb:32
For using string as the body, I found this tutorial: http://rubybook.ca/2008/07/13/gmail/
Do you already have an account? Log in and claim this comment.
I sent you an e-mail earlier -- check again for another msg.
Do you already have an account? Log in and claim this comment.
:tls => 'true',
:domain => 'gmail.com',
Do you already have an account? Log in and claim this comment.
C:/Ruby/lib/ruby/1.8/net/smtp.rb:576:in `auth_plain'
C:/Ruby/lib/ruby/1.8/net/smtp.rb:570:in `__send__'
C:/Ruby/lib/ruby/1.8/net/smtp.rb:570:in `authenticate'
C:/Ruby/lib/ruby/1.8/net/smtp.rb:410:in `do_start'
C:/Ruby/lib/ruby/1.8/net/smtp.rb:377:in `start'
C:/Ruby/lib/ruby/1.8/net/smtp.rb:315:in `start'
C:/Ruby/lib/ruby/gems/1.8/gems/actionmailer-2.1.1/lib/action_mailer/base.rb:628:in `perform_delivery_smtp'
C:/Ruby/lib/ruby/gems/1.8/gems/actionmailer-2.1.1/lib/action_mailer/base.rb:508:in `__send__'
C:/Ruby/lib/ruby/gems/1.8/gems/actionmailer-2.1.1/lib/action_mailer/base.rb:508:in `deliver!'
C:/Ruby/lib/ruby/gems/1.8/gems/actionmailer-2.1.1/lib/action_mailer/base.rb:383:in `method_missing'
app/models/user_observer.rb:3:in `after_create'
app/controllers/users_controller.rb:18:in `create'
Code from my environment.rb file:
Rails::Initializer.run do |config|
config.active_record.observers = :user_observer
config.gem 'rubyist-aasm', :lib => 'aasm', :source => 'http://gems.github.com'
config.action_mailer.smtp_settings = {
:address => 'smtp.gmail.com',
:port => 587,
:authentication => :plain,
:user_name => 'myemail',
:password => 'mypassword',
:tls => 'true',
:domain => 'gmail.com'
}
I've tried to reinstall action_mailer_tls but it looks like the website isn't up -- is there another place to get it?
Do you already have an account? Log in and claim this comment.
The error message indicates that you weren't able to establish an SSL connection with gmail.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Mike
Do you already have an account? Log in and claim this comment.
Coming: Another screencast that shows how to get at your RESTful services with authentication -- and how to refactor a bit of your app into RESTful form.
Do you already have an account? Log in and claim this comment.
I had looked at a few and they each were dated and broken.
I'm looking forward to seeing the next screencast... hoping you could show how to best use the authentication with RESTful clients. In my case, I'm looking to implement "basic authentication" from a service running on a PC. It needs to login and report usage information. I'm using Net::HTTP::Post, and I have it working, but probably could be improved to be more 'Best Practices' like.
Mike
Do you already have an account? Log in and claim this comment.
I am not sure what the relationship between OPEN ID and the restful authentication.
If it is that complicated, I probably would not choose this feature in the final project.
By the way, since I can reuse the same email address when testing, is it safe just to go into the database and
change the email address of existing user, and hence, when I retry creating a different user, I can reuse my email address.
Do you already have an account? Log in and claim this comment.
1. restful_auth gets you more than just the ability to login: It provides for securing your REST data so that unathorized users can't use your service. Open id alone would not get you that.
2. All open id is about is having your credentials clearing happen on another server. By this means, you enter an open id into a site that supports it, and then the check for your credentials jumps to your open id provider. If you check out, then it jumps back to your app. So, in a lot of ways, open id is a whole different thing. Since maybe users don't have open id's, it is a terrible idea to support only open id.
3. You can blend in open id to restful_authentication. I believe there is a screen on this at railscasts.com -- i hcw no idea if the procedure outlined there would work with what I've described in my screencast.
4. There *is* a lot of magic implementing restful_auth. That is why I provide the screencast. It's hard. On the other hand, authentication is also hard (actually really hard to get right -- cookie handling is just the tip of the iceberg).
5. I actually think the restful_authentication plugin is pretty awful. Please don't quite me. There are things coming along that will be better.
Do you already have an account? Log in and claim this comment.
worked better for me
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
script/generate authenticated User Session --aasm
worked.
So the other download I had used may indeed not be a recent enough version.
Do you already have an account? Log in and claim this comment.
But, really, all bets are off unless you use the versions above. To do it with Rails 2.2 should be pretty easy, but I think a stumbling block would be researching whether or not you need a plugin for special TLS handling. Also, either way (Rails 2.2 TLS or using a special plugin) it is essential that you use the right port in your mail config settings as given in the screencast).
Add New Comment