-1

I have a site which is protected using the http basic authentication.

I wish to code a ruby snippet which allows me to pass the http basic authentication and read the contents of the web page.

I have used a way in java to send the key and other details set in HTTP header and thus read the contents of the web page. But couldn't able to find the same in ruby.

edit:

The code I use is here:

   def express
    http_basic_authenticate_with :name => "dhh", :password => "secret"
   end

which gives me the error:

undefined method `http_basic_authenticate_with' for #<ServiceController:0x7f9bd73e5448>

Thanks in advance.

Community
  • 1
  • 1
sriram
  • 8,562
  • 19
  • 63
  • 82
  • You asked a similar question a while ago? http://stackoverflow.com/questions/14374580/doing-a-http-basic-authentication-in-rails – Nishant Jan 17 '13 at 09:49

3 Answers3

2

Devise does have built inHTTP Basic Authentication yet you do need to have two little things in your app to have it working :

:database_authenticatable strategy in your user/account model config.http_authenticatable = true in the devise initializer

Devise relies on Warden, which is a rack framework for authentication. On its own, Warden tries to provide a custom response to all unauthenticated requests, by something called failure_app, which is a valid rack app.

A simple code that performs http authentication is below:

def http_authenticate
  authenticate_or_request_with_http_digest do |user_name, password|
    user_name == "foo" && password == "bar"
  end
  warden.custom_failure! if performed?
end

There is also a good post on it. Check here.

One more solution(alternative) is to use a Mechanize library

    require 'rubygems'
    require 'mechanize'
    require 'logger'

    agent = WWW::Mechanize.new { |a| a.log = Logger.new("mech.log") }
    agent.user_agent_alias = 'Windows Mozilla'
    agent.auth('username', 'password')
    agent.get('http://example.com') do |page|
      puts page.title
    end
sjain
  • 23,126
  • 28
  • 107
  • 185
1

I think it is what you are looking for:

 class SecretController < ApplicationController
   http_basic_authenticate_with :name => "frodo", :password => "thering"
   def index
     ...
   end
 end

You can also pass an exception for any particular action:

 http_basic_authenticate_with :name => "dhh", :password => "secret", :except => :index

There is also a RailsCasts about it.

EDIT - if your version of rails is prior to 3.1, you can create your own method:

class ApplicationController < ActionController::Base
USER, PASSWORD = 'dhh', 'secret'

before_filter :authentication_check, :except => :index

...

 private
  def authentication_check
   authenticate_or_request_with_http_basic do |user, password|
    user == USER && password == PASSWORD
   end
  end
end 
gabrielhilal
  • 10,660
  • 6
  • 54
  • 81
  • I'm getting an erro like this :`undefined method `http_basic_authenticate_with` – sriram Jan 17 '13 at 09:46
  • Which rails version are you using?? I think this method is only available from Rails 3.1. – gabrielhilal Jan 17 '13 at 09:52
  • if you are running rais 3.2 you should be able to use the `http_basic_authenticate_with :name` method, which must be included in the `controller`. Please include the code in your question, so we can try to help you. – gabrielhilal Jan 17 '13 at 10:22
  • you have included the `http_basic_authenticate_with` inside a method and not the controller. I have edited my answer... also watch the railscasts link for more details. – gabrielhilal Jan 17 '13 at 10:38
0

Use gem such as httparty:

opt = {basic_auth: {username: "user123", password: "pass"}}
response = HTTParty.get("your_site", opt)
Jiří Pospíšil
  • 14,296
  • 2
  • 41
  • 52