Skip to content
This repository was archived by the owner on Jul 14, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions config/locales/client.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ en:
js:
login:
microsoft_office365:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason for not renaming the microsoft_office365 key?

Copy link
Member Author

@davidtaylorhq davidtaylorhq Dec 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the name of the authenticator is tied to the 'callback url'. If we change it, everyone would have to update their config with microsoft. I don't think it's worth that pain.

name: "Office 365"
title: "with Office 365"
message: "Log in via Office 365"
name: "Microsoft"
title: "with Microsoft"
6 changes: 3 additions & 3 deletions config/locales/server.en.yml
Original file line number Diff line number Diff line change
@@ -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 <a href="https://apps.dev.microsoft.com/#/appList">https://apps.dev.microsoft.com/#/appList</a>)'
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 <a href="https://apps.dev.microsoft.com/#/appList">https://apps.dev.microsoft.com/#/appList</a>)'
microsoft_auth_client_secret: 'Microsoft Secret Password'
6 changes: 3 additions & 3 deletions config/settings.yml
Original file line number Diff line number Diff line change
@@ -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: ''
30 changes: 30 additions & 0 deletions db/migrate/20211202134547_migrate_office365_user_info.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

class MigrateOffice365UserInfo < 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',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

microsoft_office365?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above, we can't change it without a lot of pain

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
23 changes: 23 additions & 0 deletions db/migrate/20211202140128_rename_office365_to_microsoft.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
File renamed without changes.
56 changes: 13 additions & 43 deletions plugin.rb
Original file line number Diff line number Diff line change
@@ -1,65 +1,35 @@
# frozen_string_literal: true

# name: discourse-plugin-office365-auth
# about: Enable Login via Office365
# version: 0.0.1
# 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 'auth/oauth2_authenticator'
require File.expand_path('../omniauth-microsoft365.rb', __FILE__)
require_relative "lib/omniauth-microsoft365"

enabled_site_setting :office365_enabled
enabled_site_setting :microsoft_auth_enabled

class Office365Authenticator < ::Auth::OAuth2Authenticator
PLUGIN_NAME = 'oauth-office365'
register_svg_icon "fab-microsoft"

class ::MicrosoftAuthenticator < ::Auth::ManagedAuthenticator
def name
'microsoft_office365'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the authenticator still called microsoft_office365?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above, we can't change it without a lot of pain

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|
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 enabled_setting: "office365_enabled",
frame_width: 920,
frame_height: 800,
authenticator: Office365Authenticator.new(
'microsoft_office365',
trusted: true,
auto_create_account: true
)

register_css <<CSS
.btn-social.microsoft_office365 {
background: #EB3D01;
}
CSS
auth_provider authenticator: MicrosoftAuthenticator.new,
icon: "fab-microsoft"