Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Faraday deprecation warning #36

Merged
merged 2 commits into from
Nov 2, 2021

Conversation

sambostock
Copy link
Contributor

Goal

This addresses the following deprecation warning:

WARNING: `Faraday::Connection#authorization` is deprecated; it will be removed in version 2.0.
While initializing your connection, use `#request(:authorization, ...)` instead.
See https://lostisland.github.io/faraday/middleware/authentication for more usage info.

Design

We more-or-less use the approach suggested by the deprecation message and documentation.

However, Faraday 2.x will support passing a Basic auth username and password to the middleware, and encoding it automatically, but versions prior to that do not. Therefore, we encode it ourselves for backwards compatibility.

This is the same approach I suggested in octokit/octokit.rb#1359 (comment).

Changeset

The method for specifying the Authorization header has been updated.

Testing

Presumably the existing tests should cover it?

@@ -169,9 +171,9 @@ def agent
http.headers[:user_agent] = configuration.user_agent

if basic_authenticated?
http.basic_auth configuration.email, configuration.password
http.request :authorization, "Basic", Base64.strict_encode64("#{configuration.email}:#{configuration.password}")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell, this is backwards compatible at least as far back as Faraday 0.9.0, and forward compatible with Faraday 2.0.0, where as

http.request :authorization, "Basic", configuration.email, configuration.password

would be compatible with Faraday 2, but not backwards compatible with Faraday < 2.

@luke-belton
Copy link
Member

hey @sambostock - thanks for this, I can see that this silences the deprecation warning. Were you able to test this successfully though? I've tried running it locally with an auth token and get HTTP 401s back from the Bugsnag API unless I make the following change:

http.request :authorization, "Token", configuration.auth_token

to

http.request :authorization, "token", configuration.auth_token

Thanks!

@luke-belton luke-belton added the awaiting feedback Awaiting a response from a customer. Will be automatically closed after approximately 2 weeks. label Sep 10, 2021
@sambostock
Copy link
Contributor Author

Ahh, interesting. I think I was going by Faraday::Request::TokenAuthentication (removed in lostisland/faraday#1308), which used :Token. Thanks for checking that; I've updated it.

@johnkiely1
Copy link
Member

Thanks for the PR @sambostock. We will review this as soon as priorities allow.

@johnkiely1 johnkiely1 added backlog We hope to fix this feature/bug in the future and removed awaiting feedback Awaiting a response from a customer. Will be automatically closed after approximately 2 weeks. labels Sep 24, 2021
@imjoehaines
Copy link
Contributor

Hey @sambostock, thanks for the PR!

Is there a downside to setting the Authorization header ourselves? i.e.:

if basic_authenticated?
  credentials = Base64.strict_encode64("#{configuration.email}:#{configuration.password}")

  http.headers[:Authorization] = "Basic #{credentials}"
elsif token_authenticated?
  http.headers[:Authorization] = "token #{configuration.auth_token}"
end

I've tested that this sends the same header as our tests (see also #37) and it's fully backwards compatible, so it seems like the best option to me

@johnkiely1 johnkiely1 added the awaiting feedback Awaiting a response from a customer. Will be automatically closed after approximately 2 weeks. label Oct 26, 2021
@sambostock
Copy link
Contributor Author

@imjoehaines I can't think of any, other than it meaning we're responsible for more of the header implementation. But at this point, we're half way there already given we have to encode the credentials ourselves.

The code above is also using a similar approach to set headers, so seems fine to me 👍

@imjoehaines
Copy link
Contributor

@imjoehaines I can't think of any, other than it meaning we're responsible for more of the header implementation. But at this point, we're half way there already given we have to encode the credentials ourselves.

The code above is also using a similar approach to set headers, so seems fine to me 👍

Cool, I think if we're encoding the header ourselves anyway then it makes sense to manually set it to avoid the potential BC breaks

I'll look at making that change & pushing a new release next week

This addresses the following deprecation warning:

    WARNING: `Faraday::Connection#authorization` is deprecated; it will be removed in version 2.0.
    While initializing your connection, use `#request(:authorization, ...)` instead.
    See https://lostisland.github.io/faraday/middleware/authentication for more usage info.

To maintain backwards and forward compatibility across Faraday versions,
we simply generate and attach the Authorization header ourselves.
@sambostock
Copy link
Contributor Author

@imjoehaines I've updated this branch to reflect the discussion above 👍

Copy link
Contributor

@imjoehaines imjoehaines left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @sambostock!

@imjoehaines imjoehaines merged commit 462e5a2 into bugsnag:master Nov 2, 2021
@imjoehaines imjoehaines mentioned this pull request Nov 2, 2021
@luke-belton luke-belton added released This feature/bug fix has been released and removed awaiting feedback Awaiting a response from a customer. Will be automatically closed after approximately 2 weeks. backlog We hope to fix this feature/bug in the future labels Nov 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
released This feature/bug fix has been released
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants