Class: Diesel::Middleware::Auth::OAuth2

Inherits:
Object
  • Object
show all
Defined in:
lib/diesel/middleware/auth/oauth2.rb

Constant Summary collapse

AUTHORIZATION_HEADER =
'Authorization'.freeze
AUTHORIZATION_HEADER_FORMAT =
'Bearer %s'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(app, options) ⇒ OAuth2

Returns a new instance of OAuth2.



9
10
11
12
13
14
# File 'lib/diesel/middleware/auth/oauth2.rb', line 9

def initialize(app, options)
  @app = app
  @id = options[:id]
  @in = options[:in]
  @name = options[:name]
end

Instance Method Details

#call(env) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/diesel/middleware/auth/oauth2.rb', line 16

def call(env)
  context = env[:context]
  auth_options = context.options[@id]
  token = auth_options[:token]
  if @in == :query
    env[:params][@name] = token
  else
    env[:request_headers][AUTHORIZATION_HEADER] = AUTHORIZATION_HEADER_FORMAT % token
  end
  @app.call(env)
end