Faraday: 传递带有嵌入式基本身份验证的 URL 已损坏

创建于 2021-09-01  ·  5评论  ·  资料来源: lostisland/faraday

最近的身份验证中间件重构破坏了在 URI 的用户信息部分传递带有嵌入基本身份验证的 URL,因为 basic_auth 仍在url_prefix= https://github.com/lostisland/faraday/blob/0f9626c48d0daa24888cb4e5e7962c106a48d 中使用lib/法拉第/connection.rb#L364 -L367

~/src/github.com/lostisland/faraday (1.x) $ ruby -Ilib -rfaraday -rfaraday/net_http -rjson -e "puts JSON.load(Faraday.new(url: 'https://user:[email protected]/headers').get.body)['headers']['Authorization']"
WARNING: `Faraday::Connection#basic_auth` is deprecated; it will be removed in version 2.0.
While initializing your connection, use `#request(:basic_auth, ...)` instead.
See https://lostisland.github.io/faraday/middleware/authentication for more usage info.
Basic dXNlcjpwYXNzd29yZA==
~/src/github.com/lostisland/faraday (main) $ ruby -Ilib -rfaraday -rfaraday/net_http -rjson -e "puts JSON.load(Faraday.new(url: 'https://user:[email protected]/headers').get.body)['headers']['Authorization']"
lib/faraday/connection.rb:365:in `block in url_prefix=': undefined method `basic_auth' for #<Faraday::Connection:0x00007fed250abaa8 @parallel_manager=nil, @headers={}, @params={}, @options=#<Faraday::RequestOptions (empty)>, @ssl=#<Faraday::SSLOptions (empty)>, @default_parallel_manager=nil, @manual_proxy=nil, @builder=#<Faraday::RackBuilder:0x00007fed25092210 @adapter=Faraday::Adapter::NetHttp, @handlers=[Faraday::Request::UrlEncoded]>, @url_prefix=#<URI::HTTPS https://user:[email protected]/headers>> (NoMethodError)
    from lib/faraday/connection.rb:506:in `with_uri_credentials'
    from lib/faraday/connection.rb:364:in `url_prefix='
    from lib/faraday/connection.rb:84:in `initialize'
    from lib/faraday.rb:96:in `new'
    from lib/faraday.rb:96:in `new'
    from -e:1:in `<main>'

可以在此处获得证明这一点的测试: https :

it 'uses User Information from the URI for Basic authentication' do
  conn.url_prefix = 'http://user:[email protected]'
  expect(conn.url_prefix.to_s).to eq('http://sushi.com/')
  request = conn.build_request(:get)
  expect(request.headers['Authorization']).to eq("Basic #{Base64.strict_encode64('user:password')}")
end

我们应该决定是要修复此问题还是删除对此功能的支持。

https://github.com/lostisland/faraday/issues/1308#issuecomment -909109525_ 中查看@etiennebarrie 的评论

bug unconfirmed

最有用的评论

嗨,这是另一个用例:elastic/elasticsearch-ruby#1479

所有5条评论

@etiennebarrie我从评论中引用了你的想法:

虽然我不反对在 Connection 上弃用 basic_auth 和授权,但我认为能够在 URL 中嵌入基本身份验证真的很有用(例如,它可以在机密中进行配置)。

我正在认真考虑取消对此的支持。
似乎这种做法在 2005 年就被

我也很好奇你上面的例子:

例如,它可以在秘密中配置
我不明白如何使用basic_auth中间件不允许使用机密?
不会像这样的工作吗?

# Faraday 1.x
conn = Faraday.new(url_with_no_userinfo) do |f|
  conn.request :basic_auth, Secrets.basic_auth_user, Secrets.basic_auth_pass
  ...
end

我们基本上是这样做的:

def connection
  Faraday.new(url: server) do
    # other config
  end
end

def server
  if global?
    secrets.global
  elsif something?
    secrets.other
  elsif something_else?
    secrets.another
  end
end

所以我们只需要每个 URL/用户/密码一个秘密。


我们可以将每个秘密完全拆分为三个,甚至可以保留单个 URL 秘密,但在将其传递给法拉第之前提取用户密码。

是的,这就是我的想法,感谢您确认@etiennebarrie 👍

嗨,这是另一个用例:elastic/elasticsearch-ruby#1479

感谢@tagliala的输入,最终我们希望取悦社区,因此了解此功能的使用量以决定其未来非常重要。

这显然是一个广泛使用的图书馆,非常感谢您指出

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

jeffb-stell picture jeffb-stell  ·  5评论

mvastola picture mvastola  ·  4评论

jedeleh picture jedeleh  ·  3评论

t3hk0d3 picture t3hk0d3  ·  3评论

Lewiscowles1986 picture Lewiscowles1986  ·  4评论