views:

66

answers:

2

Could someone please explain for me what is happening here?

i feel like the documentation doesnt mention a lot or describe what is happening. it just say use this method like this.

what will happen if username and password are true, what will happen if false etc?

class AdminController < ApplicationController
  USERNAME, PASSWORD = "humbaba", "5baa61e4"

  before_filter :authenticate

  private

  def authenticate
    authenticate_or_request_with_http_basic do |username, password|
      username == USERNAME &&
      Digest::SHA1.hexdigest(password) == PASSWORD
    end
  end
end

thanks

+1  A: 

The before_filter method ensures that the private method authenticate is run before all requests.

authenticate_or_request_with_http_basic pops up the browser's "enter your username and password" box, and passes them into the block, as username and password, in this case.

If the block returns true (if the username and password match), the request proceeds to your more specific code. If the block returns false (the username and password don't match), the request is cut short, and an authentication failure page with the correct HTTP status code is returned. The browser may retry the request a few more times before showing the failure page.

Matchu
"pops up the browser's "enter your username and password" box". i dont quite understand this. it creates a box that the user type in the info? where and how? and if it return false, which page will be displayed? there is so little info about the details :/
never_had_a_name
There is a [standard protocol](http://en.wikipedia.org/wiki/Basic_access_authentication) for authentication over HTTP. The browser will pop up its own username and password box above the web page. I'm not sure what content Rails returns for the authentication failure page, but it's probably something very generic. Have you tried running this code yet to see it in action? That's the best way to understand.
Matchu
[Here's a demo to show you what the login interface will be like.](http://www.pagetutor.com/keeper/http_authentication/index.html)You probably shouldn't use this for public web applications, though, as the average user isn't exactly used to it.
Matchu
thanks now i understand better. but i entered wrong a lot of times but it kept asking. so it didnt redirect.
never_had_a_name
@fayer - whether or not it ever shows that page or simply keeps retrying is the browser's choice :)
Matchu
+1  A: 

There is standard authentication functionality built into every browser called "Basic HTTP Authentication". I'm sure you've seen a generic username/password dialog (styled as part of your operating system) show up on web pages. This is it.

It works as follows:

  • Browser sends GET request for a protected URL
  • Server sends 401 Response which means "Authorization Required"
  • Browser knows what it means and pops up a dialog box to the user with user/pass fields
  • When user submits, browser sends another GET request, but with Authorization header which contains base64 encoded username and password
  • Server checks, and if successful — sends back 200 success response with the content of requested page

In your before_filter you're simply telling Rails to perform all of the above song-and-dance when any controller action is accessed anywhere. Rails handles all the protocol communication described above for you.

In case of denied access, Rails sends back 403 Forbidden response, and browser has built-in way to show that.

To find out more: http://en.wikipedia.org/wiki/Basic_access_authentication

hakunin
great explanation! thanks!
never_had_a_name