<commit_before>require 'base64'

require_dependency 'carto/user_authenticator'
require_dependency 'carto/email_cleaner'
require_dependency 'carto/errors'

Rails.configuration.middleware.use RailsWarden::Manager do |manager|
  manager.default_strategies :password, :api_authentication
  manager.failure_app = SessionsController
end

# All strategies should:
# - Include this module
# - Override the methods as needed
module CartoStrategy
  def skip_multifactor_authentication?
    true
  end

  def affected_by_password_expiration?
    true
  end

  def check_password_expired(user)
    if affected_by_password_expiration? && user.password_expired?
      throw(:warden, action: :password_change, username: user.username)
    end
  end

  def force_skip_multifactor_authentication(user)
    if skip_multifactor_authentication? && user.mfa_configured?
      throw(:warden, action: :skip_mulfifactor_authentication, user_id: user.id)
    end
  end

  def trigger_login_event(user)
    check_password_expired(user)
    CartoGearsApi::Events::EventManager.instance.notify(CartoGearsApi::Events::UserLoginEvent.new(user))

    # From the very beginning it's been assumed that after login you go to the dashboard, and
    # we're using that event as a synonymous to "last logged in date". Now you can skip dashboard
    # after login (see #11946), so marking that event on authentication is more accurate with the
    # meaning (although not with the name).
    user.view_dashboard
  end
end

# Setup Session Serialization
class Warden::SessionSerializer
  def serialize(user)
    user.username
  end

  def deserialize(username)
    ::User.filter(username: username).first<commit_msg>fix bug<commit_after>require 'base64'

require_dependency 'carto/user_authenticator'
require_dependency 'carto/email_cleaner'
require_dependency 'carto/errors'

Rails.configuration.middleware.use RailsWarden::Manager do |manager|
  manager.default_strategies :password, :api_authentication
  manager.failure_app = SessionsController
end

# All strategies should:
# - Include this module
# - Override the methods as needed
module CartoStrategy
  def skip_multifactor_authentication?
    true
  end

  def affected_by_password_expiration?
    true
  end

  def check_password_expired(user)
    if affected_by_password_expiration? && user.password_expired?
      throw(:warden, action: :password_change, username: user.username)
    end
  end

  def force_skip_multifactor_authentication(user)
    if skip_multifactor_authentication? && user.multifactor_authentication_configured?
      throw(:warden, action: :skip_mulfifactor_authentication, user_id: user.id)
    end
  end

  def trigger_login_event(user)
    check_password_expired(user)
    CartoGearsApi::Events::EventManager.instance.notify(CartoGearsApi::Events::UserLoginEvent.new(user))

    # From the very beginning it's been assumed that after login you go to the dashboard, and
    # we're using that event as a synonymous to "last logged in date". Now you can skip dashboard
    # after login (see #11946), so marking that event on authentication is more accurate with the
    # meaning (although not with the name).
    user.view_dashboard
  end
end

# Setup Session Serialization
class Warden::SessionSerializer
  def serialize(user)
    user.username
  end

  def deserialize(username)
    ::User.filter(username: username).first
{'fraction_non_alphanumeric': 0.07060219519430436, 'fraction_numerical': 0.004153070305547315, 'mean_word_length': 4.91578947368421, 'pattern_counts': {'":': 0, '<': 3, '<?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': '899575', 'n_tokens_mistral': 1022, 'n_tokens_neox': 977, 'n_words': 274}