-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.rb
141 lines (112 loc) · 4.01 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# name: discourse-oauth2-cj
# about: Generic OAuth2 Plugin
# version: 0.3
# authors: Robin Ward
# url: https://github.com/fiddlerwoaroof/discourse-oauth2-basic
require_dependency 'auth/oauth2_authenticator.rb'
enabled_site_setting :oauth2_cj_enabled
class ::OmniAuth::Strategies::Oauth2CJ < ::OmniAuth::Strategies::OAuth2
option :name, 'oauth2_cj'
option :client_options, {
:site => 'https://login.cj.com',
:authorize_url => 'https://login.cj.com/auth',
:token_url => 'https://login.cj.com/token',
}
def build_access_token
options.token_params.merge!(:headers => {'Authorization' => basic_auth_header})
super
end
def basic_auth_header
puts options.to_hash.to_s
"Basic " + Base64.strict_encode64("#{options[:client_id]}:#{options[:client_secret]}")
end
def request_phase
super
end
def authorize_params
super.tap do |params|
%w[scope client_options].each do |v|
if request.params[v]
params[v.to_sym] = request.params[v]
end
end
end
end
uid do
decoded = ::JWT.decode(access_token.token, nil, false).first
decoded["userId"]
end
extra do
fetch_user_details(uid, access_token.token).select {|k| k != "companies"}
end
def fetch_user_details(user_id, access_token)
user_json_url = URI("https://members.cj.com/affapi/oauth/user/#{user_id}")
puts "user json url: #{user_json_url}"
bearer_token = "Bearer #{access_token}"
req = Net::HTTP::Get.new(user_json_url)
req['Authorization'] = bearer_token
res = Net::HTTP.start(user_json_url.hostname, user_json_url.port, :use_ssl => true) do |http|
http.request(req)
end
puts 'user info!!!', res.body
JSON.parse res.body
end
def callback_url
full_host + script_name + callback_path
end
end
OmniAuth.config.add_camelization 'oauth2_cj', 'Oauth2CJ'
class OAuth2CJAuthenticator < ::Auth::OAuth2Authenticator
def register_middleware(omniauth)
omniauth.provider :oauth2_cj,
name: 'oauth2_cj',
setup: lambda { |env|
opts = env['omniauth.strategy'].options
opts[:client_id] = SiteSetting.oauth2_client_id
opts[:client_secret] = SiteSetting.oauth2_client_secret
opts[:provider_ignores_state] = false
}
end
def log(info)
Rails.logger.warn("OAuth2 Debugging: #{info}") if SiteSetting.oauth2_debug_auth
end
def after_authenticate(auth)
log("after_authenticate response: \n\ncreds: #{auth['credentials'].to_hash}\ninfo: #{auth['info'].to_hash}\nextra: #{auth['extra'].to_hash}")
log("result #{auth.to_hash}")
result = Auth::Result.new
email = auth['extra']['emailAddress']
result.email = email
result.username = email.split('@').first
result.email_valid = result.email.present? && SiteSetting.oauth2_email_verified?
log "uid #{auth.uid}"
current_info = ::PluginStore.get("oauth2_cj", "oauth2_cj_user_#{auth.uid}")
if current_info
result.user = User.where(id: current_info[:user_id]).first
result.user&.update!(email: result.email) if SiteSetting.oauth2_overrides_email && result.email
elsif SiteSetting.oauth2_email_verified?
result.user = User.find_by_email(result.email)
if result.user && auth.uid
::PluginStore.set("oauth2_cj", "oauth2_cj_user_#{auth.uid}", user_id: result.user.id)
end
end
result.extra_data = { oauth2_cj_user_id: auth.uid }
result
end
def after_create_account(user, auth)
log("after_create_account:: #{auth} #{auth.to_hash}")
::PluginStore.set("oauth2_cj", "oauth2_cj_user_#{auth.uid}", user_id: user.id)
end
def enabled?
SiteSetting.oauth2_cj_enabled
end
end
auth_provider title_setting: "oauth2_button_title",
enabled_setting: "oauth2_cj_enabled",
authenticator: OAuth2CJAuthenticator.new('oauth2_cj'),
message: "OAuth2 CJ",
full_screen_login_setting: "oauth2_full_screen_login"
register_css <<CSS
button.btn-social.oauth2_cj {
background-color: #00af66;
}
CSS