forked from discourse/discourse-azure-ad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.rb
77 lines (62 loc) · 2.4 KB
/
plugin.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# name: discourse-azure-ad
# about: Microsoft Azure Active Directory OAuth support for Discourse
# version: 0.1
# authors: Neil Lalonde
# url: https://github.com/discourse/discourse-azure-ad
require_dependency 'auth/oauth2_authenticator'
gem 'omniauth-azure-oauth2', '0.0.10'
class AzureOAuth2Authenticator < ::Auth::OAuth2Authenticator
def register_middleware(omniauth)
if enabled?
omniauth.provider :azure_oauth2,
:name => 'azure_oauth2',
:tenant_id => GlobalSetting.try(:azure_tenant_id) || "common",
:client_id => GlobalSetting.azure_client_id,
:client_secret => GlobalSetting.azure_client_secret
end
end
def enabled?
if defined?(GlobalSetting.azure_client_id) && defined?(GlobalSetting.azure_client_secret)
!GlobalSetting.azure_client_id.blank? && !GlobalSetting.azure_client_secret.blank?
end
end
def after_authenticate(auth)
result = Auth::Result.new
if info = auth['info'].present?
email = auth['info']['email']
if email.present?
result.email = email
result.email_valid = true
end
result.name = auth['info']['name']
end
current_info = ::PluginStore.get("azure_oauth2", "azure_oauth2_user_#{auth['uid']}")
if current_info
result.user = User.where(id: current_info[:user_id]).first
elsif result.email_valid && (user = User.find_by_email(result.email))
result.user = user
plugin_store_azure_user auth['uid'], user.id
end
result.extra_data = { azure_user_id: auth['uid'] }
result
end
def after_create_account(user, auth)
plugin_store_azure_user auth[:extra_data][:azure_user_id], user.id
end
def plugin_store_azure_user(azure_user_id, discourse_user_id)
::PluginStore.set("azure_oauth2", "azure_oauth2_user_#{azure_user_id}", {user_id: discourse_user_id })
end
end
title = GlobalSetting.try(:azure_title) || "Azure AD"
button_title = GlobalSetting.try(:azure_title) || "with Azure AD"
auth_provider :title => button_title,
:authenticator => AzureOAuth2Authenticator.new('azure_oauth2'),
:message => "Authorizing with #{title} (make sure pop up blockers are not enabled)",
:frame_width => 725,
:frame_height => 500,
:background_color => '#71B1D1'
register_css <<CSS
.btn-social.azure_oauth2 {
background: #71B1D1;
}
CSS