diff --git a/lib/noosfero/api/api.rb b/lib/noosfero/api/api.rb index 2d5fd60..427d11b 100644 --- a/lib/noosfero/api/api.rb +++ b/lib/noosfero/api/api.rb @@ -11,9 +11,11 @@ module Noosfero logger.formatter = GrapeLogging::Formatters::Default.new use GrapeLogging::Middleware::RequestLogger, { logger: logger } - #rescue_from :all do |e| - # logger.error e - #end + rescue_from :all do |e| + #puts e.inspect + #puts e.backtrace.inspect + #logger.error e + end @@NOOSFERO_CONF = nil def self.NOOSFERO_CONF diff --git a/lib/noosfero/api/session.rb b/lib/noosfero/api/session.rb index acef657..a3cd4cb 100644 --- a/lib/noosfero/api/session.rb +++ b/lib/noosfero/api/session.rb @@ -33,8 +33,8 @@ module Noosfero params do requires :email, type: String, desc: _("Email") requires :login, type: String, desc: _("Login") - requires :password, type: String, desc: _("Password") - requires :password_confirmation, type: String, desc: _("Password confirmation") + #requires :password, type: String, desc: _("Password") + #requires :password_confirmation, type: String, desc: _("Password confirmation") end post "/register" do attrs = attributes_for_keys [:email, :login, :password, :password_confirmation] + environment.signup_person_fields @@ -47,7 +47,6 @@ module Noosfero user = User.new(attrs) if user.save - user.activate user.generate_private_token! present user, :with => Entities::UserLogin else diff --git a/plugins/oauth_client/controllers/public/oauth_client_plugin_public_controller.rb b/plugins/oauth_client/controllers/public/oauth_client_plugin_public_controller.rb index 4c86282..4f6af06 100644 --- a/plugins/oauth_client/controllers/public/oauth_client_plugin_public_controller.rb +++ b/plugins/oauth_client/controllers/public/oauth_client_plugin_public_controller.rb @@ -53,8 +53,9 @@ class OauthClientPluginPublicController < PublicController # reading provider from session and writing to cache to read when # api calls register to confirm signup - provider = OauthClientPlugin::Provider.find(session[:provider_id]) - OauthClientPlugin.write_cache(auth.info.email, provider.id, auth.uid) + auth_cach_hash = auth.to_hash + auth_cach_hash[:provider_id] = session[:provider_id] + signup_token = OauthClientPlugin::SignupDataStore.store_oauth_data(auth.info.email, auth_cach_hash) session[:oauth_data] = auth session[:oauth_client_popup] = true if request.env.fetch("omniauth.params", {})['oauth_client_popup'] @@ -63,7 +64,16 @@ class OauthClientPluginPublicController < PublicController name ||= auth.extra && auth.extra.raw_info ? auth.extra.raw_info.name : '' if session[:oauth_client_popup] - redirect_to :controller => :oauth_client_plugin_public, :action => :finish, :user => {:login => login, :email => auth.info.email, :oauth_providers => [session[:provider_id]]}, :profile_data => {:name => name}, :oauth_client_popup => session[:oauth_client_popup] + redirect_to :controller => :oauth_client_plugin_public, + :action => :finish, + :user => { + :signup_token => signup_token, + :login => login, + :email => auth.info.email, + :oauth_providers => [session[:provider_id]] + }, + :profile_data => {:name => name}, + :oauth_client_popup => session[:oauth_client_popup] else redirect_to :controller => :account, :action => :signup, :user => {:login => login, :email => auth.info.email}, :profile_data => {:name => name} end diff --git a/plugins/oauth_client/db/migrate/20150714200000_add_oauth_auth_fields_to_user_provider.rb b/plugins/oauth_client/db/migrate/20150714200000_add_oauth_auth_fields_to_user_provider.rb index c54e8dc..6ce58b7 100644 --- a/plugins/oauth_client/db/migrate/20150714200000_add_oauth_auth_fields_to_user_provider.rb +++ b/plugins/oauth_client/db/migrate/20150714200000_add_oauth_auth_fields_to_user_provider.rb @@ -1,16 +1,12 @@ -class AddOAuthAuthFieldsToUserProvider < ActiveRecord::Migration +class AddOauthAuthFieldsToUserProvider < ActiveRecord::Migration def self.up change_table :oauth_client_plugin_user_providers do |t| - t.string :token - t.boolean :expires - t.datetime :expiration_date + t.text :oauth_data end end def self.down - remove_column :oauth_client_plugin_user_providers, :token - remove_column :oauth_client_plugin_user_providers, :expires - remove_column :oauth_client_plugin_user_providers, :expiration_date + remove_column :oauth_client_plugin_user_providers, :oauth_data end end diff --git a/plugins/oauth_client/lib/ext/environment.rb b/plugins/oauth_client/lib/ext/environment.rb index 725221e..2507b7b 100644 --- a/plugins/oauth_client/lib/ext/environment.rb +++ b/plugins/oauth_client/lib/ext/environment.rb @@ -4,4 +4,10 @@ class Environment has_many :oauth_providers, :class_name => 'OauthClientPlugin::Provider' + def signup_person_fields_with_oauth + signup_person_fields_without_oauth + [:oauth_signup_token] + end + + alias_method_chain :signup_person_fields, :oauth + end diff --git a/plugins/oauth_client/lib/ext/user.rb b/plugins/oauth_client/lib/ext/user.rb index 468203a..334ffa8 100644 --- a/plugins/oauth_client/lib/ext/user.rb +++ b/plugins/oauth_client/lib/ext/user.rb @@ -6,34 +6,59 @@ class User has_many :oauth_providers, :through => :oauth_user_providers, :source => :provider def password_required_with_oauth? + # user creation through api does not set oauth_providers + check_providers password_required_without_oauth? && oauth_providers.empty? end + def oauth_data + @oauth_data + end + + def oauth_signup_token= value + @oauth_signup_token = value + end + + def oauth_signup_token + @oauth_signup_token + end + alias_method_chain :password_required?, :oauth after_create :activate_oauth_user - def activate_oauth_user - # user creation through api does not set oauth_providers - if oauth_providers.empty? + # user creation through api does not set oauth_providers + # so it is being shared through a distributed cache + def check_providers + if @call_count + @call_count +=1 + else + @call_count = 0 + end + if oauth_providers.empty? && oauth_signup_token.present? #check if is oauth user, reading oauth_data recorded at cache store - oauth_data = OauthClientPlugin.read_cache_for(self.email) - if oauth_data - oauth_providers = [OauthClientPlugin::Provider.find(oauth_data[:provider])] - OauthClientPlugin.delete_cache_for(self.email) + @oauth_data = OauthClientPlugin::SignupDataStore.get_oauth_data(self.email, self.oauth_signup_token) + if @oauth_data + provider_id = @oauth_data.delete(:provider_id) + self.oauth_providers = [OauthClientPlugin::Provider.find(provider_id)] end end + end - unless oauth_providers.empty? - activate - oauth_providers.each do |provider| - OauthClientPlugin::UserProvider.create!(:user => self, :provider => provider, :enabled => true) + def activate_oauth_user + self.oauth_providers.each do |provider| + OauthClientPlugin::UserProvider.create! do |user_provider| + user_provider.user = self + user_provider.provider = provider + user_provider.enabled = true + user_provider.oauth_data = oauth_data end end + activate unless oauth_providers.empty? end def make_activation_code_with_oauth - oauth_providers.blank? ? make_activation_code_without_oauth : nil + self.oauth_providers.blank? ? make_activation_code_without_oauth : nil end alias_method_chain :make_activation_code, :oauth diff --git a/plugins/oauth_client/lib/oauth_client_plugin.rb b/plugins/oauth_client/lib/oauth_client_plugin.rb index 0cba29c..2b86e62 100644 --- a/plugins/oauth_client/lib/oauth_client_plugin.rb +++ b/plugins/oauth_client/lib/oauth_client_plugin.rb @@ -10,35 +10,6 @@ class OauthClientPlugin < Noosfero::Plugin _("Login with Oauth.") end - def self.cache_prefix - 'CACHE_OAUTH_CLIENT_AUTH' - end - - def self.cache_name_for email - "#{cache_prefix}_#{email}" - end - - def self.read_cache_for email - if cache_value = Rails.cache.fetch(cache_name_for(email)) - if cache_value.include?('-') - cache_arr = cache_value.split('-') - return { - provider: cache_arr[0], - uid: cache_arr[1] - } - end - end - end - - def self.write_cache email, provider, uid - Rails.cache.write(cache_name_for(email), "#{provider}-#{uid}" , :expires_in => 300) - end - - def self.delete_cache_for email - Rails.cache.delete(cache_name_for(email)) - end - - def login_extra_contents plugin = self proc do diff --git a/plugins/oauth_client/lib/oauth_client_plugin/signup_data_store.rb b/plugins/oauth_client/lib/oauth_client_plugin/signup_data_store.rb new file mode 100644 index 0000000..543e309 --- /dev/null +++ b/plugins/oauth_client/lib/oauth_client_plugin/signup_data_store.rb @@ -0,0 +1,34 @@ +# A Distributed Cache Store is needed +# to save oauth autenthication to be +# used on OAUTH flow using the Noosfero REST API. +# Because of the nature session less of api implementation +# When using more than one server is strongly recomended +# provide your Rails application with a distributed Cache Store, +# otherwise you will have to rely on client/server affinify provided by +# network infrastructure +class OauthClientPlugin::SignupDataStore + + def self.key_name_for email, signup_token + "#{email}_#{signup_token}" + end + + def self.get_oauth_data email, signup_token + key_name = key_name_for(email, signup_token) + puts "OAUTH_KEY_NAME :::: #{key_name}" + oauth_data = Rails.cache.fetch(key_name) + Rails.cache.delete(key_name) + oauth_data + end + + def self.store_oauth_data email, auth_obj + signup_token = SecureRandom.hex + Rails.cache.write(key_name_for(email, signup_token), auth_obj, :expires_in => 300) + signup_token + end + + def self.delete_cache_for email + Rails.cache.delete(cache_name_for(email)) + end + + +end diff --git a/plugins/oauth_client/lib/oauth_client_plugin/user_provider.rb b/plugins/oauth_client/lib/oauth_client_plugin/user_provider.rb index 77b2bda..365b1c9 100644 --- a/plugins/oauth_client/lib/oauth_client_plugin/user_provider.rb +++ b/plugins/oauth_client/lib/oauth_client_plugin/user_provider.rb @@ -7,4 +7,5 @@ class OauthClientPlugin::UserProvider < Noosfero::Plugin::ActiveRecord attr_accessible :user, :provider, :enabled + acts_as_having_settings :field => :oauth_data end diff --git a/plugins/oauth_client/test/unit/user_test.rb b/plugins/oauth_client/test/unit/user_test.rb index 8393657..27016fc 100644 --- a/plugins/oauth_client/test/unit/user_test.rb +++ b/plugins/oauth_client/test/unit/user_test.rb @@ -37,6 +37,11 @@ class UserTest < ActiveSupport::TestCase assert user.activation_code end + should 'not send activation email when created with oauth' do + UserMailer.expects(:activation_code).never + user = User.create!(:email => 'testoauth@example.com', :login => 'testoauth', :oauth_providers => [provider]) + end + should 'save oauth token when create with oauth' do end -- libgit2 0.21.2