Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9194025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:18:20+00:00 2026-06-17T21:18:20+00:00

I currently have a before_filter in my ApplicationController which is used for credential verification.

  • 0

I currently have a before_filter in my ApplicationController which is used for credential verification. I also want to be able to verify user credentials when I access the url directly (i.e., from a script and not from a browser login form/session). Are there issues with accepting user credentials via a GET request?

application_controller.rb

before_filter :login_required

def login_required
    reset_session if session[:last_seen] < Rails.configuration.admin_timeout.minutes.ago rescue nil
    return true if session[:user]
    if (!params[:login].nil? && !params[:password].nil?) && session[:user] = User.authenticate(params[:login], params[:password])
        return true
    else
        render :nothing => true, :status => 401
        return false
    end
    flash[:alert] = 'Please login to continue'
    redirect_to login_url and return false
end

If it’s not an acceptable practice I’d like to only be able to do this on certain ‘safe’ controller actions. So for example:

product_controller.rb

before_filter :do_something
permit_login_via_get :only => [:index]

def index
    # Do some stuff in here. This should be accessible via http://mydomain.com/products?login=admin&password=yeahlikeidtellyouthat
end

So now my login_requred function would need to be modified so that the ‘permit_login_via_get’ method works with it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:18:21+00:00Added an answer on June 17, 2026 at 9:18 pm

    Ok so I gave it some thought and decided it would be best to create API keys for each of my applications and with the Net::HTTP request, set a custom header which contained the API key. That way there are no user credentials that are sent through the URL. Now as to what is ‘secure’ or not about this method or what I need to do to MAKE it secure is something else. But here’s how I’ve done it so far and it seems to be working:


    application.rb (Manager application)

    # API Keys
    config.api_key = "somelongstringofcharacters"
    config.pos_api_key = "anotherlongstringofcharacters"
    config.website_api_key = "yetANOTHERlongstringofcharacters"
    

    application.rb in the POS and Website apps are likewise to the above example.


    application_controller.rb (all applications)

    before_filter :login_required
    
    def login_required
        reset_session if session[:last_seen] < Rails.configuration.admin_timeout.minutes.ago rescue nil
        return true if session[:user]
            unless request.headers["X-API-Key"].nil?
            if request.headers["X-API-Key"] == Rails.configuration.api_key
                return true
            else
                render :nothing => true, :status => 401
                return false
            end
            end
        flash[:alert] = 'Please login to continue'
        redirect_to :controller => :users, :action => :login
        return false
    end
    

    This way all methods are private (except those that have skip_before_filter :login_required) and are accessible if you are logged in over a session using a login/password combination through the use of a form, OR if the request you’ve made has a “X-API-Key” header with the correct application key.

    The following is an example Rake task which is run through the Manager app; it requests a JSON file from the POS server which is protected by the login_required method:

    desc "Test Task so that larger projects don't need to be run through"
    task :testing => :environment do
    
        require "net/http"
        require "uri"
    
        url = URI.parse("http://myposdomain/items.json?all=true&category=Wines")
        req = Net::HTTP::Get.new(url.path)
        req.add_field("X-API-Key", Rails.configuration.pos_api_key)
        res = Net::HTTP.new(url.host, url.port).start do |http|
        http.request(req)
        end
        myposdomain_wines = res.body
    
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a check_user_access_control before_filter in my ApplicationController that checks the logged user's roles
I currently have code in my ApplicationController to check if a user is logged
I currently have a table of concentrations, which are linked to a table of
I currently have a form with inputs and name attributes. I'm able to get
I have implemented Devise confirmable which is working properly. When a user signs up
I am currently working on a Rails 3.2 application in which I have two
I have a before_filter in my application controller to keep a user's session alive
I have two models, user and treating (which you can think of as a
I'd like to have additional attributes for my User model and don't want to
I have a bunch of different controllers, and I want to be able to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.