<commit_before>class SamlIdpController < SamlIdp::IdpController
  layout false
  before_action :setup_saml_configuration

  private

  def idp_authenticate(email, password)
    User.find_and_check_user(email, password)
    valid_user ? User.get_user(email) : nil
  end

  def idp_make_saml_response(found_user)
    encode_response found_user
  end

  def idp_logout
    # user = User.by_email(saml_request.name_id)
    # user.logout
  end

  def setup_saml_configuration
    slug = params[:slug]
    org = Organisation.find_by_slug(slug)
    saml_url = "#{Figaro.env.gate_url}#{slug}/saml"
    SamlIdp.configure do |config|
      config.x509_certificate = org.cert_key
      config.secret_key = org.cert_private_key
      config.organization_name = org.name
      config.organization_url = org.website
      config.base_saml_location = saml_url
      config.session_expiry = 86400
      config.name_id.formats = {
        email_address: ->(principal) { principal.email },
        transient: ->(principal) { principal.user_login_id },
        persistent: ->(principal) { principal.user_login_id },
        name: ->(principal) { principal.name },
      }<commit_msg>Fixed SAML Login Issue<commit_after>class SamlIdpController < SamlIdp::IdpController
  layout false
  before_action :setup_saml_configuration

  private

  def idp_authenticate(email, password)
    valid_user = User.find_and_check_user(email, password)
    valid_user ? User.get_user(email) : nil
  end

  def idp_make_saml_response(found_user)
    encode_response found_user
  end

  def idp_logout
    # user = User.by_email(saml_request.name_id)
    # user.logout
  end

  def setup_saml_configuration
    slug = params[:slug]
    org = Organisation.find_by_slug(slug)
    saml_url = "#{Figaro.env.gate_url}#{slug}/saml"
    SamlIdp.configure do |config|
      config.x509_certificate = org.cert_key
      config.secret_key = org.cert_private_key
      config.organization_name = org.name
      config.organization_url = org.website
      config.base_saml_location = saml_url
      config.session_expiry = 86400
      config.name_id.formats = {
        email_address: ->(principal) { principal.email },
        transient: ->(principal) { principal.user_login_id },
        persistent: ->(principal) { principal.user_login_id },
        name: ->(principal) { principal.name },
      }
{'fraction_non_alphanumeric': 0.09319148936170213, 'fraction_numerical': 0.006808510638297872, 'mean_word_length': 3.46958174904943, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '1994910', 'n_tokens_mistral': 812, 'n_tokens_neox': 789, 'n_words': 132}