<commit_before># frozen_string_literal: true
module ShopifyApp
  class SessionsController < ActionController::Base
    include ShopifyApp::LoginProtection

    layout false, only: :new

    after_action only: [:new, :create] do |controller|
      controller.response.headers.except!('X-Frame-Options')
    end

    def new
      authenticate if sanitized_shop_name.present?
    end

    def create
      authenticate
    end

    def enable_cookies
      return unless validate_shop_presence

      render(:enable_cookies, layout: false, locals: {
        does_not_have_storage_access_url: top_level_interaction_path(
          shop: sanitized_shop_name,
          return_to: params[:return_to]
        ),
        has_storage_access_url: login_url_with_optional_shop(top_level: true),
        app_target_url: granted_storage_access_path(
          shop: sanitized_shop_name,
          return_to: params[:return_to]
        ),
        current_shopify_domain: current_shopify_domain,
      })
    end

    def top_level_interaction
      @url = login_url_with_optional_shop(top_level: true)
      validate_shop_presence
    end

    def granted_storage_access
      return unless validate_shop_presence

      session['shopify.granted_storage_access'] = true

      copy_return_to_param_to_session

      redirect_to(return_address_with_params({ shop: @shop }))
    end

    def destroy
      reset_session
      flash[:notice] = I18n.t('.logged_out')
      redirect_to(login_url_with_optional_shop)
    end

    private

    def authenticate
      return render_invalid_shop_error unless sanitized_shop_name.present?
      session['shopify.omniauth_params'] = { shop: sanitized_shop_name }

      copy_return_to_param_to_session

      set_user_tokens_option

      if user_agent_can_partition_cookies
        authenticate_with_partitioning
      else
        authenticate_normally
      end
    end

    def authenticate_normally
      if request_storage_access?
        redirect_to_request_storage_access
      elsif authenticate_in_context?
        authenticate_in_context
      else
        authenticate_at_top_level
      end
    end

    def authenticate_with_partitioning
      if session['shopify.cookies_persist']
        clear_top_level_oauth_cookie
        authenticate_in_context
      else
        set_top_level_oauth_cookie
        enable_cookie_access
      end
    end

    def set_user_tokens_option
      if shop_session.blank?
        session[:user_tokens] = false
        return
      end

      session[:user_tokens] = ShopifyApp::SessionRepository.user_storage.present?

      ShopifyAPI::Session.temp(
        domain: shop_session.domain,
        token: shop_session.token,
        api_version: shop_session.api_version
      ) do
        ShopifyAPI::Metafield.find(:token_validity_bogus_check)
      end
    rescue ActiveResource::UnauthorizedAccess
      session[:user_tokens] = false
    rescue StandardError
    end

    def validate_shop_presence
      @shop = sanitized_shop_name
      unless @shop
        render_invalid_shop_error
        return false
      end

      true
    end

    def copy_return_to_param_to_session
      session[:return_to] = params[:return_to] if params[:return_to]
    end

    def render_invalid_shop_error
      flash[:error] = I18n.t('invalid_shop_url')
      redirect_to(return_address)
    end

    def enable_cookie_access
      fullpage_redirect_to(enable_cookies_path(
        shop: sanitized_shop_name,
        return_to: session[:return_to]<commit_msg>raise standard error in sessions_controller.rb<commit_after># frozen_string_literal: true
module ShopifyApp
  class SessionsController < ActionController::Base
    include ShopifyApp::LoginProtection

    layout false, only: :new

    after_action only: [:new, :create] do |controller|
      controller.response.headers.except!('X-Frame-Options')
    end

    def new
      authenticate if sanitized_shop_name.present?
    end

    def create
      authenticate
    end

    def enable_cookies
      return unless validate_shop_presence

      render(:enable_cookies, layout: false, locals: {
        does_not_have_storage_access_url: top_level_interaction_path(
          shop: sanitized_shop_name,
          return_to: params[:return_to]
        ),
        has_storage_access_url: login_url_with_optional_shop(top_level: true),
        app_target_url: granted_storage_access_path(
          shop: sanitized_shop_name,
          return_to: params[:return_to]
        ),
        current_shopify_domain: current_shopify_domain,
      })
    end

    def top_level_interaction
      @url = login_url_with_optional_shop(top_level: true)
      validate_shop_presence
    end

    def granted_storage_access
      return unless validate_shop_presence

      session['shopify.granted_storage_access'] = true

      copy_return_to_param_to_session

      redirect_to(return_address_with_params({ shop: @shop }))
    end

    def destroy
      reset_session
      flash[:notice] = I18n.t('.logged_out')
      redirect_to(login_url_with_optional_shop)
    end

    private

    def authenticate
      return render_invalid_shop_error unless sanitized_shop_name.present?
      session['shopify.omniauth_params'] = { shop: sanitized_shop_name }

      copy_return_to_param_to_session

      set_user_tokens_option

      if user_agent_can_partition_cookies
        authenticate_with_partitioning
      else
        authenticate_normally
      end
    end

    def authenticate_normally
      if request_storage_access?
        redirect_to_request_storage_access
      elsif authenticate_in_context?
        authenticate_in_context
      else
        authenticate_at_top_level
      end
    end

    def authenticate_with_partitioning
      if session['shopify.cookies_persist']
        clear_top_level_oauth_cookie
        authenticate_in_context
      else
        set_top_level_oauth_cookie
        enable_cookie_access
      end
    end

    def set_user_tokens_option
      if shop_session.blank?
        session[:user_tokens] = false
        return
      end

      session[:user_tokens] = ShopifyApp::SessionRepository.user_storage.present?

      ShopifyAPI::Session.temp(
        domain: shop_session.domain,
        token: shop_session.token,
        api_version: shop_session.api_version
      ) do
        ShopifyAPI::Metafield.find(:token_validity_bogus_check)
      end
    rescue ActiveResource::UnauthorizedAccess
      session[:user_tokens] = false
    rescue StandardError
      raise StandardError
    end

    def validate_shop_presence
      @shop = sanitized_shop_name
      unless @shop
        render_invalid_shop_error
        return false
      end

      true
    end

    def copy_return_to_param_to_session
      session[:return_to] = params[:return_to] if params[:return_to]
    end

    def render_invalid_shop_error
      flash[:error] = I18n.t('invalid_shop_url')
      redirect_to(return_address)
    end

    def enable_cookie_access
      fullpage_redirect_to(enable_cookies_path(
        shop: sanitized_shop_name,
        return_to: session[:return_to]
{'fraction_non_alphanumeric': 0.053684655488234465, 'fraction_numerical': 0.0011272368606453432, 'mean_word_length': 2.9389567147613764, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '129201', 'n_tokens_mistral': 2169, 'n_tokens_neox': 2074, 'n_words': 377}