class Simple::OAuth2::Generators::Token

Token generator class. Processes the request by required Grant Type and builds the response

Public Class Methods

generate_for(env) { |request, response| ... } click to toggle source

Generates Token Response based on the request

@return [Simple::OAuth2::Responses] response

# File lib/simple_oauth2/generators/token.rb, line 12
def generate_for(env, &_block)
  token = Rack::OAuth2::Server::Token.new do |request, response|
    request.unsupported_grant_type! unless allowed_grants.include?(request.grant_type.to_s)

    if block_given?
      yield(request, response)
    else
      execute_default(request, response)
    end
  end

  Simple::OAuth2::Responses.new(token.call(env))
end
revoke(token, env) click to toggle source

OAuth 2.0 Token Revocation - tools.ietf.org/html/rfc7009

@return [Response] with HTTP status code 200

# File lib/simple_oauth2/generators/token.rb, line 30
def revoke(token, env)
  access_token = config.access_token_class.by_refresh_token(token)

  if access_token
    request = Rack::OAuth2::Server::Token::Request.new(env)

    # The authorization server, if applicable, first authenticates the client
    # and checks its ownership of the provided token.
    client = Simple::OAuth2::Strategies::Base.authenticate_client(request) || request.invalid_client!
    client.id == access_token.client.id && access_token.revoke!
  end
  # The authorization server responds with HTTP status code 200 if the token
  # has been revoked successfully or if the client submitted an invalid token
  [200, {}, []]
end

Private Class Methods

execute_default(request, response) click to toggle source

Runs default Simple::OAuth2 functionality for Token endpoint.

@param request [Rack::Request] request object @param response [Rack::Response] response object

# File lib/simple_oauth2/generators/token.rb, line 53
def execute_default(request, response)
  strategy = find_strategy(request.grant_type) || request.invalid_grant!
  response.access_token = strategy.process(request)
end
find_strategy(grant_type) click to toggle source

Returns Simple::OAuth2 strategy class by Grant Type

@param grant_type [Symbol] grant type value

@return [Password, RefreshToken, AuthorizationCode] strategy class

# File lib/simple_oauth2/generators/token.rb, line 64
def find_strategy(grant_type)
  "Simple::OAuth2::Strategies::#{grant_type.to_s.camelize}".constantize
end