2024-06-11 05:28:46 +00:00
|
|
|
# plugins/discourse-md5_authentication/plugin.rb
|
2024-06-11 04:26:59 +00:00
|
|
|
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2024-06-11 05:28:46 +00:00
|
|
|
# name: discourse-md5_authentication
|
2024-06-11 04:26:59 +00:00
|
|
|
# about: A plugin to authenticate users with MD5 passwords from legacy systems
|
2024-06-11 05:28:46 +00:00
|
|
|
# version: 0.3
|
2024-06-11 04:26:59 +00:00
|
|
|
# authors: saint
|
|
|
|
# url: https://gitea.federated.computer/saint/discourse-md5_authentication.git
|
|
|
|
|
|
|
|
after_initialize do
|
2024-06-11 05:28:46 +00:00
|
|
|
Rails.logger.error("MD5 Authentication Plugin: Initialized")
|
2024-06-11 04:26:59 +00:00
|
|
|
class ::User
|
|
|
|
module LegacyMd5Authentication
|
|
|
|
def self.included(base)
|
|
|
|
base.singleton_class.prepend(ClassMethods)
|
|
|
|
end
|
|
|
|
|
|
|
|
module ClassMethods
|
|
|
|
def authenticate(login, password)
|
2024-06-11 05:28:46 +00:00
|
|
|
Rails.logger.error("LegacyMd5Authentication: Trying to authenticate user with login #{login}")
|
2024-06-11 04:26:59 +00:00
|
|
|
|
|
|
|
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
|
2024-06-11 05:28:46 +00:00
|
|
|
Rails.logger.error("LegacyMd5Authentication: User found: #{user.username}")
|
2024-06-11 04:26:59 +00:00
|
|
|
if user.custom_fields['md5_password'] && user.custom_fields['md5_password'] == Digest::MD5.hexdigest(password)
|
2024-06-11 05:28:46 +00:00
|
|
|
Rails.logger.error("LegacyMd5Authentication: MD5 password match for user: #{user.username}")
|
2024-06-11 04:26:59 +00:00
|
|
|
user.update!(password: password)
|
|
|
|
user.custom_fields['md5_password'] = nil
|
|
|
|
user.save_custom_fields
|
|
|
|
return user
|
|
|
|
else
|
2024-06-11 05:28:46 +00:00
|
|
|
Rails.logger.error("LegacyMd5Authentication: MD5 password did not match for user: #{user.username}")
|
2024-06-11 04:26:59 +00:00
|
|
|
end
|
|
|
|
else
|
2024-06-11 05:28:46 +00:00
|
|
|
Rails.logger.error("LegacyMd5Authentication: No user found with login #{login}")
|
2024-06-11 04:26:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
super(login, password)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
include LegacyMd5Authentication
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|