v0.4 -- change to attempt use of Auth::DefaultAuthenticator Instead of User, code comments

This commit is contained in:
dsainty 2024-06-11 16:35:22 +10:00
parent b4707d53dd
commit fa22e04e65

View File

@ -4,54 +4,57 @@
# name: discourse-md5_authentication # name: discourse-md5_authentication
# about: A plugin to authenticate users with MD5 passwords from legacy systems # about: A plugin to authenticate users with MD5 passwords from legacy systems
# version: 0.3 # version: 0.4
# authors: saint # authors: saint
# url: https://gitea.federated.computer/saint/discourse-md5_authentication.git # url: https://gitea.federated.computer/saint/discourse-md5_authentication.git
# This block of code is executed after the Discourse application is initialized.
after_initialize do after_initialize do
Rails.logger.error("MD5 Authentication Plugin: Initialized") # Reopening the Auth::DefaultAuthenticator class to add custom authentication logic.
class ::User class ::Auth::DefaultAuthenticator
# Define a module to encapsulate the MD5 authentication logic.
module LegacyMd5Authentication module LegacyMd5Authentication
def self.included(base) # This method is called when the module is prepended to the class.
base.singleton_class.prepend(ClassMethods) # It creates an alias for the existing authenticate method to preserve it.
def self.prepended(base)
base.singleton_class.class_eval do
alias_method :old_authenticate, :authenticate
end
end end
module ClassMethods # Override the authenticate method to add custom MD5 password logic.
def authenticate(login, password) def authenticate(email_or_username, password)
Rails.logger.error("LegacyMd5Authentication: Trying to authenticate user with login #{login}") # Log an attempt to authenticate.
Rails.logger.info("MD5 Auth: Attempting to authenticate #{email_or_username}")
user = nil # Find the user by their username or email.
user = User.find_by_username_or_email(email_or_username.downcase.strip)
if login.include?('@') # Log if a user is found.
# Assume it's an email address if user
user_email = UserEmail.find_by(email: login.downcase.strip) Rails.logger.info("MD5 Auth: User found - #{user.username}")
user = user_email ? User.find(user_email.user_id) : nil
else
# Assume it's a username
user = User.find_by(username: login.downcase.strip)
end
if user
Rails.logger.error("LegacyMd5Authentication: User found: #{user.username}")
if user.custom_fields['md5_password'] && user.custom_fields['md5_password'] == Digest::MD5.hexdigest(password)
Rails.logger.error("LegacyMd5Authentication: MD5 password match for user: #{user.username}")
user.update!(password: password)
user.custom_fields['md5_password'] = nil
user.save_custom_fields
return user
else
Rails.logger.error("LegacyMd5Authentication: MD5 password did not match for user: #{user.username}")
end
else
Rails.logger.error("LegacyMd5Authentication: No user found with login #{login}")
end
super(login, password)
end end
# Check if the user's custom field contains an MD5 password and if it matches the provided password.
if user && user.custom_fields['md5_password'] && user.custom_fields['md5_password'] == Digest::MD5.hexdigest(password)
Rails.logger.info("MD5 Auth: MD5 password match for user #{user.username}")
# Update the user's password to the new format and clear the MD5 password field.
user.update!(password: password)
user.custom_fields['md5_password'] = nil
user.save_custom_fields
# Return an authentication result indicating success.
return Auth::Result.new(user)
end
# If MD5 authentication fails, fall back to the original authentication method.
Rails.logger.info("MD5 Auth: Falling back to default authentication for #{email_or_username}")
old_authenticate(email_or_username, password)
end end
end end
include LegacyMd5Authentication # Prepend the module to the DefaultAuthenticator class.
prepend LegacyMd5Authentication
end end
end end