Class: OAuth2::Provider::Rack::ResourceRequest

Inherits:
Rack::Request
  • Object
show all
Defined in:
lib/oauth2/provider/rack/resource_request.rb

Instance Method Summary collapse

Instance Method Details

#authenticate_request!(options, &block) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/oauth2/provider/rack/resource_request.rb', line 31

def authenticate_request!(options, &block)
  if authenticated?
    if options[:scope].nil? || has_scope?(options[:scope])
      yield
    else
      insufficient_scope!
    end
  else
    authentication_required!
  end
end

#authenticated?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/oauth2/provider/rack/resource_request.rb', line 48

def authenticated?
  authorization.present?
end

#authentication_required!(reason = nil) ⇒ Object



56
57
58
59
# File 'lib/oauth2/provider/rack/resource_request.rb', line 56

def authentication_required!(reason = nil)
  env['warden'] && env['warden'].custom_failure!
  throw_response Responses.unauthorized(reason)
end

#authorizationObject



43
44
45
46
# File 'lib/oauth2/provider/rack/resource_request.rb', line 43

def authorization
  validate_token!
  @authorization
end

#authorization_keyObject



25
26
27
28
29
# File 'lib/oauth2/provider/rack/resource_request.rb', line 25

def authorization_key
  @authorization_key ||= Rack::Auth::AbstractRequest::AUTHORIZATION_KEYS.detect do |key|
    @env.has_key?(key)
  end
end

#block_bad_requestObject



73
74
75
76
77
# File 'lib/oauth2/provider/rack/resource_request.rb', line 73

def block_bad_request
  if token_from_param && token_from_header && (token_from_param != token_from_header)
    throw_response Responses.json_error('invalid_request', :description => 'both authorization header and oauth_token provided, with conflicting tokens')
  end
end

#block_invalid_tokenObject



79
80
81
82
83
# File 'lib/oauth2/provider/rack/resource_request.rb', line 79

def block_invalid_token
  access_token = OAuth2::Provider.access_token_class.find_by_access_token(token)
  @authorization = access_token.authorization if access_token
  authentication_required! 'invalid_token' if access_token.nil? || access_token.expired?
end

#has_token?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/oauth2/provider/rack/resource_request.rb', line 11

def has_token?
  !token.nil?
end

#insufficient_scope!Object



61
62
63
# File 'lib/oauth2/provider/rack/resource_request.rb', line 61

def insufficient_scope!
  throw_response Responses.json_error('insufficient_scope', :status => 403)
end

#resource_ownerObject



52
53
54
# File 'lib/oauth2/provider/rack/resource_request.rb', line 52

def resource_owner
  authorization && authorization.resource_owner
end

#tokenObject



7
8
9
# File 'lib/oauth2/provider/rack/resource_request.rb', line 7

def token
  token_from_param || token_from_header
end

#token_from_headerObject



19
20
21
22
23
# File 'lib/oauth2/provider/rack/resource_request.rb', line 19

def token_from_header
  if @env[authorization_key] =~ /OAuth (.*)/
    $1
  end
end

#token_from_paramObject



15
16
17
# File 'lib/oauth2/provider/rack/resource_request.rb', line 15

def token_from_param
  params["oauth_token"]
end

#validate_token!Object



65
66
67
68
69
70
71
# File 'lib/oauth2/provider/rack/resource_request.rb', line 65

def validate_token!
  if has_token? && @token_validated.nil?
    @token_validated = true
    block_bad_request
    block_invalid_token
  end
end