<commit_before>require_relative 'application_controller'

class SocialAccountsController < ApplicationController
  # Got some inspiration from: http://www.communityguides.eu/articles/16

  def service_callback
    omniauth_obj = parse_omniauth_env provider_name

    if user_signed_in?
      connect_provider provider_name, omniauth_obj
    else
      sign_in_through_provider provider_name, omniauth_obj
    end

    respond_to do |format|
      format.html { render :callback, { layout: 'social_popup', locals: {event_details: @provider_name}}}
    end
  rescue Exception => error
    @event = "social_error"
    @error = error.message

    flash[:alert] = @error

    respond_to do |format|
      format.html { render :callback, { layout: 'social_popup', locals: {event_details: @error}}}
    end
  end

  def service_deauthorize
    case provider_name
    when 'facebook'
      provider_deauthorize provider_name do |uid, token|
        response = HTTParty.delete("https://graph.facebook.com/#{uid}/permissions?access_token=#{token}")
        if response.code != 200 and response.code != 400
          raise "Facebook deauthorize failed: '#{response.body}'."
        end
      end
    when 'twitter'
      provider_deauthorize provider_name do
        flash[:notice] = 'To complete, please deauthorize Factlink at the Twitter website.'
      end
    else
      raise "Wrong OAuth provider: #{omniauth['provider']}"
    end

    redirect_to edit_user_path(current_user)
  end

  def oauth_failure
    if params[:error_description].blank?
      params[:error_description] ||= "unspecified error"
    end
    @event = "social_error"
    @error = "Authorization failed: #{params[:error_description]}."

    respond_to do |format|
      format.html { render :callback, { layout: 'social_popup', locals: {event_details: @error}}}
    end
  end

  private

  def connect_provider provider_name, omniauth_obj
    authorize! :update, current_user

    if is_connected_to_different_user(provider_name, omniauth_obj)
      raise "Already connected to a different account, please sign in to the connected account or reconnect your account."
    elsif omniauth_obj
      current_user.social_account(provider_name).save_omniauth_obj!(omniauth_obj)
      flash[:notice] = "Succesfully connected."
      @event = 'authorized'
    else
      raise "Error connecting."
    end
  end

  def is_connected_to_different_user provider_name, omniauth_obj
    current_user.social_account(provider_name).different_from?(omniauth_obj)
  end

  def sign_in_through_provider provider_name, omniauth_obj
    social_account = SocialAccount.find_with_omniauth_obj(provider_name, omniauth_obj)

    if social_account and social_account.user
      @user = social_account.user
      sign_in @user
      @event = 'signed_in'
    else
      @event = 'social_error'
      @provider_name = "No connected #{provider_name.capitalize} account found. "+
                       "Please sign in with your credentials and connect your #{provider_name.capitalize} account."
    end
  end

  def parse_omniauth_env provider_name
    omniauth = request.env['omniauth.auth']
    raise "Wrong OAuth provider: #{omniauth['provider']}" if provider_name != omniauth['provider']
    raise 'Invalid omniauth object' unless omniauth['uid']

    if provider_name == 'twitter'
      omniauth['extra']['oath_version'] = omniauth['extra']['access_token'].consumer.options['oauth_version'];
      omniauth['extra']['signature_method'] = omniauth['extra']['access_token'].consumer.options['signature_method']
      omniauth['extra'].delete 'access_token'
    end

    omniauth
  end

  def provider_deauthorize provider_name, &block
    authorize! :update, current_user

    social_account = current_user.social_account(provider_name)

    if social_account.persisted?
      uid = social_account.omniauth_obj['uid']
      token = social_account.omniauth_obj['credentials']['token']<commit_msg>Use "fail" instead of "raise"<commit_after>require_relative 'application_controller'

class SocialAccountsController < ApplicationController
  # Got some inspiration from: http://www.communityguides.eu/articles/16

  def service_callback
    omniauth_obj = parse_omniauth_env provider_name

    if user_signed_in?
      connect_provider provider_name, omniauth_obj
    else
      sign_in_through_provider provider_name, omniauth_obj
    end

    respond_to do |format|
      format.html { render :callback, { layout: 'social_popup', locals: {event_details: @provider_name}}}
    end
  rescue Exception => error
    @event = "social_error"
    @error = error.message

    flash[:alert] = @error

    respond_to do |format|
      format.html { render :callback, { layout: 'social_popup', locals: {event_details: @error}}}
    end
  end

  def service_deauthorize
    case provider_name
    when 'facebook'
      provider_deauthorize provider_name do |uid, token|
        response = HTTParty.delete("https://graph.facebook.com/#{uid}/permissions?access_token=#{token}")
        if response.code != 200 and response.code != 400
          fail "Facebook deauthorize failed: '#{response.body}'."
        end
      end
    when 'twitter'
      provider_deauthorize provider_name do
        flash[:notice] = 'To complete, please deauthorize Factlink at the Twitter website.'
      end
    else
      fail "Wrong OAuth provider: #{omniauth['provider']}"
    end

    redirect_to edit_user_path(current_user)
  end

  def oauth_failure
    if params[:error_description].blank?
      params[:error_description] ||= "unspecified error"
    end
    @event = "social_error"
    @error = "Authorization failed: #{params[:error_description]}."

    respond_to do |format|
      format.html { render :callback, { layout: 'social_popup', locals: {event_details: @error}}}
    end
  end

  private

  def connect_provider provider_name, omniauth_obj
    authorize! :update, current_user

    if is_connected_to_different_user(provider_name, omniauth_obj)
      fail "Already connected to a different account, please sign in to the connected account or reconnect your account."
    elsif omniauth_obj
      current_user.social_account(provider_name).save_omniauth_obj!(omniauth_obj)
      flash[:notice] = "Succesfully connected."
      @event = 'authorized'
    else
      fail "Error connecting."
    end
  end

  def is_connected_to_different_user provider_name, omniauth_obj
    current_user.social_account(provider_name).different_from?(omniauth_obj)
  end

  def sign_in_through_provider provider_name, omniauth_obj
    social_account = SocialAccount.find_with_omniauth_obj(provider_name, omniauth_obj)

    if social_account and social_account.user
      @user = social_account.user
      sign_in @user
      @event = 'signed_in'
    else
      @event = 'social_error'
      @provider_name = "No connected #{provider_name.capitalize} account found. "+
                       "Please sign in with your credentials and connect your #{provider_name.capitalize} account."
    end
  end

  def parse_omniauth_env provider_name
    omniauth = request.env['omniauth.auth']
    fail "Wrong OAuth provider: #{omniauth['provider']}" if provider_name != omniauth['provider']
    fail 'Invalid omniauth object' unless omniauth['uid']

    if provider_name == 'twitter'
      omniauth['extra']['oath_version'] = omniauth['extra']['access_token'].consumer.options['oauth_version'];
      omniauth['extra']['signature_method'] = omniauth['extra']['access_token'].consumer.options['signature_method']
      omniauth['extra'].delete 'access_token'
    end

    omniauth
  end

  def provider_deauthorize provider_name, &block
    authorize! :update, current_user

    social_account = current_user.social_account(provider_name)

    if social_account.persisted?
      uid = social_account.omniauth_obj['uid']
      token = social_account.omniauth_obj['credentials']['token']
{'fraction_non_alphanumeric': 0.09444021325209444, 'fraction_numerical': 0.002030972328002031, 'mean_word_length': 3.8907510862818127, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3308056', 'n_tokens_mistral': 2375, 'n_tokens_neox': 2264, 'n_words': 547}