commit 744484870e2faf58195f55020b94e2d4a3d2a4a4 Author: dsainty Date: Tue Jun 11 14:26:59 2024 +1000 v0.2 of the code and initial README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..9938762 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +A plugin to authenticate users with MD5 passwords from legacy systems + diff --git a/plugin.rb b/plugin.rb new file mode 100644 index 0000000..3550a31 --- /dev/null +++ b/plugin.rb @@ -0,0 +1,59 @@ +# plugins/md5_authentication/plugin.rb + +# frozen_string_literal: true + +# name: md5_authentication +# about: A plugin to authenticate users with MD5 passwords from legacy systems +# version: 0.2 +# authors: saint +# url: https://gitea.federated.computer/saint/discourse-md5_authentication.git + +# plugins/md5_authentication/plugin.rb + +after_initialize do + Rails.logger.info("MD5 Authentication Plugin: Initialized") + class ::User + module LegacyMd5Authentication + def self.included(base) + base.singleton_class.prepend(ClassMethods) + end + + module ClassMethods + def authenticate(login, password) + Rails.logger.info("LegacyMd5Authentication: Trying to authenticate user with login #{login}") + + user = nil + + if login.include?('@') + # Assume it's an email address + user_email = UserEmail.find_by(email: login.downcase.strip) + 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.info("LegacyMd5Authentication: User found: #{user.username}") + if user.custom_fields['md5_password'] && user.custom_fields['md5_password'] == Digest::MD5.hexdigest(password) + Rails.logger.info("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.info("LegacyMd5Authentication: MD5 password did not match for user: #{user.username}") + end + else + Rails.logger.info("LegacyMd5Authentication: No user found with login #{login}") + end + + super(login, password) + end + end + end + + include LegacyMd5Authentication + end +end +