Commit 84abea58119bbd9a20fb93b41267aee012f5504f
Exists in
theme-brasil-digital-from-staging
and in
9 other branches
Merge branch 'oauth_rails3' into stable
Showing
24 changed files
with
608 additions
and
1 deletions
Show diff stats
app/views/account/_signup_form.html.erb
@@ -16,7 +16,7 @@ | @@ -16,7 +16,7 @@ | ||
16 | <input type="hidden" id="signup_time_key" name="signup_time_key" /> | 16 | <input type="hidden" id="signup_time_key" name="signup_time_key" /> |
17 | <script type="text/javascript"> | 17 | <script type="text/javascript"> |
18 | jQuery.ajax({ | 18 | jQuery.ajax({ |
19 | - type: "POST", | 19 | + type: "GET", |
20 | url: "<%= url_for :controller=>'account', :action=>'signup_time' %>", | 20 | url: "<%= url_for :controller=>'account', :action=>'signup_time' %>", |
21 | dataType: 'json', | 21 | dataType: 'json', |
22 | success: function(data) { | 22 | success: function(data) { |
@@ -0,0 +1,11 @@ | @@ -0,0 +1,11 @@ | ||
1 | + | ||
2 | + | ||
3 | +https://github.com/mkdynamic/omniauth-facebook | ||
4 | +https://github.com/zquestz/omniauth-google-oauth2 | ||
5 | + | ||
6 | +Create Google+ application: | ||
7 | + https://developers.google.com/+/web/signin/javascript-flow | ||
8 | + | ||
9 | +Create Facebook application: | ||
10 | + https://developers.facebook.com/docs/facebook-login/v2.1 | ||
11 | + https://developers.facebook.com/docs/reference/dialogs/oauth |
plugins/oauth_client/controllers/oauth_client_plugin_admin_controller.rb
0 → 100644
@@ -0,0 +1,15 @@ | @@ -0,0 +1,15 @@ | ||
1 | +class OauthClientPluginAdminController < AdminController | ||
2 | + | ||
3 | + def index | ||
4 | + settings = params[:settings] || {} | ||
5 | + | ||
6 | + @settings = Noosfero::Plugin::Settings.new(environment, OauthClientPlugin, settings) | ||
7 | + @providers = @settings.get_setting(:providers) || {} | ||
8 | + if request.post? | ||
9 | + @settings.save! | ||
10 | + session[:notice] = 'Settings succefully saved.' | ||
11 | + redirect_to :action => 'index' | ||
12 | + end | ||
13 | + end | ||
14 | + | ||
15 | +end |
plugins/oauth_client/controllers/public/oauth_client_plugin_public_controller.rb
0 → 100644
@@ -0,0 +1,28 @@ | @@ -0,0 +1,28 @@ | ||
1 | +class OauthClientPluginPublicController < PublicController | ||
2 | + | ||
3 | + def callback | ||
4 | + auth = request.env["omniauth.auth"] | ||
5 | + login = auth.info.email.split('@').first | ||
6 | + user = environment.users.find_with_omniauth(auth) | ||
7 | + | ||
8 | + if user | ||
9 | + session[:user] = user | ||
10 | + redirect_to :controller => :account, :action => :login | ||
11 | + else | ||
12 | + session[:oauth_data] = auth | ||
13 | + name = auth.info.name | ||
14 | + name ||= auth.extra && auth.extra.raw_info ? auth.extra.raw_info.name : '' | ||
15 | + redirect_to :controller => :account, :action => :signup, :user => {:login => login, :email => auth.info.email}, :profile_data => {:name => name} | ||
16 | + end | ||
17 | + end | ||
18 | + | ||
19 | + def failure | ||
20 | + redirect_to root_url | ||
21 | + end | ||
22 | + | ||
23 | + def destroy | ||
24 | + session[:user] = nil | ||
25 | + redirect_to root_url | ||
26 | + end | ||
27 | + | ||
28 | +end |
plugins/oauth_client/db/migrate/20140828184930_add_settings_to_users.rb
0 → 100644
@@ -0,0 +1,36 @@ | @@ -0,0 +1,36 @@ | ||
1 | +require_dependency 'user' | ||
2 | + | ||
3 | +class User | ||
4 | + | ||
5 | + acts_as_having_settings :field => :settings | ||
6 | + | ||
7 | + settings_items :oauth_providers, :type => Array, :default => [] | ||
8 | + | ||
9 | + def self.find_with_omniauth(auth) | ||
10 | + user = self.find_by_email(auth.info.email) | ||
11 | + if user && !user.oauth_providers.empty? #FIXME save new oauth providers | ||
12 | + user | ||
13 | + else | ||
14 | + nil | ||
15 | + end | ||
16 | + end | ||
17 | + | ||
18 | + def password_required_with_oauth? | ||
19 | + password_required_without_oauth? && oauth_providers.blank? | ||
20 | + end | ||
21 | + | ||
22 | + alias_method_chain :password_required?, :oauth | ||
23 | + | ||
24 | + after_create :activate_oauth_user | ||
25 | + | ||
26 | + def activate_oauth_user | ||
27 | + activate unless oauth_providers.empty? | ||
28 | + end | ||
29 | + | ||
30 | + def make_activation_code_with_oauth | ||
31 | + oauth_providers.blank? ? make_activation_code_without_oauth : nil | ||
32 | + end | ||
33 | + | ||
34 | + alias_method_chain :make_activation_code, :oauth | ||
35 | + | ||
36 | +end |
@@ -0,0 +1,93 @@ | @@ -0,0 +1,93 @@ | ||
1 | +require 'omniauth/strategies/noosfero_oauth2' | ||
2 | + | ||
3 | +class OauthClientPlugin < Noosfero::Plugin | ||
4 | + | ||
5 | + def self.plugin_name | ||
6 | + "Oauth Client Plugin" | ||
7 | + end | ||
8 | + | ||
9 | + def self.plugin_description | ||
10 | + _("Login with Oauth.") | ||
11 | + end | ||
12 | + | ||
13 | + def login_extra_contents | ||
14 | + plugin = self | ||
15 | + proc do | ||
16 | + render :partial => 'auth/oauth_login', :locals => {:providers => plugin.enabled_providers} | ||
17 | + end | ||
18 | + end | ||
19 | + | ||
20 | + def signup_extra_contents | ||
21 | + plugin = self | ||
22 | + | ||
23 | + proc do | ||
24 | + if plugin.context.session[:oauth_data].present? | ||
25 | + render :partial => 'account/oauth_signup' | ||
26 | + else | ||
27 | + '' | ||
28 | + end | ||
29 | + end | ||
30 | + end | ||
31 | + | ||
32 | + def enabled_providers | ||
33 | + settings = Noosfero::Plugin::Settings.new(context.environment, OauthClientPlugin) | ||
34 | + providers = settings.get_setting(:providers) | ||
35 | + providers.select {|provider, options| options[:enabled]} | ||
36 | + end | ||
37 | + | ||
38 | + PROVIDERS = { | ||
39 | + :facebook => { | ||
40 | + :name => 'Facebook' | ||
41 | + }, | ||
42 | + :google_oauth2 => { | ||
43 | + :name => 'Google' | ||
44 | + }, | ||
45 | + :noosfero_oauth2 => { | ||
46 | + :name => 'Noosfero' | ||
47 | + } | ||
48 | + } | ||
49 | + | ||
50 | + def stylesheet? | ||
51 | + true | ||
52 | + end | ||
53 | + | ||
54 | + OmniAuth.config.on_failure = OauthClientPluginPublicController.action(:failure) | ||
55 | + | ||
56 | + Rails.application.config.middleware.use OmniAuth::Builder do | ||
57 | + PROVIDERS.each do |provider, options| | ||
58 | + provider provider, :setup => lambda { |env| | ||
59 | + request = Rack::Request.new env | ||
60 | + strategy = env['omniauth.strategy'] | ||
61 | + | ||
62 | + domain = Domain.find_by_name(request.host) | ||
63 | + environment = domain.environment rescue Environment.default | ||
64 | + settings = Noosfero::Plugin::Settings.new(environment, OauthClientPlugin) | ||
65 | + providers = settings.get_setting(:providers) | ||
66 | + | ||
67 | + strategy.options.client_id = providers[provider][:client_id] | ||
68 | + strategy.options.client_secret = providers[provider][:client_secret] | ||
69 | + }, :path_prefix => '/plugin/oauth_client', :callback_path => "/plugin/oauth_client/public/callback/#{provider}" | ||
70 | + end | ||
71 | + | ||
72 | + unless Rails.env.production? | ||
73 | + provider :developer, :path_prefix => "/plugin/oauth_client", :callback_path => "/plugin/oauth_client/public/callback/developer" | ||
74 | + end | ||
75 | + end | ||
76 | + | ||
77 | + def account_controller_filters | ||
78 | + { | ||
79 | + :type => 'before_filter', :method_name => 'signup', | ||
80 | + :block => proc { | ||
81 | + auth = session[:oauth_data] | ||
82 | + | ||
83 | + if auth.present? && params[:user].present? | ||
84 | + params[:user][:oauth_providers] = [{:provider => auth.provider, :uid => auth.uid}] | ||
85 | + if request.post? && auth.info.email != params[:user][:email] | ||
86 | + raise "Wrong email for oauth signup" | ||
87 | + end | ||
88 | + end | ||
89 | + } | ||
90 | + } | ||
91 | + end | ||
92 | + | ||
93 | +end |
plugins/oauth_client/lib/omniauth/strategies/noosfero_oauth2.rb
0 → 100644
@@ -0,0 +1,30 @@ | @@ -0,0 +1,30 @@ | ||
1 | +require 'omniauth/strategies/oauth2' | ||
2 | + | ||
3 | +module OmniAuth | ||
4 | + module Strategies | ||
5 | + class NoosferoOauth2 < OmniAuth::Strategies::OAuth2 | ||
6 | + option :name, :noosfero_oauth2 | ||
7 | + | ||
8 | + option :client_options, { | ||
9 | + :site => "http://noosfero.com:3001", | ||
10 | + :authorize_url => "/oauth/authorize" | ||
11 | + } | ||
12 | + | ||
13 | + uid { raw_info["id"] } | ||
14 | + | ||
15 | + info do | ||
16 | + { | ||
17 | + :email => raw_info["email"] | ||
18 | + # and anything else you want to return to your API consumers | ||
19 | + } | ||
20 | + end | ||
21 | + | ||
22 | + def raw_info | ||
23 | + #@raw_info ||= access_token.get('/api/v1/me.json').parsed | ||
24 | + #FIXME | ||
25 | + #raise access_token.inspect | ||
26 | + User['vfcosta'].attributes | ||
27 | + end | ||
28 | + end | ||
29 | + end | ||
30 | +end |
831 Bytes
1.58 KB
@@ -0,0 +1,22 @@ | @@ -0,0 +1,22 @@ | ||
1 | +.oauth-login .provider a { | ||
2 | + min-width: 20px; | ||
3 | + min-height: 20px; | ||
4 | + background-size: 20px; | ||
5 | + display: inline-block; | ||
6 | + text-decoration: none; | ||
7 | + background-repeat: no-repeat; | ||
8 | + padding-left: 22px; | ||
9 | + line-height: 20px; | ||
10 | +} | ||
11 | + | ||
12 | +.oauth-login .provider .facebook { | ||
13 | + background-image: url(images/facebook-icon.png); | ||
14 | +} | ||
15 | + | ||
16 | +.oauth-login .provider .google_oauth2 { | ||
17 | + background-image: url(images/google-icon.png); | ||
18 | +} | ||
19 | + | ||
20 | +.oauth-login .provider .developer { | ||
21 | + display: none; | ||
22 | +} |
@@ -0,0 +1 @@ | @@ -0,0 +1 @@ | ||
1 | +require File.dirname(__FILE__) + '/../../../test/test_helper' |
plugins/oauth_client/test/unit/oauth_client_plugin_test.rb
0 → 100644
@@ -0,0 +1,86 @@ | @@ -0,0 +1,86 @@ | ||
1 | +require File.dirname(__FILE__) + '/../test_helper' | ||
2 | + | ||
3 | +class OauthClientPluginTest < ActiveSupport::TestCase | ||
4 | + | ||
5 | + def setup | ||
6 | + @plugin = OauthClientPlugin.new | ||
7 | + @params = {} | ||
8 | + @plugin.stubs(:context).returns(self) | ||
9 | + @environment = Environment.default | ||
10 | + end | ||
11 | + | ||
12 | + attr_reader :params, :plugin, :environment | ||
13 | + | ||
14 | + should 'has extra contents for login' do | ||
15 | + assert plugin.login_extra_contents | ||
16 | + end | ||
17 | + | ||
18 | + should 'has no signup extra contents if no provider was enabled' do | ||
19 | + assert_equal '', instance_eval(&plugin.signup_extra_contents) | ||
20 | + end | ||
21 | + | ||
22 | + should 'has signup extra contents if there is enabled providers' do | ||
23 | + params[:user] = {:oauth_providers => [:provider]} | ||
24 | + expects(:render).with(:partial => 'account/oauth_signup').once | ||
25 | + instance_eval(&plugin.signup_extra_contents) | ||
26 | + end | ||
27 | + | ||
28 | + should 'list enabled providers' do | ||
29 | + settings = Noosfero::Plugin::Settings.new(environment, OauthClientPlugin) | ||
30 | + providers = {:test => {:enabled => true}, :test2 => {:enabled => false}} | ||
31 | + settings.set_setting(:providers, providers) | ||
32 | + assert_equal({:test => {:enabled => true}}, plugin.enabled_providers) | ||
33 | + end | ||
34 | + | ||
35 | + should 'define before filter for account controller' do | ||
36 | + assert plugin.account_controller_filters | ||
37 | + end | ||
38 | + | ||
39 | + should 'raise error if oauth email was changed' do | ||
40 | + request = mock | ||
41 | + stubs(:request).returns(request) | ||
42 | + request.expects(:post?).returns(true) | ||
43 | + | ||
44 | + oauth_data = mock | ||
45 | + info = mock | ||
46 | + oauth_data.stubs(:info).returns(info) | ||
47 | + info.stubs(:email).returns('test@example.com') | ||
48 | + stubs(:session).returns({:oauth_data => oauth_data}) | ||
49 | + | ||
50 | + params[:user] = {:email => 'test2@example.com'} | ||
51 | + assert_raises RuntimeError do | ||
52 | + instance_eval(&plugin.account_controller_filters[:block]) | ||
53 | + end | ||
54 | + end | ||
55 | + | ||
56 | + should 'do not raise error if oauth email was not changed' do | ||
57 | + request = mock | ||
58 | + stubs(:request).returns(request) | ||
59 | + request.expects(:post?).returns(true) | ||
60 | + | ||
61 | + oauth_data = mock | ||
62 | + info = mock | ||
63 | + oauth_data.stubs(:info).returns(info) | ||
64 | + info.stubs(:email).returns('test@example.com') | ||
65 | + stubs(:session).returns({:oauth_data => oauth_data}) | ||
66 | + | ||
67 | + params[:user] = {:email => 'test@example.com'} | ||
68 | + instance_eval(&plugin.account_controller_filters[:block]) | ||
69 | + end | ||
70 | + | ||
71 | + should 'do not raise error if oauth session is not set' do | ||
72 | + request = mock | ||
73 | + stubs(:request).returns(request) | ||
74 | + request.expects(:post?).returns(true) | ||
75 | + stubs(:session).returns({}) | ||
76 | + instance_eval(&plugin.account_controller_filters[:block]) | ||
77 | + end | ||
78 | + | ||
79 | + should 'do not raise error if it is not a post' do | ||
80 | + request = mock | ||
81 | + stubs(:request).returns(request) | ||
82 | + request.expects(:post?).returns(false) | ||
83 | + instance_eval(&plugin.account_controller_filters[:block]) | ||
84 | + end | ||
85 | + | ||
86 | +end |
@@ -0,0 +1,49 @@ | @@ -0,0 +1,49 @@ | ||
1 | +require File.dirname(__FILE__) + '/../test_helper' | ||
2 | + | ||
3 | +class UserTest < ActiveSupport::TestCase | ||
4 | + | ||
5 | + should 'find with omniauth params' do | ||
6 | + user = fast_create(User) | ||
7 | + user.settings[:oauth_providers] = [:test => {}] | ||
8 | + user.save! | ||
9 | + auth = {:info => OpenStruct.new({:email => user.email})} | ||
10 | + assert_equal user, User.find_with_omniauth(OpenStruct.new(auth)) | ||
11 | + end | ||
12 | + | ||
13 | + should 'do not return user if there is no provider' do | ||
14 | + user = fast_create(User) | ||
15 | + auth = {:info => OpenStruct.new({:email => user.email})} | ||
16 | + assert_equal nil, User.find_with_omniauth(OpenStruct.new(auth)) | ||
17 | + end | ||
18 | + | ||
19 | + should 'password is not required if there is a oauth provider' do | ||
20 | + User.create!(:email => 'testoauth@example.com', :login => 'testoauth', :oauth_providers => [:test]) | ||
21 | + end | ||
22 | + | ||
23 | + should 'password is required if there is a oauth provider' do | ||
24 | + user = User.new(:email => 'testoauth@example.com', :login => 'testoauth') | ||
25 | + user.save | ||
26 | + assert user.errors[:password].present? | ||
27 | + end | ||
28 | + | ||
29 | + should 'activate user when created with oauth' do | ||
30 | + user = User.create!(:email => 'testoauth@example.com', :login => 'testoauth', :oauth_providers => [:test]) | ||
31 | + assert user.activated? | ||
32 | + end | ||
33 | + | ||
34 | + should 'not activate user when created without oauth' do | ||
35 | + user = fast_create(User) | ||
36 | + assert !user.activated? | ||
37 | + end | ||
38 | + | ||
39 | + should 'not make activation code when created with oauth' do | ||
40 | + user = User.create!(:email => 'testoauth@example.com', :login => 'testoauth', :oauth_providers => [:test]) | ||
41 | + assert !user.activation_code | ||
42 | + end | ||
43 | + | ||
44 | + should 'make activation code when created without oauth' do | ||
45 | + user = User.create!(:email => 'testoauth@example.com', :login => 'testoauth', :password => 'test', :password_confirmation => 'test') | ||
46 | + assert user.activation_code | ||
47 | + end | ||
48 | + | ||
49 | +end |
plugins/oauth_client/views/account/_oauth_signup.html.erb
0 → 100644
@@ -0,0 +1,11 @@ | @@ -0,0 +1,11 @@ | ||
1 | +<%= hidden_field_tag 'return_to', '/' %> | ||
2 | + | ||
3 | +<style> | ||
4 | + #signup-password, #signup-password-confirmation, #signup-email { | ||
5 | + display: none; | ||
6 | + } | ||
7 | +</style> | ||
8 | + | ||
9 | +<div id='signup-email-readonly'> | ||
10 | + <%= labelled_form_field(_('Email'), text_field(:user, :email, :class => "disabled", :readonly => true)) %> | ||
11 | +</div> |
@@ -0,0 +1 @@ | @@ -0,0 +1 @@ | ||
1 | +<a class="facebook" href="/plugin/oauth_client/facebook"><%= _('Login with Facebook') %></a> |
@@ -0,0 +1 @@ | @@ -0,0 +1 @@ | ||
1 | +<a class="google_oauth2" href="/plugin/oauth_client/google_oauth2"><%= _('Login with Google') %></a> |
plugins/oauth_client/views/auth/_noosfero_oauth2.html.erb
0 → 100644
@@ -0,0 +1 @@ | @@ -0,0 +1 @@ | ||
1 | +<a class="noosfero_oauth2" href="/plugin/oauth_client/noosfero_oauth2"><%= _('Login with Noosfero') %></a> |
@@ -0,0 +1,13 @@ | @@ -0,0 +1,13 @@ | ||
1 | +<div class="oauth-login"> | ||
2 | + <% providers.each do |provider, options| %> | ||
3 | + <span class="provider"> | ||
4 | + <%= render :partial => "auth/#{provider}", :locals => {:app_id => options['client_id'] } %> | ||
5 | + </span> | ||
6 | + <% end %> | ||
7 | + | ||
8 | + <span class="provider"> | ||
9 | + <% unless Rails.env.production? %> | ||
10 | + <%= link_to _('Developer Login'), "/plugin/oauth/developer", :class => 'developer' %> | ||
11 | + <% end %> | ||
12 | + </span> | ||
13 | +</div> |
plugins/oauth_client/views/oauth_client_plugin_admin/index.html.erb
0 → 100644
@@ -0,0 +1,44 @@ | @@ -0,0 +1,44 @@ | ||
1 | +<h1><%= _('Oauth Client Settings') %></h1> | ||
2 | + | ||
3 | +<%= form_for(:settings) do |f| %> | ||
4 | + <div class="providers"> | ||
5 | + <h3><%= _('Providers') %></h3> | ||
6 | + <%= f.fields_for :providers, OpenStruct.new(@providers) do |p| %> | ||
7 | + | ||
8 | + <% OauthClientPlugin::PROVIDERS.each do |available_provider, options| %> | ||
9 | + <% provider = OpenStruct.new(@providers[available_provider]) %> | ||
10 | + | ||
11 | + <%= p.fields_for available_provider, provider do |o| %> | ||
12 | + <div class="provider"> | ||
13 | + <div class="name"> | ||
14 | + <h4><%= o.check_box :enabled, {:class => 'enable', :checked => provider.enabled=='true'}, true, false %> | ||
15 | + <%= options[:name] %></h4> | ||
16 | + </div> | ||
17 | + <div class="options" style="<%= provider.enabled=='true' ? '':'display:none' %>"> | ||
18 | + <div class="client-id"> | ||
19 | + <span class="label"><%= _('Client ID') %></span> | ||
20 | + <span class="value"><%= o.text_field :client_id %></span> | ||
21 | + </div> | ||
22 | + <div class="client-secret"> | ||
23 | + <span class="label"><%= _('Client Secret') %></span> | ||
24 | + <span class="value"><%= o.text_field :client_secret %></span> | ||
25 | + </div> | ||
26 | + </div> | ||
27 | + </div> | ||
28 | + <% end %> | ||
29 | + <% end %> | ||
30 | + <% end %> | ||
31 | + | ||
32 | + <% button_bar do %> | ||
33 | + <%= submit_button(:save, _('Save'), :cancel => {:controller => 'plugins', :action => 'index'}) %> | ||
34 | + <% end %> | ||
35 | + </div> | ||
36 | +<% end %> | ||
37 | + | ||
38 | +<script> | ||
39 | + jQuery(document).ready(function($) { | ||
40 | + $('.providers .provider .enable').on('click', function() { | ||
41 | + $(this).parents('.provider').find('.options').toggle('fast'); | ||
42 | + }); | ||
43 | + }); | ||
44 | +</script> |
@@ -0,0 +1 @@ | @@ -0,0 +1 @@ | ||
1 | +gem 'doorkeeper' |
plugins/oauth_provider/db/migrate/20140829153047_create_doorkeeper_tables.rb
0 → 100644
@@ -0,0 +1,41 @@ | @@ -0,0 +1,41 @@ | ||
1 | +class CreateDoorkeeperTables < ActiveRecord::Migration | ||
2 | + def change | ||
3 | + create_table :oauth_applications do |t| | ||
4 | + t.string :name, null: false | ||
5 | + t.string :uid, null: false | ||
6 | + t.string :secret, null: false | ||
7 | + t.text :redirect_uri, null: false | ||
8 | + t.timestamps | ||
9 | + end | ||
10 | + | ||
11 | + add_index :oauth_applications, :uid, unique: true | ||
12 | + | ||
13 | + create_table :oauth_access_grants do |t| | ||
14 | + t.integer :resource_owner_id, null: false | ||
15 | + t.integer :application_id, null: false | ||
16 | + t.string :token, null: false | ||
17 | + t.integer :expires_in, null: false | ||
18 | + t.text :redirect_uri, null: false | ||
19 | + t.datetime :created_at, null: false | ||
20 | + t.datetime :revoked_at | ||
21 | + t.string :scopes | ||
22 | + end | ||
23 | + | ||
24 | + add_index :oauth_access_grants, :token, unique: true | ||
25 | + | ||
26 | + create_table :oauth_access_tokens do |t| | ||
27 | + t.integer :resource_owner_id | ||
28 | + t.integer :application_id | ||
29 | + t.string :token, null: false | ||
30 | + t.string :refresh_token | ||
31 | + t.integer :expires_in | ||
32 | + t.datetime :revoked_at | ||
33 | + t.datetime :created_at, null: false | ||
34 | + t.string :scopes | ||
35 | + end | ||
36 | + | ||
37 | + add_index :oauth_access_tokens, :token, unique: true | ||
38 | + add_index :oauth_access_tokens, :resource_owner_id | ||
39 | + add_index :oauth_access_tokens, :refresh_token, unique: true | ||
40 | + end | ||
41 | +end |
@@ -0,0 +1,111 @@ | @@ -0,0 +1,111 @@ | ||
1 | +class OauthProviderPlugin < Noosfero::Plugin | ||
2 | + | ||
3 | + def self.plugin_name | ||
4 | + "Oauth Provider Plugin" | ||
5 | + end | ||
6 | + | ||
7 | + def self.plugin_description | ||
8 | + _("Oauth Provider.") | ||
9 | + end | ||
10 | + | ||
11 | + Doorkeeper.configure do | ||
12 | + # Change the ORM that doorkeeper will use. | ||
13 | + # Currently supported options are :active_record, :mongoid2, :mongoid3, :mongo_mapper | ||
14 | + orm :active_record | ||
15 | + | ||
16 | + # This block will be called to check whether the resource owner is authenticated or not. | ||
17 | + resource_owner_authenticator do | ||
18 | + domain = Domain.find_by_name(request.host) | ||
19 | + environment = domain ? domain.environment : Environment.default | ||
20 | + environment.users.find_by_id(session[:user]) || redirect_to('/account/login') | ||
21 | + end | ||
22 | + | ||
23 | + # If you want to restrict access to the web interface for adding oauth authorized applications, you need to declare the block below. | ||
24 | + admin_authenticator do | ||
25 | + domain = Domain.find_by_name(request.host) | ||
26 | + environment = domain ? domain.environment : Environment.default | ||
27 | + user = environment.users.find_by_id(session[:user]) | ||
28 | + unless user && user.person.is_admin?(environment) | ||
29 | + redirect_to('/account/login') | ||
30 | + end | ||
31 | + user | ||
32 | + end | ||
33 | + | ||
34 | + # Authorization Code expiration time (default 10 minutes). | ||
35 | + # authorization_code_expires_in 10.minutes | ||
36 | + | ||
37 | + # Access token expiration time (default 2 hours). | ||
38 | + # If you want to disable expiration, set this to nil. | ||
39 | + # access_token_expires_in 2.hours | ||
40 | + | ||
41 | + # Reuse access token for the same resource owner within an application (disabled by default) | ||
42 | + # Rationale: https://github.com/doorkeeper-gem/doorkeeper/issues/383 | ||
43 | + # reuse_access_token | ||
44 | + | ||
45 | + # Issue access tokens with refresh token (disabled by default) | ||
46 | + # use_refresh_token | ||
47 | + | ||
48 | + # Provide support for an owner to be assigned to each registered application (disabled by default) | ||
49 | + # Optional parameter :confirmation => true (default false) if you want to enforce ownership of | ||
50 | + # a registered application | ||
51 | + # Note: you must also run the rails g doorkeeper:application_owner generator to provide the necessary support | ||
52 | + # enable_application_owner :confirmation => false | ||
53 | + | ||
54 | + # Define access token scopes for your provider | ||
55 | + # For more information go to | ||
56 | + # https://github.com/doorkeeper-gem/doorkeeper/wiki/Using-Scopes | ||
57 | + # default_scopes :public | ||
58 | + # optional_scopes :write, :update | ||
59 | + | ||
60 | + # Change the way client credentials are retrieved from the request object. | ||
61 | + # By default it retrieves first from the `HTTP_AUTHORIZATION` header, then | ||
62 | + # falls back to the `:client_id` and `:client_secret` params from the `params` object. | ||
63 | + # Check out the wiki for more information on customization | ||
64 | + # client_credentials :from_basic, :from_params | ||
65 | + | ||
66 | + # Change the way access token is authenticated from the request object. | ||
67 | + # By default it retrieves first from the `HTTP_AUTHORIZATION` header, then | ||
68 | + # falls back to the `:access_token` or `:bearer_token` params from the `params` object. | ||
69 | + # Check out the wiki for more information on customization | ||
70 | + # access_token_methods :from_bearer_authorization, :from_access_token_param, :from_bearer_param | ||
71 | + | ||
72 | + # Change the native redirect uri for client apps | ||
73 | + # When clients register with the following redirect uri, they won't be redirected to any server and the authorization code will be displayed within the provider | ||
74 | + # The value can be any string. Use nil to disable this feature. When disabled, clients must provide a valid URL | ||
75 | + # (Similar behaviour: https://developers.google.com/accounts/docs/OAuth2InstalledApp#choosingredirecturi) | ||
76 | + # | ||
77 | + # native_redirect_uri 'urn:ietf:wg:oauth:2.0:oob' | ||
78 | + | ||
79 | + # Specify what grant flows are enabled in array of Strings. The valid | ||
80 | + # strings and the flows they enable are: | ||
81 | + # | ||
82 | + # "authorization_code" => Authorization Code Grant Flow | ||
83 | + # "implicit" => Implicit Grant Flow | ||
84 | + # "password" => Resource Owner Password Credentials Grant Flow | ||
85 | + # "client_credentials" => Client Credentials Grant Flow | ||
86 | + # | ||
87 | + # If not specified, Doorkeeper enables all the four grant flows. | ||
88 | + # | ||
89 | + # grant_flows %w(authorization_code implicit password client_credentials) | ||
90 | + | ||
91 | + # Under some circumstances you might want to have applications auto-approved, | ||
92 | + # so that the user skips the authorization step. | ||
93 | + # For example if dealing with trusted a application. | ||
94 | + # skip_authorization do |resource_owner, client| | ||
95 | + # client.superapp? or resource_owner.admin? | ||
96 | + # end | ||
97 | + | ||
98 | + # WWW-Authenticate Realm (default "Doorkeeper"). | ||
99 | + # realm "Doorkeeper" | ||
100 | + | ||
101 | + # Allow dynamic query parameters (disabled by default) | ||
102 | + # Some applications require dynamic query parameters on their request_uri | ||
103 | + # set to true if you want this to be allowed | ||
104 | + # wildcard_redirect_uri false | ||
105 | + end | ||
106 | + | ||
107 | + Rails.application.routes.prepend do | ||
108 | + use_doorkeeper | ||
109 | + end | ||
110 | + | ||
111 | +end |