Module: Simple::OAuth2::Helpers

Defined in:
lib/simple_oauth2/helpers.rb

Overview

Set of Simple::OAuth2 helpers

Instance Method Summary collapse

Instance Method Details

#access_token_required!(*scopes) ⇒ Object

Adds OAuth2 AccessToken protection for routes

Parameters:

  • scopes (Array<String, Symbol>)

    set of scopes required to access the endpoint

Raises:

  • (Rack::OAuth2::Server::Resource::Bearer::Unauthorized)

    invalid AccessToken value

  • (Rack::OAuth2::Server::Resource::Bearer::Forbidden)

    AccessToken expired, revoked or does’t have required scopes



13
14
15
16
# File 'lib/simple_oauth2/helpers.rb', line 13

def access_token_required!(*scopes)
  raise Rack::OAuth2::Server::Resource::Bearer::Unauthorized if current_access_token.nil?
  raise Rack::OAuth2::Server::Resource::Bearer::Forbidden unless valid_access_token?(scopes)
end

#current_access_tokenObject

Returns AccessToken instance found by access_token value passed with the request



24
25
26
# File 'lib/simple_oauth2/helpers.rb', line 24

def current_access_token
  @current_access_token ||= request.env[Rack::OAuth2::Server::Resource::ACCESS_TOKEN]
end

#current_resource_ownerObject

Returns ResourceOwner from the AccessToken found by access_token value passed with the request



19
20
21
# File 'lib/simple_oauth2/helpers.rb', line 19

def current_resource_owner
  @current_resource_owner ||= instance_eval(&Simple::OAuth2.config.resource_owner_authenticator)
end