<commit_before>class TokenEndpoint

  def call(env)
    authenticator.call(env)
  end

  private

  def authenticator
    Rack::OAuth2::Server::Token.new do |req, res|
      client = Client.find_by_identifier(req.client_id) || req.invalid_client!
      client.secret == req.client_secret || req.invalid_client!

      refresh_token = refresh_token(req, client)
      if refresh_token && token = refresh_token.access_tokens.create(:client => client, :user => refresh_token.user)
        res.access_token = token.to_bearer_token
      else
        req.invalid_grant!
      end
    end
  end

  # NOTE: extended assertion grant_types are not supported yet.
  # NOTE: client_credentials grant_types are not yet supported
  def refresh_token(req, client)
    case req.grant_type
    when :authorization_code
      code = AuthorizationCode.valid.find_by_token(req.code)
      return nil unless code.valid_request?(req)
      client.refresh_tokens.create! :user => code.user
    when :password
      resource = mapping.to.find_for_authentication(mapping.to.authentication_keys.first => req.username)
      return nil unless resource && resource.respond_to?(:valid_password?)
      valid = resource.valid_for_authentication? { resource.valid_password?(req.password) }
      return nil unless valid.is_a?(TrueClass)
      client.refresh_tokens.create! :user => resource
    when :refresh_token
      refresh_token = client.refresh_tokens.valid.find_by_token(req.refresh_token)
      return nil unless refresh_token.present?
      refresh_token
    else
      nil
    end
  end
  def mapping
    Devise.mappings[scope]
  end
  #TODO: allow configurable mapping to other resources
  def scope
    :user
  end
end
<commit_msg>use exceptions to control flow<commit_after>class TokenEndpoint
  class InvalidGrantType < StandardError; end
  def call(env)
    authenticator.call(env)
  end

  private

  def authenticator
    Rack::OAuth2::Server::Token.new do |req, res|
      client = Client.find_by_identifier(req.client_id) || req.invalid_client!
      client.secret == req.client_secret || req.invalid_client!

      begin
        refresh_token = find_refresh_token(req, client)
        access_token = refresh_token.access_tokens.create!(:client => client, :user => refresh_token.user)
        res.access_token = access_token.to_bearer_token
      rescue => e
        puts e.inspect
        req.invalid_grant!
      end
    end
  end

  # NOTE: extended assertion grant_types are not supported yet.
  # NOTE: client_credentials grant_types are not yet supported
  def find_refresh_token(req, client)
    case req.grant_type
    when :authorization_code
      code = AuthorizationCode.valid.find_by_token(req.code)
      raise InvalidGrantType.new('invalid authorization code') unless code && code.valid_request?(req)
      client.refresh_tokens.create! :user => code.user
    when :password
      resource = mapping.to.find_for_authentication(mapping.to.authentication_keys.first => req.username)
      raise InvalidGrantType.new('user not found') unless resource
      raise InvalidGrantType.new('user does not support password authentication') unless resource.respond_to?(:valid_password?)
      valid = resource.valid_for_authentication? { resource.valid_password?(<PASSWORD>) }
      raise InvalidGrantType.new("authentication failed: #{valid}") unless valid.is_a?(TrueClass)
      client.refresh_tokens.create! :user => resource
    when :refresh_token
      refresh_token = client.refresh_tokens.valid.find_by_token(req.refresh_token)
      raise InvalidGrantType.new('refresh token not found') unless refresh_token
      refresh_token
    else
      raise InvalidGrantType.new('invalid grant type')
    end
  end
  def mapping
    Devise.mappings[scope]
  end
  #TODO: allow configurable mapping to other resources
  def scope
    :user
  end
end

{'fraction_non_alphanumeric': 0.08352849336455893, 'fraction_numerical': 0.0005204267499349467, 'mean_word_length': 3.659393939393939, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '620669', 'n_tokens_mistral': 1122, 'n_tokens_neox': 1106, 'n_words': 263}