Skip to content

Commit

Permalink
auto load authorization middlewhere when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
technoweenie committed May 13, 2013
1 parent e35819b commit 3a9e033
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions lib/faraday/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,7 @@ def #{method}(url = nil, body = nil, headers = nil, &block)
#
# Returns nothing.
def basic_auth(login, pass)
headers[Faraday::Request::Authorization::KEY] =
Faraday::Request::BasicAuthentication.header(login, pass)
set_authorization_header(:basic_auth, login, pass)
end

# Public: Sets up the Authorization header with the given token.
Expand All @@ -204,8 +203,7 @@ def basic_auth(login, pass)
#
# Returns nothing.
def token_auth(token, options = nil)
headers[Faraday::Request::Authorization::KEY] =
Faraday::Request::TokenAuthentication.header(token, options)
set_authorization_header(:token_auth, token, options)
end

# Public: Sets up a custom Authorization header.
Expand All @@ -227,8 +225,7 @@ def token_auth(token, options = nil)
#
# Returns nothing.
def authorization(type, token)
headers[Faraday::Request::Authorization::KEY] =
Faraday::Request::Authorization.header(type, token)
set_authorization_header(:authorization, type, token)
end

# Internal: Traverse the middleware stack in search of a
Expand Down Expand Up @@ -395,6 +392,12 @@ def with_uri_credentials(uri)
yield Utils.unescape(uri.user), Utils.unescape(uri.password)
end
end

def set_authorization_header(header_type, *args)
header = Faraday::Request.lookup_middleware(header_type).
header(*args)
headers[Faraday::Request::Authorization::KEY] = header
end
end
end

0 comments on commit 3a9e033

Please sign in to comment.