法拉第令牌认证

时间:2016-07-28 18:54:07

标签: ruby api authentication orm faraday

我正在尝试使用纯ruby,active_record,her和faraday来访问REST API。

目前我正在尝试两种方法:

▶ 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

▶ 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对应于Faraday :: Request :: TokenAuthentication

token.rb对应于#c.use TokenAuth,:token =&gt; &#34; 9234bca04b2b9aaceab919770528af13a92447ff&#34;

我只是根据测试需要发表评论。

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

当使用curl并通过tcpdump查看标题时,我得到:

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

使用自己的中间件:

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."}

使用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."}

我很混淆......

看起来Faraday :: Request :: TokenAuthentication正确格式化但是错误地结束了..

我的中间件似乎在起作用,但它添加了冒号&#39;:&#39;在令牌之后:

Authorization: token: 9234bca04b2b9aaceab919770528af13a92447ff

应该是:

Authorization: token 9234bca04b2b9aaceab919770528af13a92447ff

谢谢你!

2 个答案:

答案 0 :(得分:0)

完全错误的课程应该是:

c.use Faraday :: Request :: Authorization,&#39; Token&#39;,&#34; 9234bca04b2b9aaceab919770528af13a92447ff&#34;

答案 1 :(得分:0)

我知道我参加聚会迟到了,但是这段代码现在可以正常工作了,而且可读性更强:

c=df.columns.str.contains('A')
df2=df.T.groupby(c).mean().T
df2.columns=['B','A']
df=pd.concat([df.T[c].fillna(df2['A']),df.T[~c].fillna(df2['B'])]).T
print(df)


     A1   A2   A3   B1   B2   B3
aa  1.0  4.0  9.0  2.0  2.0  2.0
bb  9.0  9.0  9.0  9.0  2.0  2.0
cc  3.0  2.0  7.0  1.0  3.0  2.0
dd  4.0  5.0  7.0  4.5  4.0  5.0
ee  5.0  3.0  1.0  5.0  1.0  5.0
ff  3.0  5.0  2.0  5.0  2.0  3.5

您可以将connection = Faraday.new url: url do |faraday| faraday.authorization :Token, @token faraday.adapter Faraday.default_adapter end 替换为:Token,以使OAuth2可以使用授权。