From 8e11280d619e30e980e79222eca49680bb7ba504 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Thu, 2 Dec 2021 13:56:13 +0000 Subject: [PATCH 1/2] DEV: Update plugin to use ManagedAuthenticator --- ...11202134547_migrate_office365_user_info.rb | 30 ++++++++++++++ .../omniauth-microsoft365.rb | 0 plugin.rb | 41 ++----------------- 3 files changed, 34 insertions(+), 37 deletions(-) create mode 100644 db/migrate/20211202134547_migrate_office365_user_info.rb rename omniauth-microsoft365.rb => lib/omniauth-microsoft365.rb (100%) diff --git a/db/migrate/20211202134547_migrate_office365_user_info.rb b/db/migrate/20211202134547_migrate_office365_user_info.rb new file mode 100644 index 0000000..b081e66 --- /dev/null +++ b/db/migrate/20211202134547_migrate_office365_user_info.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +class MigrateFacebookUserInfo < ActiveRecord::Migration[6.1] + def up + execute <<~SQL + INSERT INTO user_associated_accounts ( + provider_name, + provider_uid, + user_id, + info, + last_used, + created_at, + updated_at + ) SELECT + 'microsoft_office365', + uid, + user_id, + json_build_object('email', email, 'name', name), + updated_at, + created_at, + updated_at + FROM oauth2_user_infos + WHERE provider = 'microsoft_office365' + SQL + end + + def down + raise ActiveRecord::IrreversibleMigration + end +end diff --git a/omniauth-microsoft365.rb b/lib/omniauth-microsoft365.rb similarity index 100% rename from omniauth-microsoft365.rb rename to lib/omniauth-microsoft365.rb diff --git a/plugin.rb b/plugin.rb index 6469b6e..a088065 100644 --- a/plugin.rb +++ b/plugin.rb @@ -2,37 +2,19 @@ # name: discourse-plugin-office365-auth # about: Enable Login via Office365 -# version: 0.0.1 +# version: 1.0 # authors: Matthew Wilkin # url: https://github.com/discourse/discourse-plugin-office365-auth -require 'auth/oauth2_authenticator' -require File.expand_path('../omniauth-microsoft365.rb', __FILE__) +require_relative "lib/omniauth-microsoft365" enabled_site_setting :office365_enabled -class Office365Authenticator < ::Auth::OAuth2Authenticator - PLUGIN_NAME = 'oauth-office365' - +class ::Office365Authenticator < ::Auth::ManagedAuthenticator def name 'microsoft_office365' end - def after_authenticate(auth_token) - result = super - - if result.user && result.email && (result.user.email != result.email) - begin - result.user.primary_email.update!(email: result.email) - rescue - used_by = User.find_by_email(result.email)&.username - Rails.loger.warn("FAILED to update email for #{user.username} to #{result.email} cause it is in use by #{used_by}") - end - end - - result - end - def register_middleware(omniauth) omniauth.provider :microsoft_office365, setup: lambda { |env| @@ -47,19 +29,4 @@ def enabled? end end -auth_provider enabled_setting: "office365_enabled", - frame_width: 920, - frame_height: 800, - authenticator: Office365Authenticator.new( - 'microsoft_office365', - trusted: true, - auto_create_account: true - ) - -register_css < Date: Thu, 2 Dec 2021 14:20:15 +0000 Subject: [PATCH 2/2] UX: Rename from Office365 to 'Microsoft Auth' This rename avoids changing any externally-facing APIs, so no manual configuration changes are required --- config/locales/client.en.yml | 5 ++-- config/locales/server.en.yml | 6 ++--- config/settings.yml | 6 ++--- ...11202134547_migrate_office365_user_info.rb | 2 +- ...202140128_rename_office365_to_microsoft.rb | 23 +++++++++++++++++++ ...1030_remove_old_office365_site_settings.rb | 10 ++++++++ plugin.rb | 21 +++++++++-------- 7 files changed, 54 insertions(+), 19 deletions(-) create mode 100644 db/migrate/20211202140128_rename_office365_to_microsoft.rb create mode 100644 db/post_migrate/20211202141030_remove_old_office365_site_settings.rb diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 34d6bb0..d973ce7 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -2,6 +2,5 @@ en: js: login: microsoft_office365: - name: "Office 365" - title: "with Office 365" - message: "Log in via Office 365" + name: "Microsoft" + title: "with Microsoft" diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 9a13896..33880a4 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -1,5 +1,5 @@ en: site_settings: - office365_enabled: 'Allow users to authenticate using Office365?' - office365_client_id: 'Office365 App ID/Client Id (need one? visit https://apps.dev.microsoft.com/#/appList)' - office365_secret: 'Office365 Secret Password' + microsoft_auth_enabled: 'Allow users to authenticate using Microsoft?' + microsoft_auth_client_id: 'Microsoft App ID/Client Id (need one? visit https://apps.dev.microsoft.com/#/appList)' + microsoft_auth_client_secret: 'Microsoft Secret Password' diff --git a/config/settings.yml b/config/settings.yml index c983fba..2a317ab 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -1,10 +1,10 @@ plugins: - office365_enabled: + microsoft_auth_enabled: client: true default: false - office365_client_id: + microsoft_auth_client_id: client: false default: '' - office365_secret: + microsoft_auth_client_secret: client: false default: '' \ No newline at end of file diff --git a/db/migrate/20211202134547_migrate_office365_user_info.rb b/db/migrate/20211202134547_migrate_office365_user_info.rb index b081e66..820913b 100644 --- a/db/migrate/20211202134547_migrate_office365_user_info.rb +++ b/db/migrate/20211202134547_migrate_office365_user_info.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -class MigrateFacebookUserInfo < ActiveRecord::Migration[6.1] +class MigrateOffice365UserInfo < ActiveRecord::Migration[6.1] def up execute <<~SQL INSERT INTO user_associated_accounts ( diff --git a/db/migrate/20211202140128_rename_office365_to_microsoft.rb b/db/migrate/20211202140128_rename_office365_to_microsoft.rb new file mode 100644 index 0000000..4a6a1c4 --- /dev/null +++ b/db/migrate/20211202140128_rename_office365_to_microsoft.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true +class RenameOffice365ToMicrosoft < ActiveRecord::Migration[6.1] + CHANGES = [ + ["office365_enabled", "microsoft_auth_enabled"], + ["office365_client_id", "microsoft_auth_client_id"], + ["office365_secret", "microsoft_auth_client_secret"], + ] + + def up + CHANGES.each do |old, new| + DB.exec(<<~SQL, old_name: old, new_name: new) + INSERT INTO site_settings (name, data_type, value, created_at, updated_at) + SELECT :new_name, data_type, value, created_at, updated_at + FROM site_settings + WHERE name = :old_name + SQL + end + end + + def down + raise ActiveRecord::IrreversibleMigration + end +end diff --git a/db/post_migrate/20211202141030_remove_old_office365_site_settings.rb b/db/post_migrate/20211202141030_remove_old_office365_site_settings.rb new file mode 100644 index 0000000..1e3aa10 --- /dev/null +++ b/db/post_migrate/20211202141030_remove_old_office365_site_settings.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true +class RemoveOldOffice365SiteSettings < ActiveRecord::Migration[6.1] + def up + execute "DELETE FROM site_settings WHERE name IN ('office365_enabled', 'office365_client_id', 'office365_secret')" + end + + def down + raise ActiveRecord::IrreversibleMigration + end +end diff --git a/plugin.rb b/plugin.rb index a088065..1476492 100644 --- a/plugin.rb +++ b/plugin.rb @@ -1,16 +1,18 @@ # frozen_string_literal: true -# name: discourse-plugin-office365-auth -# about: Enable Login via Office365 +# name: discourse-microsoft-auth +# about: Enable Login via Microsoft Identity Platform # version: 1.0 # authors: Matthew Wilkin -# url: https://github.com/discourse/discourse-plugin-office365-auth +# url: https://github.com/discourse/discourse-microsoft-auth require_relative "lib/omniauth-microsoft365" -enabled_site_setting :office365_enabled +enabled_site_setting :microsoft_auth_enabled -class ::Office365Authenticator < ::Auth::ManagedAuthenticator +register_svg_icon "fab-microsoft" + +class ::MicrosoftAuthenticator < ::Auth::ManagedAuthenticator def name 'microsoft_office365' end @@ -19,14 +21,15 @@ def register_middleware(omniauth) omniauth.provider :microsoft_office365, setup: lambda { |env| strategy = env['omniauth.strategy'] - strategy.options[:client_id] = SiteSetting.office365_client_id - strategy.options[:client_secret] = SiteSetting.office365_secret + strategy.options[:client_id] = SiteSetting.microsoft_auth_client_id + strategy.options[:client_secret] = SiteSetting.microsoft_auth_client_secret } end def enabled? - SiteSetting.office365_enabled + SiteSetting.microsoft_auth_enabled end end -auth_provider authenticator: Office365Authenticator.new +auth_provider authenticator: MicrosoftAuthenticator.new, + icon: "fab-microsoft"