oauth_client_plugin.rb
1.93 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
class OauthClientPlugin < Noosfero::Plugin
def self.plugin_name
"Oauth Client Plugin"
end
def self.plugin_description
_("Login with Oauth.")
end
def login_extra_contents
plugin = self
proc do
render :partial => 'auth/oauth_login', :locals => {:providers => plugin.enabled_providers}
end
end
def signup_extra_contents
plugin = self
proc do
unless (plugin.context.params[:user]||{})[:oauth_providers].blank?
render :partial => 'oauth_signup'
else
''
end
end
end
def enabled_providers
settings = Noosfero::Plugin::Settings.new(context.environment, OauthClientPlugin)
providers = settings.get_setting(:providers)
providers.select {|provider, options| options[:enabled]}
end
PROVIDERS = {
:facebook => {
:name => 'Facebook'
},
:google_oauth2 => {
:name => 'Google'
}
}
def stylesheet?
true
end
OmniAuth.config.on_failure = OauthClientPluginPublicController.action(:failure)
Rails.application.config.middleware.use OmniAuth::Builder do
PROVIDERS.each do |provider, options|
provider provider, :setup => lambda { |env|
request = Rack::Request.new env
strategy = env['omniauth.strategy']
domain = Domain.find_by_name(request.host)
environment = domain.environment rescue Environment.default
settings = Noosfero::Plugin::Settings.new(environment, OauthClientPlugin)
providers = settings.get_setting(:providers)
strategy.options.client_id = providers[provider][:client_id]
strategy.options.client_secret = providers[provider][:client_secret]
}, :path_prefix => '/plugin/oauth_client', :callback_path => "/plugin/oauth_client/public/callback/#{provider}"
end
unless Rails.env.production?
provider :developer, :path_prefix => "/plugin/oauth_client", :callback_path => "/plugin/oauth_client/public/callback/developer"
end
end
end