Class: Tito::OAuth2Middleware

Inherits:
FaradayMiddleware::OAuth2
  • Object
show all
Defined in:
lib/tito/oauth2_middleware.rb

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of OAuth2Middleware.

Raises:

  • (ArgumentError)


27
28
29
30
31
32
# File 'lib/tito/oauth2_middleware.rb', line 27

def initialize(app, token = nil, options = {})
  super(app)
  options, token = token, nil if token.is_a? Hash
  @token = token
  raise ArgumentError, ":param_name can't be blank" if @param_name.empty?
end

Instance Method Details

#call(env) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/tito/oauth2_middleware.rb', line 3

def call(env)
  params = query_params(env[:url])

  token = params.delete(:api_key) || params.delete('api_key')
  token ||= if @token.is_a?(Proc)
    @token.call(env)
  else
    @token
  end

  token ||= Tito.api_key

  if token.respond_to?(:empty?) && !token.empty?
    env[:url].query = build_query params
    if token.length > 30
      env[:request_headers][AUTH_HEADER] ||= %(Bearer #{token})
    else
      env[:request_headers][AUTH_HEADER] ||= %(Token token="#{token}")
    end
  end

  @app.call env
end