Bryan Taylor
Bryan Taylor

Reputation: 31

Faraday TokenAuthentication

I am trying to use pure ruby, active_record, her, and faraday to access a REST API.

Currently I have two methods I am attempting:

▶ cat lib/auth.rb
class MyTokenAuthentication < Faraday::Middleware
  def call(env)
    env[:request_headers]["Authorization: Token"] = RequestStore.store[:my_api_token]
    @app.call(env)
  end
end

and

▶ cat lib/token.rb
class TokenAuth < Faraday::Middleware

def initialize(app, options={})
  @app = app
  @options = options
end


 def call(env)
    binding.pry
    env[:request_headers]["Authorization: Token"] = @options[:token] if @options.include?(:token)
    env[:request_headers]["Accept"] = "application/json\; version=v1"
    @app.call(env)
  end

end

auth.rb corresponds to Faraday::Request::TokenAuthentication

token.rb corresponds to # c.use TokenAuth, :token => "9234bca04b2b9aaceab919770528af13a92447ff"

I just comment out as needed for testing.

require_relative './lib/auth'
require_relative './lib/token'


db_config       = YAML::load(File.open('config/database.yml'))
db_config_admin = db_config.merge({'database' => 'superbot', 'schema_search_path' => 'public'})
ActiveRecord::Base.establish_connection(db_config_admin[ENV['RAILS_ENV']])

RequestStore.store[:my_api_token] = '9234bca04b2b9aaceab919770528af13a92447ff' # or something similar based on `session`

Her::API.setup url: "http://cool:880/api/" do |c|
  # Request
  c.use Faraday::Request::TokenAuthentication,  :token => "9234bca04b2b9aaceab919770528af13a92447ff"
#  c.use TokenAuth, :token => "9234bca04b2b9aaceab919770528af13a92447ff"
  c.use Faraday::Request::UrlEncoded
  c.use Her::Middleware::AcceptJSON
  # Response
  c.use Her::Middleware::DefaultParseJSON

  # Adapter
  c.use Faraday::Adapter::NetHttp
end

When using curl and looking at the headers through tcpdump I get:

curl -X GET http://cool:880/api/ -H 'Authorization: token 9234bca04b2b9aaceab919770528af13a92447ff'

...[...pGET /api/ HTTP/1.1
Host: cool:880
User-Agent: curl/7.43.0
Accept: */*
Authorization: token 9234bca04b2b9aaceab919770528af13a92447ff

Using Own Middleware:

tcpdump:
........GET /api/clusters/2/ HTTP/1.1
User-Agent: Faraday v0.9.2
Authorization: token: 9234bca04b2b9aaceab919770528af13a92447ff
Accept: application/json
Accept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3
Connection: close
Host: 134.6.27.164:880

Pry
    {"User-Agent"=>"Faraday v0.9.2",
     "Authorization: Token"=>
      "9234bca04b2b9aaceab919770528af13a92447ff",
     "Accept"=>"application/json"}

{"detail":"Authentication credentials were not provided."}

Using Faraday::Request::TokenAuthentication

    4: def self.header(token, options = nil)
 => 5:   binding.pry
    6:   options ||= {}
    7:   options[:token] = token
    8:   super(:Token, options)
    9: end

[1] pry(Faraday::Request::TokenAuthentication)> options ||= {}
=> {}
[2] pry(Faraday::Request::TokenAuthentication)> options[:token] = token
=> {:token=>"9234bca04b2b9aaceab919770528af13a92447ff"}
[3] pry(Faraday::Request::TokenAuthentication)> super(:Token, options)
=> "Token token=\"{:token=>\\\"9234bca04b2b9aaceab919770528af13a92447ff\\\"}\""

User-Agent: Faraday v0.9.2
Authorization: Token token="{:token=>\"9234bca04b2b9aaceab919770528af13a92447ff\"}"
Accept: application/json
Accept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3
Connection: close
Host: cool:880

{"detail":"Invalid token."}

I'm pretty confuse...

It seems Faraday::Request::TokenAuthentication is formatting correctly but is concating it wrong..

My middleware seems like it's working but it's adding a colon ':' after token:

Authorization: token: 9234bca04b2b9aaceab919770528af13a92447ff

should be:

Authorization: token 9234bca04b2b9aaceab919770528af13a92447ff

thanks you!

Upvotes: 2

Views: 4020

Answers (2)

eloyesp
eloyesp

Reputation: 3275

I know I'm late to the party, but this code is working now and is far more readable:

connection = Faraday.new url: url do |faraday|
  faraday.authorization :Token, @token
  faraday.adapter Faraday.default_adapter
end

You can replace :Token with :Bearer to make the authorization work with OAuth2.

Upvotes: 1

Bryan Taylor
Bryan Taylor

Reputation: 31

had the totally wrong class should be:

c.use Faraday::Request::Authorization, 'Token', "9234bca04b2b9aaceab919770528af13a92447ff"

Upvotes: 0

Related Questions