Sha256: 2ec72da1274f8df4b5f1c922dbd94a3c0977e6007b3c47008e7a5090798fc5ad

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

module HttpBasicAuthentication
  module Patches
    # This module patches the default authentication system
    # by using HTTP Basic Authorization headers fields to login
    # users or create them if necessary.
    module ApplicationControllerPatch
      extend ActiveSupport::Concern

      included do
        alias_method_chain :try_to_autologin, :http_basic
      end

      # We hijack the autologin method as this the HTTP Basic authorization
      # is a kind of auto login system which created users on the fly.
      def try_to_autologin_with_http_basic
        if http_authorization?
          authenticate_with_http_basic do |username, _password|
            logger.info "Successful authentication for '#{username}'" \
              "from #{request.remote_ip} at #{Time.now.utc}"
            self.logged_user = User.find_by_login(username) ||
              create_http_authorization_user(username)
          end
        else
          try_to_autologin_without_http_basic
        end
      end

      private

      def http_authorization?
        request.authorization.present?
      end

      def create_http_authorization_user(username)
        email = "#{username}#{email_suffix}"
        user = User.new(mail: email, firstname: username, lastname: username)
        user.login = username
        user.save!
      end

      def email_suffix
        Setting.plugin_http_basic_authorization["email_suffix"]
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
redmine-http_basic_authentication-0.0.1 lib/http_basic_authentication/patches/application_controller_patch.rb