From 3a45d3f4a4c838996406fbe0936fd59030ad7ae8 Mon Sep 17 00:00:00 2001 From: Ábner Silva de Oliveira Date: Wed, 15 Jul 2015 09:02:46 -0300 Subject: [PATCH] changed to save oauth data on cache store to use on signup through api --- plugins/oauth_client/controllers/public/oauth_client_plugin_public_controller.rb | 6 ++++++ plugins/oauth_client/db/migrate/20150714200000_add_oauth_auth_fields_to_user_provider.rb | 16 ++++++++++++++++ plugins/oauth_client/lib/ext/user.rb | 10 ++++++++++ plugins/oauth_client/lib/oauth_client_plugin.rb | 29 +++++++++++++++++++++++++++++ plugins/oauth_client/test/unit/user_test.rb | 8 ++++++++ plugins/oauth_client/views/oauth_client_plugin_public/finish.html.erb | 6 ++++++ 6 files changed, 75 insertions(+), 0 deletions(-) create mode 100644 plugins/oauth_client/db/migrate/20150714200000_add_oauth_auth_fields_to_user_provider.rb 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 eaf2f1d..9e196f5 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 @@ -50,6 +50,12 @@ class OauthClientPluginPublicController < PublicController def signup(auth) login = auth.info.email.split('@').first + + # 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) + session[:oauth_data] = auth session[:oauth_client_popup] = true if request.env["omniauth.params"]['oauth_client_popup'] session[:return_to] = url_for(:controller => :oauth_client_plugin_public, :action => :finish) 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 new file mode 100644 index 0000000..c54e8dc --- /dev/null +++ b/plugins/oauth_client/db/migrate/20150714200000_add_oauth_auth_fields_to_user_provider.rb @@ -0,0 +1,16 @@ +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 + 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 + end +end diff --git a/plugins/oauth_client/lib/ext/user.rb b/plugins/oauth_client/lib/ext/user.rb index f2972a4..468203a 100644 --- a/plugins/oauth_client/lib/ext/user.rb +++ b/plugins/oauth_client/lib/ext/user.rb @@ -14,6 +14,16 @@ class User after_create :activate_oauth_user def activate_oauth_user + # user creation through api does not set oauth_providers + if oauth_providers.empty? + #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) + end + end + unless oauth_providers.empty? activate oauth_providers.each do |provider| diff --git a/plugins/oauth_client/lib/oauth_client_plugin.rb b/plugins/oauth_client/lib/oauth_client_plugin.rb index 2b86e62..0cba29c 100644 --- a/plugins/oauth_client/lib/oauth_client_plugin.rb +++ b/plugins/oauth_client/lib/oauth_client_plugin.rb @@ -10,6 +10,35 @@ 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/test/unit/user_test.rb b/plugins/oauth_client/test/unit/user_test.rb index f4bda17..8393657 100644 --- a/plugins/oauth_client/test/unit/user_test.rb +++ b/plugins/oauth_client/test/unit/user_test.rb @@ -37,4 +37,12 @@ class UserTest < ActiveSupport::TestCase assert user.activation_code end + should 'save oauth token when create with oauth' do + + end + + should 'note save oauth token when create with oauth' do + + end + end diff --git a/plugins/oauth_client/views/oauth_client_plugin_public/finish.html.erb b/plugins/oauth_client/views/oauth_client_plugin_public/finish.html.erb index 55dd8dc..73d35c9 100644 --- a/plugins/oauth_client/views/oauth_client_plugin_public/finish.html.erb +++ b/plugins/oauth_client/views/oauth_client_plugin_public/finish.html.erb @@ -1,3 +1,9 @@ +<%# +# The Application caller, in general a noosfero api client, +# sends periodically messages events with the payload +# message requestOauthClientPluginResult to the Popup Window +# and gets notified when the oauth authentication is confirmed +#%>