How to send Basic Authentication (BA) credentials in ruby on rails

HTTP Basic authentication is a simple authentication scheme in which user authentication is done by a username and password eliminating the needs of cookies, sessions and login pages. It is base64 encoded.

Rails provides a method for this type of authentication: authenticate_with_http_basic

This method can be used as follows:

authenticate_with_http_basic do |username, password|
  ——
end

Inside the block you can access the username and password.

But how to send a request with basic auth in rails ?

This is really tricky and I didn’t find any documentation for this. Here I am describing how to send a request with basic auth.

1. Use Faraday library (Faraday is an HTTP client lib)
https://github.com/lostisland/faraday

Create a connection:

connection = Faraday.new(:url => HOST) do |faraday|
      faraday.request  :url_encoded             # form-encode POST params
      faraday.response :logger                  # log requests to STDOUT
      faraday.adapter  Faraday.default_adapter  # make requests with Net::HTTP
      faraday.basic_auth(USERNAME, PASSWORD)
end

parameters = params[‘user’].permit!.to_h

response = connection.get do |req|
      req.url(params[:url])
      req.headers['Content-Type'] = 'application/json'
      req.params.merge!(parameters)
end

response = connection.post do |req|
        req.url(params[:url])
        req.headers['Content-Type'] = 'application/json'
        req.body = parameters
end

render json: response.body

Here,

HOST = 'http://lvh.me:3002'
USERNAME = 'EdcddzrbmET55016'
PASSWORD = 'UXBJnS309S49st3rHqmH5934'
params['url'] = '/users/profile'

We can use Faraday’s (‘faraday.basic_auth’) basic auth method to reach out our solution.

If you are getting any error like follows in post request

NoMethodError (undefined method `bytesize' for {}:ActiveSupport::HashWithIndifferentAccess):

Don’t forget to convert the ruby hash ‘parameters’ to json by calling ‘to_json’ upon it.

        req.body = parameters.to_json 

In Other way you can use something like this to generate the basic auth header

request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(USERNAME, PASSWORD)

How to use Ruby Rest Client:

require 'rest_client'

headers = {
  :authorization => 'Basic FytxhZGKpbjpvcGVuIHNlc2FtHUHU'
}

response = RestClient.get 'https://yourdomain.com/api/users.json?activityId=02ddf868-6484-440f-8c39-c7d4fb4e7b33', headers
puts response
Advertisements

Install latest PHP version on Mac using homebrew

Check php latest version here: http://php.net/downloads.php
=> Update your homebrew

$ brew update
$ brew upgrade

=> Install a centralized repository for PHP-related brews: homebrew-php
https://github.com/Homebrew/homebrew-php

Requirements
* Homebrew
* Yosemite, El Capitan, and Sierra. Untested everywhere else.

Run the following in your command-line:

$ brew tap homebrew/homebrew-php

$ brew search php

will show you all php formula

We will Install php 7.1, because php 7.1.8 is the latest stable version till now (Aug 2017)

$ brew install php71

Check php version installed

$ php --version
PHP 7.1.8 (cli) (built: Aug  7 2017 15:02:19) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies

You are done.

Rbenv: Start with new ruby and rails versions

Check for the new ruby and rails versions
https://www.ruby-lang.org/en/downloads/
https://rubygems.org/gems/rails/versions

Suppose we are going to install Ruby – 2.4.1 & Rails – 5.1.3

Get rbenv into action

1. $ rbenv install --list # Gets the list of ruby versions available

$ rbenv install 2.4.1

ruby-build: definition not found: 2.4.1

The following versions contain `2.4.1' in the name:
  rbx-2.4.1

See all available versions with `rbenv install --list'.

If the version you need is missing, try upgrading ruby-build:

  brew update && brew upgrade ruby-build

Oops..!

rbenv cannot find the version: 2.4.1

Upgrade ruby-build

Mac:

$ brew upgrade ruby-build --HEAD

Now install ruby 2.4.1

$ rbenv install 2.4.1

Create a new gemset:

$ rbenv gemset create 2.4.1 demo-app
That set up a directory for you in ~/.rbenv/versions/2.4.1/gemsets/demo-app

Set the ruby version to the newest

$ rbenv local 2.4.1

$ rbenv version
=> 2.4.1

    Activate New Gemset


For activating a gemset we need to create a .rbenv-gemsets file in the current directory.

$ touch .rbenv-gemsets
$ echo demo-app > .rbenv-gemsets

Check active gemset:

$ rbenv gemset active

Install Rails 5.1.3

$ gem install rails -v '5.1.3'
$ gem install --no-rdoc --no-ri rails -v '5.1.3' # skips the documentation

Later we can delete this .rbenv-gemsets file and add a new file named ‘.ruby-gemset’ in the rails project directory. I cannot see any other good way of doing this. If anybody know about some other good way of doing this please give a comment.

Create a New Rails app

$ rails new demo-app

$ rm .rbenv-gemsets

$ cd demo-app
$ touch .ruby-gemset
$ echo demo-app > .ruby-gemset
$ touch .ruby-version
$ echo 2.4.1 > .ruby-version
$ rails s
=> Booting Puma
=> Rails 5.1.3 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.9.1 (ruby 2.4.1-p111), codename: Private Caller
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop

Goto http://localhost:3000/

rails-5-new.png

Done! Lets go…

#Ruby Class 2: Why Attribute Accessor?

In class 1 we defined the instance variables and we saw how difficult to accessing it. We managed to access it via calling methods that initializing it.
So we can simplify it by defining access methods.

class Test
 def initialize
  @one = 1
 end 
 def get_one
   @one
 end 
end

The ‘get_one’ method is the reader method for reading the instance variable value.

Ruby has shortcut for this.

class Test
 attr_reader :one
 def initialize
  @one = 1
 end 
 
end

These accessor methods are identical to the methods we wrote by hand earlier.

Sometimes we need to modify the value of these variables from outside. how we do that?

At first we can do this by hand:

class Test
 def initialize
  @one = 1
 end 
 def one=(new_value)
   @one = new_value
 end 
end

The above code allows us to call ‘equal to’ sign on the objects method. This is the setter method. We can set a value to @one like:

t = Test.new
t.one = 3
t.one
=> 3

Ruby also provides a shortcut for this. It is: ‘attr_writer’, so rewrite the above code:

class Test
 attr_writer :one
 def initialize
  @one = 1
 end 
end

Most of the cases we needed ‘attr_reader’ and ‘attr_writer’. So the code becomes:

class Test
 attr_reader :one
 attr_writer :one

 def initialize
  @one = 1
 end 
end

We can again simplify the above code!

class Test
 attr_accessor :one

 def initialize
  @one = 1
 end 
end

How is it? Nice. is n’t it?

#Ruby Class 1: Ruby’s instance variable and initialize method simplified

We read so many documents about ruby but do you think about some facts that we really don’t know. See below.

1. Instance variables are accessible across the class for that instance

Example:

class Test2
 def one
  @one = 1
  @two
 end
 def two
  @two = 2
  @one
 end
end

Here

t = Test.new
=> 
t.one is what ?
t.two is what ?

Can you guess?

t.one is nil why? t.one will return @two, but it is not initialized yet. We have to call t.two for intitializing it (the variable gets a value).

t.two is what?
Ans: 1
Why? because we already initialized @one by calling t.one above. If not it will also return nil.

So what is the purpose of instance variable if we are not getting its value when initializing the Class (say t = Test.new)

Here is the importance of ‘initialize’ method. Lets change the method named ‘two’ to ‘initialize’

class Test3
 def one
  @one = 1
  @two
 end
 def initialize
  @two = 2
  @one
 end
end

then you try:

t = Test.new
=>    ### See here already declared the variable @two

t.one
=> 2

Nice. So we can access that instance variable anywhere in the class.