Skip to content

Commit

Permalink
[1.x] Deprecate Authorization helpers in Faraday::Connection (#1306)
Browse files Browse the repository at this point in the history
  • Loading branch information
iMacTia committed Aug 14, 2021
1 parent 83a97b7 commit fa612a0
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Expand Up @@ -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.
Expand Down
26 changes: 21 additions & 5 deletions docs/middleware/request/authentication.md
Expand Up @@ -9,22 +9,38 @@ 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::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

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

`BasicAuthentication` 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.token_auth('authentication-token')
conn.request :token_auth, 'authentication-token', **options
end
```
15 changes: 15 additions & 0 deletions lib/faraday/connection.rb
Expand Up @@ -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 for more usage info.
TEXT
set_authorization_header(:basic_auth, login, pass)
end

Expand All @@ -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 for more usage info.
TEXT
set_authorization_header(:token_auth, token, options)
end

Expand All @@ -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 for more usage info.
TEXT
set_authorization_header(:authorization, type, token)
end

Expand Down

0 comments on commit fa612a0

Please sign in to comment.