Class: Rack::OAuth2Utils::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/rack-oauth2_utils/middleware.rb

Constant Summary collapse

INVALID_HEADERS_AND_BODY =
[{'Content-Type' => 'text/plain'}, ['The access token is invalid.']].freeze

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}, &resolver) ⇒ Middleware

Returns a new instance of Middleware.



8
9
10
11
12
13
14
# File 'lib/rack-oauth2_utils/middleware.rb', line 8

def initialize(app, options = {}, &resolver)
  @app = app
  @realm = options[:realm]
  @logger = options[:logger]
  @resolver = resolver
  @invalid_token_response = options[:invalid_token_response] || INVALID_HEADERS_AND_BODY.dup
end

Instance Method Details

#call(env) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rack-oauth2_utils/middleware.rb', line 16

def call(env)
  request = OAuthRequest.new(env)
  logger = @logger || env["rack.logger"]
  
  # If not oauth header / param, leave it up to the app.
  return @app.call(env) unless request.oauth?
  
  # Fetch identity
  if identity = @resolver.call(request.access_token) # identity found, forward to backend
    env["oauth.identity"] = identity
    logger.info "RO2U: Authorized #{identity}" if logger
  else # invalid token
    logger.info "RO2U: Invalid token" if logger
    return unauthorized(request)
  end
  @app.call(env)
end