From 980709cb2948b90cde4f61c982613e6acdbb8f46 Mon Sep 17 00:00:00 2001 From: Matt Date: Sat, 14 Aug 2021 08:45:32 +0100 Subject: [PATCH 1/6] Deprecate auth helpers in Faraday::Connection --- lib/faraday/connection.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lib/faraday/connection.rb b/lib/faraday/connection.rb index 3379ec5d2..31b4abea3 100644 --- a/lib/faraday/connection.rb +++ b/lib/faraday/connection.rb @@ -297,6 +297,11 @@ def #{method}(url = nil, body = nil, headers = nil, &block) # # @return [void] def basic_auth(login, pass) + warn <<~TEXT + 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 or more usage info. + TEXT set_authorization_header(:basic_auth, login, pass) end @@ -314,6 +319,11 @@ def basic_auth(login, pass) # # @return [void] def token_auth(token, options = nil) + warn <<~TEXT + WARNING: `Faraday::Connection#token_auth` is deprecated; it will be removed in version 2.0. + While initializing your connection, use `#request(:token_auth, ...)` instead. + See https://lostisland.github.io/faraday/middleware/authentication or more usage info. + TEXT set_authorization_header(:token_auth, token, options) end @@ -336,6 +346,11 @@ def token_auth(token, options = nil) # # @return [void] def authorization(type, token) + warn <<~TEXT + 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 or more usage info. + TEXT set_authorization_header(:authorization, type, token) end From a8d81c9818c3cefeb23c7802c717a0030b550518 Mon Sep 17 00:00:00 2001 From: Matt Date: Sat, 14 Aug 2021 09:05:50 +0100 Subject: [PATCH 2/6] Update docs to show correct usage of the authorization middleware --- docs/middleware/request/authentication.md | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/docs/middleware/request/authentication.md b/docs/middleware/request/authentication.md index 867efa187..4239f6941 100644 --- a/docs/middleware/request/authentication.md +++ b/docs/middleware/request/authentication.md @@ -15,16 +15,32 @@ These can be added as middleware manually or through the helper methods. ### Basic Authentication +`TokenAuthentication` adds a 'Basic' type Authorization header to a Faraday request. + ```ruby Faraday.new(...) do |conn| - conn.basic_auth('username', 'password') + conn.request :basic_auth, 'username', 'password' end ``` ### Token Authentication +`TokenAuthentication` adds a 'Token' type Authorization header to a Faraday request. +You can optionally provide a hash of `options` that will be appended to the token. +This is not used anymore in modern web and have been replaced by Bearer tokens. + +```ruby +Faraday.new(...) do |conn| + conn.request :token_auth, 'authentication-token', **options +end +``` + +### Custom Authentication + +The generic `Authorization` middleware allows you to add any other type of Authorization header. + ```ruby Faraday.new(...) do |conn| - conn.token_auth('authentication-token') + conn.request :authorization, 'Bearer', 'authentication-token' end ``` From 64b1c0bd2db11753c44df45bb563a3d4b31692da Mon Sep 17 00:00:00 2001 From: Matt Date: Sat, 14 Aug 2021 09:10:18 +0100 Subject: [PATCH 3/6] Fix Rubocop offenses --- .rubocop_todo.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 3ea14c7ee..f9b7fdd11 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -14,7 +14,7 @@ Metrics/AbcSize: # Offense count: 5 # Configuration parameters: CountComments, CountAsOne. Metrics/ClassLength: - Max: 250 + Max: 256 # Offense count: 15 # Configuration parameters: IgnoredMethods. From 7de5dc82d34e66893ea11dd6486f892a229e8819 Mon Sep 17 00:00:00 2001 From: Matt Date: Sat, 14 Aug 2021 09:37:42 +0100 Subject: [PATCH 4/6] Fix Code Review comments --- docs/middleware/request/authentication.md | 2 +- lib/faraday/connection.rb | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/middleware/request/authentication.md b/docs/middleware/request/authentication.md index 4239f6941..f2041794b 100644 --- a/docs/middleware/request/authentication.md +++ b/docs/middleware/request/authentication.md @@ -15,7 +15,7 @@ These can be added as middleware manually or through the helper methods. ### Basic Authentication -`TokenAuthentication` adds a 'Basic' type Authorization header to a Faraday request. +`BasicAuthentication` adds a 'Basic' type Authorization header to a Faraday request. ```ruby Faraday.new(...) do |conn| diff --git a/lib/faraday/connection.rb b/lib/faraday/connection.rb index 31b4abea3..6344f6a0c 100644 --- a/lib/faraday/connection.rb +++ b/lib/faraday/connection.rb @@ -300,7 +300,7 @@ def basic_auth(login, pass) warn <<~TEXT 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 or more usage info. + See https://lostisland.github.io/faraday/middleware/authentication for more usage info. TEXT set_authorization_header(:basic_auth, login, pass) end @@ -322,7 +322,7 @@ def token_auth(token, options = nil) warn <<~TEXT WARNING: `Faraday::Connection#token_auth` is deprecated; it will be removed in version 2.0. While initializing your connection, use `#request(:token_auth, ...)` instead. - See https://lostisland.github.io/faraday/middleware/authentication or more usage info. + See https://lostisland.github.io/faraday/middleware/authentication for more usage info. TEXT set_authorization_header(:token_auth, token, options) end @@ -349,7 +349,7 @@ def authorization(type, token) warn <<~TEXT 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 or more usage info. + See https://lostisland.github.io/faraday/middleware/authentication for more usage info. TEXT set_authorization_header(:authorization, type, token) end From 0ee20e03b049658ac9f6af9cc89a8136563b8a08 Mon Sep 17 00:00:00 2001 From: Matt Date: Sat, 14 Aug 2021 09:44:05 +0100 Subject: [PATCH 5/6] Reorganise authentication.md putting suggested implementation on top --- docs/middleware/request/authentication.md | 26 +++++++++++------------ 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/docs/middleware/request/authentication.md b/docs/middleware/request/authentication.md index f2041794b..d42333da5 100644 --- a/docs/middleware/request/authentication.md +++ b/docs/middleware/request/authentication.md @@ -9,9 +9,19 @@ top_name: Back to Middleware top_link: ./list --- -Basic and Token authentication are handled by Faraday::Request::BasicAuthentication -and Faraday::Request::TokenAuthentication respectively. -These can be added as middleware manually or through the helper methods. +The `Faraday::Request::Authentication` middleware allows you to automatically add an `Authorization` header +to your requests. It also feature 2 specialised sub-classes that provide useful extra features for Basic Authentication +and Token Authentication requests. + +### Any Authentication + +The generic `Authorization` middleware allows you to add any type of Authorization header. + +```ruby +Faraday.new(...) do |conn| + conn.request :authorization, 'Bearer', 'authentication-token' +end +``` ### Basic Authentication @@ -34,13 +44,3 @@ Faraday.new(...) do |conn| conn.request :token_auth, 'authentication-token', **options end ``` - -### Custom Authentication - -The generic `Authorization` middleware allows you to add any other type of Authorization header. - -```ruby -Faraday.new(...) do |conn| - conn.request :authorization, 'Bearer', 'authentication-token' -end -``` From 2b296f05a0c32a693d82e3bfecaf24cddbfc4835 Mon Sep 17 00:00:00 2001 From: Matt Date: Sat, 14 Aug 2021 09:50:59 +0100 Subject: [PATCH 6/6] Tiny fixes --- docs/middleware/request/authentication.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/middleware/request/authentication.md b/docs/middleware/request/authentication.md index d42333da5..3342f1fb5 100644 --- a/docs/middleware/request/authentication.md +++ b/docs/middleware/request/authentication.md @@ -9,8 +9,8 @@ top_name: Back to Middleware top_link: ./list --- -The `Faraday::Request::Authentication` middleware allows you to automatically add an `Authorization` header -to your requests. It also feature 2 specialised sub-classes that provide useful extra features for Basic Authentication +The `Faraday::Request::Authorization` middleware allows you to automatically add an `Authorization` header +to your requests. It also features 2 specialised sub-classes that provide useful extra features for Basic Authentication and Token Authentication requests. ### Any Authentication