This repository was archived by the owner on Apr 1, 2021. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathplugin.rb
More file actions
253 lines (201 loc) · 7.8 KB
/
Copy pathplugin.rb
File metadata and controls
253 lines (201 loc) · 7.8 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# frozen_string_literal: true
# name: discourse-patreon-creator
# about: Integration features between Patreon and Discourse for Creators
# version: 1.0
# author: Dan Bosan from Mutations Limited <dan@mutations.ltd>
# url: https://github.com/discourse/discourse-patreon-creator
require 'auth/oauth2_authenticator'
require 'omniauth-oauth2'
enabled_site_setting :patreon_creator_enabled
PLUGIN_NAME = 'discourse-patreon-creator'.freeze
register_svg_icon "fab-patreon" if respond_to?(:register_svg_icon)
after_initialize do
require_dependency 'admin_constraint'
module ::Patreon
PLUGIN_NAME = 'discourse-patreon-creator'.freeze
CREATOR_BYPASS_REGEX = /.*(@patreon\.com|patron1@mutations\.ltd)$/
class Engine < ::Rails::Engine
engine_name PLUGIN_NAME
isolate_namespace Patreon
end
def self.bypass_creator_logic?(email)
CREATOR_BYPASS_REGEX.match?(email)
end
def self.default_image_url
"#{Discourse.base_url}/plugins/discourse-patreon-creator/images/patreon-logomark-color-on-white.png"
end
def self.get(key)
store.get(key)
end
def self.get_user_info_record(email)
get("user_info.#{email}") || {}
end
def self.nsfw_group
@nsfw_group = Group.find_by_name(SiteSetting.patreon_creator_nsfw_group)
end
def self.remove(key)
store.remove(key)
end
def self.save_user_info(user, email, patreon_id, has_nsfw_campaign)
plugin_store_key = "user_info.#{email}"
if user
user.custom_fields["bypass_creator_logic"] = bypass_creator_logic?(email)
user.custom_fields["has_nsfw_campaign"] = has_nsfw_campaign
user.custom_fields["patreon_id"] = patreon_id
user.save_custom_fields
# Remove the temp data from the plugin store
remove(plugin_store_key)
else
# Save data in PluginStore, will be saved to user in
# :user_created call back
user_info_record = get(plugin_store_key) || {}
user_info_record[:has_nsfw_campaign] = has_nsfw_campaign
user_info_record[:patreon_id] = patreon_id
set_user_info_record(email, user_info_record)
end
end
def self.set(key, value)
store.set(key, value)
end
def self.set_user_info_record(email, value)
set("user_info.#{email}", value)
end
def self.store
@store ||= PluginStore.new(PLUGIN_NAME)
end
def self.add_remove_nsfw_group(user, has_nsfw_campaign)
return unless user && nsfw_group
return if bypass_creator_logic?(user.email)
has_nsfw_campaign ? nsfw_group.add(user) : nsfw_group.remove(user)
end
end
[
'../app/jobs/scheduled/patreon_update_tokens.rb',
'../lib/api.rb',
'../lib/tokens.rb'
].each { |path| load File.expand_path(path, __FILE__) }
AdminDashboardData.problem_messages << ::Patreon::Api::ACCESS_TOKEN_INVALID
Discourse::Application.routes.prepend do
mount ::Patreon::Engine, at: '/patreon'
end
class ::OmniAuth::Strategies::Patreon
option :name, 'patreon'
option :client_options,
site: 'https://www.patreon.com',
authorize_url: 'https://www.patreon.com/oauth2/authorize',
token_url: 'https://api.patreon.com/oauth2/token'
option :authorize_params, response_type: 'code'
def custom_build_access_token
verifier = request.params['code']
client.auth_code.get_token(verifier, redirect_uri: options.redirect_uri)
end
alias_method :build_access_token, :custom_build_access_token
uid {
raw_info['data']['id'].to_s
}
info do
{
email: raw_info['data']['attributes']['email'],
name: raw_info['data']['attributes']['full_name'],
access_token: access_token.token,
refresh_token: access_token.refresh_token
}
end
extra do
{
raw_info: raw_info
}
end
def raw_info
@raw_info ||= begin
response = client.request(:get, "https://api.patreon.com/oauth2/api/current_user", headers: {
'Authorization' => "Bearer #{access_token.token}"
}, parse: :json)
campaign_response = begin
client.request(:get, "https://api.patreon.com/oauth2/api/current_user/campaigns", headers: {
'Authorization' => "Bearer #{access_token.token}"
}, parse: :json).parsed
rescue => e
Rails.logger.warn("Error while getting campaign info with error: #{e}.\n\n #{e.backtrace.join("\n")}")
# Return the data in the same format as the API call, just with no campaigns.
{
data: []
}
end
response.parsed.merge({ campaign: campaign_response })
end
end
end
DiscourseEvent.on(:user_created) do |user|
if SiteSetting.patreon_creator_enabled
begin
user_info_record = Patreon.get_user_info_record(user.email)
Patreon.add_remove_nsfw_group(user, user_info_record[:has_nsfw_campaign])
Patreon.save_user_info(
user,
user.email,
user_info_record[:patreon_id],
user_info_record[:has_nsfw_campaign]
)
rescue => e
Rails.logger.warn("Patreon group membership callback failed for new user #{self.id} with error: #{e}.\n\n #{e.backtrace.join("\n")}")
end
end
end
end
# Authentication with Patreon
class OmniAuth::Strategies::Patreon < OmniAuth::Strategies::OAuth2
end
class Auth::PatreonAuthenticator < Auth::OAuth2Authenticator
def register_middleware(omniauth)
omniauth.provider :patreon,
setup: lambda { |env|
strategy = env['omniauth.strategy']
strategy.options[:client_id] = SiteSetting.patreon_creator_client_id
strategy.options[:client_secret] = SiteSetting.patreon_creator_client_secret
strategy.options[:redirect_uri] = "#{Discourse.base_url}/auth/patreon/callback"
strategy.options[:provider_ignores_state] = SiteSetting.patreon_creator_login_ignore_state
}
end
def after_authenticate(auth_token)
result = super
user = result.user
discourse_username = SiteSetting.patreon_creator_discourse_username
if discourse_username.present? && user && user.username == discourse_username
SiteSetting.patreon_creator_access_token = auth_token[:info][:access_token]
SiteSetting.patreon_creator_refresh_token = auth_token[:info][:refresh_token]
end
published_campaign = auth_token[:extra][:raw_info][:campaign][:data].any? do |campaign|
campaign[:attributes][:published_at].present?
end
if published_campaign || Patreon.bypass_creator_logic?(result.email)
has_nsfw_campaign = auth_token[:extra][:raw_info][:campaign][:data].any? do |campaign|
campaign[:attributes][:is_nsfw]
end
Patreon.add_remove_nsfw_group(user, has_nsfw_campaign)
# Save the patreon_id and has_nsfw_campaign to the user in the custom fields
# When there is no user, the data is stored in the PluginStore
# and used in the :user_created event
Patreon.save_user_info(
user,
result.email,
result.extra_data[:uid],
has_nsfw_campaign
)
else
result.failed = true
result.failed_reason = "This forum is for launched Patreon creators only. Visit <a href='https://patreon.com/faq'>patreon.com/faq</a> if you need further help. Thanks!".html_safe
end
result
end
def enabled?
SiteSetting.patreon_creator_login_enabled
end
end
auth_provider pretty_name: 'Patreon',
title: 'with Patreon',
message: 'Authentication with Patreon (make sure pop up blockers are not enabled)',
frame_width: 840,
frame_height: 570,
authenticator: Auth::PatreonAuthenticator.new('patreon', trusted: true),
enabled_setting: 'patreon_creator_login_enabled'