Saturday, December 31, 2011

HTTP basic authentication using email address

In my previous article, I have written about HTTP basic authentication in rails using a plain user access YML.

Last few days ago, I have modified the logic to log-in using email address and password. So that anyone don't need to remember another new password. Hope it might help you...

Written a news private method
def login_by_email(user_name, password)
    is_logged_in = false
    if (user_name.match(/.+\b@yourdomain.com$\b/i))
      require 'net/imap'
      require 'openssl'
      client = Net::IMAP.new(host, port, true, nil, false)
      begin
        client.login(user_name, password)
        client.logout
        is_logged_in = true
      rescue Exception => error
        logger.error "Unable to log-in :: #{error.message}"
      end
      client.disconnect
    end
    return is_logged_in
  end
On line#3 I have checked the domain name to provide access for a specific domain. After that modified the authentication method following way:
def authenticate_for_staging_server
    authenticate_or_request_with_http_basic do |user_name, password|
      login_by_email(user_name, password) == true
    end
  end

No comments:

Post a Comment