<commit_before>class OauthController < ApplicationController

  layout nil

  # ignore the csrf token
  skip_before_filter :verify_authenticity_token

  before_filter :ensure_logged_in, :only => [:authorize]
  before_filter :load_application, :except => [:authorize]

  def authorize
    @request_token = OauthProviderEngine::RequestToken.where(:token => params[:oauth_token]).first

    # ensure we have a valid request token
    return render_403("invalid request token") unless @request_token

    @application = @request_token.application

    if request.post?
      # create an access token for the current user
      @request_token.authorize!(OauthProviderEngine.user_method.call(self))
      callback_uri = URI.parse(params.fetch(:oauth_callback, @request_token.application.url))
      token_params = {
        :oauth_token => @request_token.token
      }.to_query
      if callback_uri.query.present?
        callback_uri.query = callback_uri.query + "&" + token_params
      else
        callback_uri.query = token_params
      end
      redirect_to callback_uri.to_s
    else
      # render the allow/disallow form
      render :authorize, :layout => OauthProviderEngine.oauth_layout
    end
  end

  def request_token
    # ensure that the OAuth request was properly signed
    return render_401("invalid signature") unless OAuth::Signature.verify(oauth_request, :consumer_secret => @application.secret)

    @request_token = @application.request_tokens.build()
    @request_token.save
    render :text => {
      :oauth_token => @request_token.token,
      :oauth_token_secret => @request_token.secret
    }.to_query
  end

  def access_token
    token = params.fetch(:oauth_token, oauth_params.fetch("oauth_token"))
    @request_token = OauthProviderEngine::RequestToken.authorized.where(:token => token).first

    # ensure we have a valid request token
    return render_403("invalid request token") unless @request_token

    # ensure that the OAuth request was properly signed
    return render_401("invalid signature") unless OAuth::Signature.verify(oauth_request, :consumer_secret => @application.secret, :token_secret => @request_token.secret) 

    # upgrade the request token to an access token (deletes the request token)
    @access_token = @request_token.upgrade!

    render :text => {
      :oauth_token => @access_token.token,
      :oauth_token_secret => @access_token.secret
    }.to_query
  end

  protected

  def ensure_logged_in
    OauthProviderEngine.authenticate_method.call(self)
  end

  def oauth_request
    @oauth_request ||= OAuth::RequestProxy.proxy(request)
  end

  def oauth_params
    @oauth_params ||= oauth_request.parameters
  end

  def load_application
    @application = OauthProviderEngine::Application.where(:key => oauth_params.fetch("oauth_consumer_key")).first
    render_403('invalid application') unless @application.present?
  end

  def render_401(message)
    render :text => message, :status => 401
  end

  def render_403(message)
    render :text => message, :status => 403
  end

end<commit_msg>return the existing access_token if there is a valid one already<commit_after>class OauthController < ApplicationController

  layout nil

  # ignore the csrf token
  skip_before_filter :verify_authenticity_token

  before_filter :ensure_logged_in, :only => [:authorize]
  before_filter :load_application, :except => [:authorize]

  def authorize
    # ensure we have a valid request token
    @request_token = OauthProviderEngine::RequestToken.where(:token => params[:oauth_token]).first
    return render_403("invalid request token") unless @request_token

    # check to see if the user has already authorized
    user_id = OauthProviderEngine.user_method.call(self)
    if @access_token = OauthProviderEngine::AccessToken.where(:user_id => user_id).first
      @request_token.authorize!(user_id)
      render_authorize_success(@request_token)
      return
    end

    if request.post?
      # create an access token for the current user
      @request_token.authorize!(user_id)
      render_authorize_success(@request_token)
    else
      # render the allow/disallow form
      @application = @request_token.application
      render :authorize, :layout => OauthProviderEngine.oauth_layout
    end
  end

  def request_token
    # ensure that the OAuth request was properly signed
    return render_401("invalid signature") unless OAuth::Signature.verify(oauth_request, :consumer_secret => @application.secret)

    @request_token = @application.request_tokens.build()
    @request_token.save
    render :text => {
      :oauth_token => @request_token.token,
      :oauth_token_secret => @request_token.secret
    }.to_query
  end

  def access_token
    token = params.fetch(:oauth_token, oauth_params.fetch("oauth_token"))
    @request_token = OauthProviderEngine::RequestToken.authorized.where(:token => token).first

    # ensure we have a valid request token
    return render_403("invalid request token") unless @request_token

    # ensure that the OAuth request was properly signed
    return render_401("invalid signature") unless OAuth::Signature.verify(oauth_request, :consumer_secret => @application.secret, :token_secret => @request_token.secret) 

    if @access_token =  OauthProviderEngine::AccessToken.where(:user_id => @request_token.user_id).first
      # user already has a valid access token
      @request_token.destroy
    else
      # upgrade the request token to an access token (deletes the request token)
      @access_token = @request_token.upgrade!
    end

    render :text => {
      :oauth_token => @access_token.token,
      :oauth_token_secret => @access_token.secret
    }.to_query
  end

  protected

  def ensure_logged_in
    OauthProviderEngine.authenticate_method.call(self)
  end

  def oauth_request
    @oauth_request ||= OAuth::RequestProxy.proxy(request)
  end

  def oauth_params
    @oauth_params ||= oauth_request.parameters
  end

  def load_application
    @application = OauthProviderEngine::Application.where(:key => oauth_params.fetch("oauth_consumer_key")).first
    render_403('invalid application') unless @application.present?
  end

  def render_401(message)
    render :text => message, :status => 401
  end

  def render_403(message)
    render :text => message, :status => 403
  end

  def render_authorize_success(request_token)
    callback_uri = URI.parse(params.fetch(:oauth_callback, request_token.application.url))
    token_params = {
      :oauth_token => request_token.token
    }.to_query
    if callback_uri.query.present?
      callback_uri.query = callback_uri.query + "&" + token_params
    else
      callback_uri.query = token_params
    end
    redirect_to callback_uri.to_s
  end

end
{'fraction_non_alphanumeric': 0.0885455355816372, 'fraction_numerical': 0.008022582082900015, 'mean_word_length': 4.035153328347046, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 45, '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': '5431412', 'n_tokens_mistral': 2034, 'n_tokens_neox': 1929, 'n_words': 464}