<commit_before># This file is part of Mconf-Web, a web application that provides access
# to the Mconf webconferencing system. Copyright (C) 2010-2015 Mconf.
#
# This file is licensed under the Affero General Public License version
# 3 or later. See the LICENSE file.

require 'net/ldap'
require 'devise/strategies/authenticatable'

module Devise
  module Strategies
    class LdapAuthenticatable < Authenticatable

      def valid?
        # disable this auth method if not coming from the sessions controller
        # this strategy is called from other places as well, like after registering
        # leaving it enabled for all controllers can generate a few errors
        ldap_enabled? && params && params[:controller] == "sessions"
      end

      def authenticate!
        if ldap_enabled? and params[:user]
          Rails.logger.info "LDAP: LDAP is enabled, trying to connect to LDAP server"
          configs = ldap_configs
          ldap = ldap_connection(configs)
          ldap.auth(configs.ldap_user, configs.ldap_user_password)

          # Tries to bind to the ldap server
          begin
            bind_error = ldap_bind(ldap)
          rescue Net::LDAP::LdapError => e
            Rails.logger.error "LDAP: exception: #{e.inspect}"
            bind_error = :server_error
          end

          if bind_error
            case bind_error
            when :timeout
              Rails.logger.error "LDAP: authentication failed: timeout when trying to connect to the LDAP server"
              fail(I18n.t('devise.strategies.ldap_authenticatable.invalid_bind'))
            when :bind
              Rails.logger.error "LDAP: authentication failed: initial bind failed"
              fail(I18n.t('devise.strategies.ldap_authenticatable.invalid_bind'))
            when :server_error
              Rails.logger.error "LDAP: unknown server error or invalid response"
              fail(I18n.t('devise.strategies.ldap_authenticatable.invalid_bind'))
            end

          else
            Rails.logger.info "LDAP: trying to bind the target user: '#{login_from_params}'"

            # Tries to authenticate the user to the ldap server
            filter = ldap_filter(configs)
            Rails.logger.info "LDAP: filter: #{filter.inspect}"
            ldap_user = ldap.bind_as(:base => configs.ldap_user_treebase, :filter => filter, :password => password_from_params)

            unless ldap_user
              Rails.logger.error "LDAP: authentication failed: response: #{ldap_user}"
              Rails.logger.error "LDAP: error code: #{ldap.get_operation_result.code}"
              Rails.logger.error "LDAP: error message: #{ldap.get_operation_result.message}"
              fail(:invalid)

            else
              Rails.logger.info "LDAP: user successfully authenticated: #{ldap_user}"
              ldap_helper = Mconf::LDAP.new(session)
              invalid_fields = ldap_helper.validate_user(ldap_user.first, configs)
              if invalid_fields
                case invalid_fields
                when :username
                  Rails.logger.error "LDAP: authentication failed: the user has no username set in the LDAP server"
                  fail(I18n.t('devise.strategies.ldap_authenticatable.missing_username'))
                when :name
                  Rails.logger.error "LDAP: authentication failed: the user has no name set in the LDAP server"
                  fail(I18n.t('devise.strategies.ldap_authenticatable.missing_name'))
                when :email
                  Rails.logger.error "LDAP: authentication failed: the user has no email set in the LDAP server"
                  fail(I18n.t('devise.strategies.ldap_authenticatable.missing_email'))
                end

              else
                # login or create the account
                user = ldap_helper.find_or_create_user(ldap_user.first, configs)
                if user.nil?
                  Rails.logger.error "LDAP: authentication failed: application wasn't able to create a new user"
                  fail(I18n.t('devise.strategies.ldap_authenticatable.create_failed'))
                else
                  if user.active_for_authentication?
                    ldap_helper.sign_user_in(user)
                    success!(user)
                  else
                    # throw the not_approved error that will take the user to the pending approval page
                    throw(:warden, message: :not_approved)
                  end

                end
              end
            end
          end

        # LDAP is not enabled in the site
        elsif not ldap_enabled?
          Rails.logger.info "LDAP: authentication via LDAP is not enabled"
          fail(I18n.t('devise.strategies.ldap_authenticatable.ldap_not_enabled'))
        else
          Rails.logger.info "LDAP: authentication failed: invalid user credentials"
          fail(:invalid)
        end
      end

      # Returns the login provided by user
      def login_from_params
        params[:user][:login]
      end

      # Returns the password provided by user
      def password_from_params
        params[:user][:password]
      end

      # Returns the model that stores the configurations for LDAP
      def ldap_configs
        Site.current
      end

      # Returns the filter to bind the user<commit_msg>Make ldap redirect to correct inactive login screen<commit_after># This file is part of Mconf-Web, a web application that provides access
# to the Mconf webconferencing system. Copyright (C) 2010-2015 Mconf.
#
# This file is licensed under the Affero General Public License version
# 3 or later. See the LICENSE file.

require 'net/ldap'
require 'devise/strategies/authenticatable'

module Devise
  module Strategies
    class LdapAuthenticatable < Authenticatable

      def valid?
        # disable this auth method if not coming from the sessions controller
        # this strategy is called from other places as well, like after registering
        # leaving it enabled for all controllers can generate a few errors
        ldap_enabled? && params && params[:controller] == "sessions"
      end

      def authenticate!
        if ldap_enabled? and params[:user]
          Rails.logger.info "LDAP: LDAP is enabled, trying to connect to LDAP server"
          configs = ldap_configs
          ldap = ldap_connection(configs)
          ldap.auth(configs.ldap_user, configs.ldap_user_password)

          # Tries to bind to the ldap server
          begin
            bind_error = ldap_bind(ldap)
          rescue Net::LDAP::LdapError => e
            Rails.logger.error "LDAP: exception: #{e.inspect}"
            bind_error = :server_error
          end

          if bind_error
            case bind_error
            when :timeout
              Rails.logger.error "LDAP: authentication failed: timeout when trying to connect to the LDAP server"
              fail(I18n.t('devise.strategies.ldap_authenticatable.invalid_bind'))
            when :bind
              Rails.logger.error "LDAP: authentication failed: initial bind failed"
              fail(I18n.t('devise.strategies.ldap_authenticatable.invalid_bind'))
            when :server_error
              Rails.logger.error "LDAP: unknown server error or invalid response"
              fail(I18n.t('devise.strategies.ldap_authenticatable.invalid_bind'))
            end

          else
            Rails.logger.info "LDAP: trying to bind the target user: '#{login_from_params}'"

            # Tries to authenticate the user to the ldap server
            filter = ldap_filter(configs)
            Rails.logger.info "LDAP: filter: #{filter.inspect}"
            ldap_user = ldap.bind_as(:base => configs.ldap_user_treebase, :filter => filter, :password => password_from_params)

            unless ldap_user
              Rails.logger.error "LDAP: authentication failed: response: #{ldap_user}"
              Rails.logger.error "LDAP: error code: #{ldap.get_operation_result.code}"
              Rails.logger.error "LDAP: error message: #{ldap.get_operation_result.message}"
              fail(:invalid)

            else
              Rails.logger.info "LDAP: user successfully authenticated: #{ldap_user}"
              ldap_helper = Mconf::LDAP.new(session)
              invalid_fields = ldap_helper.validate_user(ldap_user.first, configs)
              if invalid_fields
                case invalid_fields
                when :username
                  Rails.logger.error "LDAP: authentication failed: the user has no username set in the LDAP server"
                  fail(I18n.t('devise.strategies.ldap_authenticatable.missing_username'))
                when :name
                  Rails.logger.error "LDAP: authentication failed: the user has no name set in the LDAP server"
                  fail(I18n.t('devise.strategies.ldap_authenticatable.missing_name'))
                when :email
                  Rails.logger.error "LDAP: authentication failed: the user has no email set in the LDAP server"
                  fail(I18n.t('devise.strategies.ldap_authenticatable.missing_email'))
                end

              else
                # login or create the account
                user = ldap_helper.find_or_create_user(ldap_user.first, configs)
                if user.nil?
                  Rails.logger.error "LDAP: authentication failed: application wasn't able to create a new user"
                  fail(I18n.t('devise.strategies.ldap_authenticatable.create_failed'))
                else
                  # if user.active_for_authentication?
                  # We don't check authentication here, let devise find out about an
                  # unapproved user later and show the errors there
                  ldap_helper.sign_user_in(user)
                  success!(user)
                end
              end
            end
          end

        # LDAP is not enabled in the site
        elsif not ldap_enabled?
          Rails.logger.info "LDAP: authentication via LDAP is not enabled"
          fail(I18n.t('devise.strategies.ldap_authenticatable.ldap_not_enabled'))
        else
          Rails.logger.info "LDAP: authentication failed: invalid user credentials"
          fail(:invalid)
        end
      end

      # Returns the login provided by user
      def login_from_params
        params[:user][:login]
      end

      # Returns the password provided by user
      def password_from_params
        params[:user][:password]
      end

      # Returns the model that stores the configurations for LDAP
      def ldap_configs
        Site.current
      end

      # Returns the filter to bind the user
{'fraction_non_alphanumeric': 0.06875821904940822, 'fraction_numerical': 0.004696599661844825, 'mean_word_length': 2.0691842029403285, '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': '6806448', 'n_tokens_mistral': 2920, 'n_tokens_neox': 2778, 'n_words': 842}