Commit 80b76bb045b89e5552e1bef767ab1abe6f1f8a22

Authored by Victor Costa
1 parent 6996df35

oauth_provider: added noosfero client

plugins/oauth_client/lib/oauth_client_plugin.rb
  1 +require 'omniauth/strategies/noosfero_oauth2'
  2 +
1 class OauthClientPlugin < Noosfero::Plugin 3 class OauthClientPlugin < Noosfero::Plugin
2 4
3 def self.plugin_name 5 def self.plugin_name
@@ -39,6 +41,9 @@ class OauthClientPlugin &lt; Noosfero::Plugin @@ -39,6 +41,9 @@ class OauthClientPlugin &lt; Noosfero::Plugin
39 }, 41 },
40 :google_oauth2 => { 42 :google_oauth2 => {
41 :name => 'Google' 43 :name => 'Google'
  44 + },
  45 + :noosfero_oauth2 => {
  46 + :name => 'Noosfero'
42 } 47 }
43 } 48 }
44 49
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
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>
plugins/oauth_provider/lib/oauth_provider_plugin.rb
@@ -13,17 +13,22 @@ class OauthProviderPlugin &lt; Noosfero::Plugin @@ -13,17 +13,22 @@ class OauthProviderPlugin &lt; Noosfero::Plugin
13 # Currently supported options are :active_record, :mongoid2, :mongoid3, :mongo_mapper 13 # Currently supported options are :active_record, :mongoid2, :mongoid3, :mongo_mapper
14 orm :active_record 14 orm :active_record
15 15
16 - domain = Domain.find_by_name(request.host)  
17 - environment = domain ? domain.environment : Environment.default  
18 -  
19 # This block will be called to check whether the resource owner is authenticated or not. 16 # This block will be called to check whether the resource owner is authenticated or not.
20 resource_owner_authenticator do 17 resource_owner_authenticator do
  18 + domain = Domain.find_by_name(request.host)
  19 + environment = domain ? domain.environment : Environment.default
21 environment.users.find_by_id(session[:user]) || redirect_to('/account/login') 20 environment.users.find_by_id(session[:user]) || redirect_to('/account/login')
22 end 21 end
23 22
24 # If you want to restrict access to the web interface for adding oauth authorized applications, you need to declare the block below. 23 # If you want to restrict access to the web interface for adding oauth authorized applications, you need to declare the block below.
25 admin_authenticator do 24 admin_authenticator do
26 - environment.users.find_by_id(session[:user]) || redirect_to('/account/login') 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
27 end 32 end
28 33
29 # Authorization Code expiration time (default 10 minutes). 34 # Authorization Code expiration time (default 10 minutes).