<commit_before># encoding: utf-8

module Carto
  class SamlService
    def initialize(organization)
      raise "organization can't be nil" unless organization

      @organization = organization
    end

    def enabled?
      carto_saml_configuration.present?
    end

    def authentication_request
      OneLogin::RubySaml::Authrequest.new.create(saml_settings)
    end

    def get_user_email(saml_response_param)
      saml_response = saml_response_from_saml_response_param(saml_response_param)

      email_from_saml_response(saml_response) if saml_response
    end

    private

    def saml_response_from_saml_response_param(saml_response_param)
      response = get_saml_response(saml_response_param)

      response.is_valid? ? response : debug_response("SAML response not valid", response)
    end

    def email_from_saml_response(saml_response)
      email = saml_response.attributes[email_attribute]

      email.present? ? email : debug_response("SAML response lacks email", saml_response)
    end

    def debug_response(message, response)
      CartoDB::Logger.debug(message: message, response_settings: response.settings, response_options: response.options)
    end

    def get_saml_response(saml_response_param)
      OneLogin::RubySaml::Response.new(
        saml_response_param,
        settings: saml_settings,
        allowed_clock_drift: carto_saml_configuration[:allowed_clock_drift] || 3600
      )
    end
<commit_msg>Straightforward SAML response validation #11129<commit_after># encoding: utf-8

module Carto
  class SamlService
    def initialize(organization)
      raise "organization can't be nil" unless organization

      @organization = organization
    end

    def enabled?
      carto_saml_configuration.present?
    end

    def authentication_request
      OneLogin::RubySaml::Authrequest.new.create(saml_settings)
    end

    def get_user_email(saml_response_param)
      response = get_saml_response(saml_response_param)
      response.is_valid? ? email_from_saml_response(response) : debug_response("Invalid SAML response", response)
    end

    private

    def email_from_saml_response(saml_response)
      email = saml_response.attributes[email_attribute]

      email.present? ? email : debug_response("SAML response lacks email", saml_response)
    end

    def debug_response(message, response)
      CartoDB::Logger.debug(message: message, response_settings: response.settings, response_options: response.options)
      nil
    end

    def get_saml_response(saml_response_param)
      OneLogin::RubySaml::Response.new(
        saml_response_param,
        settings: saml_settings,
        allowed_clock_drift: carto_saml_configuration[:allowed_clock_drift] || 3600
      )
    end

{'fraction_non_alphanumeric': 0.0664718772826881, 'fraction_numerical': 0.00547845142439737, 'mean_word_length': 3.970961887477314, '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': '5292541', 'n_tokens_mistral': 854, 'n_tokens_neox': 799, 'n_words': 156}