Class: FaradayMiddleware::OAuth2

Inherits:
Faraday::Middleware
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/faraday_middleware/request/oauth2.rb

Overview

Public: A simple middleware that adds an access token to each request.

The token is added “Authorization” HTTP request header.

Examples

# configure default token:
OAuth2.new(app, 'abc123')

Constant Summary collapse

AUTH_HEADER =

不将 access_token 放在 url query

'Authorization'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(app, token = nil, options = {}) ⇒ OAuth2

Returns a new instance of OAuth2.



27
28
29
30
31
# File 'lib/faraday_middleware/request/oauth2.rb', line 27

def initialize(app, token = nil, options = {})
  super(app)
  options, token = token, nil if token.is_a? Hash
  @token = token && token.to_s
end

Instance Method Details

#call(env) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/faraday_middleware/request/oauth2.rb', line 19

def call(env)
  if !@token.empty?
    env[:request_headers][AUTH_HEADER] ||= "Bearer #{@token}"
  end

  @app.call env
end