Commit 3a45d3f4a4c838996406fbe0936fd59030ad7ae8

Authored by Ábner Silva de Oliveira
1 parent 88dd46f0

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
... ... @@ -50,6 +50,12 @@ class OauthClientPluginPublicController < PublicController
50 50  
51 51 def signup(auth)
52 52 login = auth.info.email.split('@').first
  53 +
  54 + # reading provider from session and writing to cache to read when
  55 + # api calls register to confirm signup
  56 + provider = OauthClientPlugin::Provider.find(session[:provider_id])
  57 + OauthClientPlugin.write_cache(auth.info.email, provider.id, auth.uid)
  58 +
53 59 session[:oauth_data] = auth
54 60 session[:oauth_client_popup] = true if request.env["omniauth.params"]['oauth_client_popup']
55 61 session[:return_to] = url_for(:controller => :oauth_client_plugin_public, :action => :finish)
... ...
plugins/oauth_client/db/migrate/20150714200000_add_oauth_auth_fields_to_user_provider.rb 0 → 100644
... ... @@ -0,0 +1,16 @@
  1 +class AddOAuthAuthFieldsToUserProvider < ActiveRecord::Migration
  2 +
  3 + def self.up
  4 + change_table :oauth_client_plugin_user_providers do |t|
  5 + t.string :token
  6 + t.boolean :expires
  7 + t.datetime :expiration_date
  8 + end
  9 + end
  10 +
  11 + def self.down
  12 + remove_column :oauth_client_plugin_user_providers, :token
  13 + remove_column :oauth_client_plugin_user_providers, :expires
  14 + remove_column :oauth_client_plugin_user_providers, :expiration_date
  15 + end
  16 +end
... ...
plugins/oauth_client/lib/ext/user.rb
... ... @@ -14,6 +14,16 @@ class User
14 14 after_create :activate_oauth_user
15 15  
16 16 def activate_oauth_user
  17 + # user creation through api does not set oauth_providers
  18 + if oauth_providers.empty?
  19 + #check if is oauth user, reading oauth_data recorded at cache store
  20 + oauth_data = OauthClientPlugin.read_cache_for(self.email)
  21 + if oauth_data
  22 + oauth_providers = [OauthClientPlugin::Provider.find(oauth_data[:provider])]
  23 + OauthClientPlugin.delete_cache_for(self.email)
  24 + end
  25 + end
  26 +
17 27 unless oauth_providers.empty?
18 28 activate
19 29 oauth_providers.each do |provider|
... ...
plugins/oauth_client/lib/oauth_client_plugin.rb
... ... @@ -10,6 +10,35 @@ class OauthClientPlugin &lt; Noosfero::Plugin
10 10 _("Login with Oauth.")
11 11 end
12 12  
  13 + def self.cache_prefix
  14 + 'CACHE_OAUTH_CLIENT_AUTH'
  15 + end
  16 +
  17 + def self.cache_name_for email
  18 + "#{cache_prefix}_#{email}"
  19 + end
  20 +
  21 + def self.read_cache_for email
  22 + if cache_value = Rails.cache.fetch(cache_name_for(email))
  23 + if cache_value.include?('-')
  24 + cache_arr = cache_value.split('-')
  25 + return {
  26 + provider: cache_arr[0],
  27 + uid: cache_arr[1]
  28 + }
  29 + end
  30 + end
  31 + end
  32 +
  33 + def self.write_cache email, provider, uid
  34 + Rails.cache.write(cache_name_for(email), "#{provider}-#{uid}" , :expires_in => 300)
  35 + end
  36 +
  37 + def self.delete_cache_for email
  38 + Rails.cache.delete(cache_name_for(email))
  39 + end
  40 +
  41 +
13 42 def login_extra_contents
14 43 plugin = self
15 44 proc do
... ...
plugins/oauth_client/test/unit/user_test.rb
... ... @@ -37,4 +37,12 @@ class UserTest &lt; ActiveSupport::TestCase
37 37 assert user.activation_code
38 38 end
39 39  
  40 + should 'save oauth token when create with oauth' do
  41 +
  42 + end
  43 +
  44 + should 'note save oauth token when create with oauth' do
  45 +
  46 + end
  47 +
40 48 end
... ...
plugins/oauth_client/views/oauth_client_plugin_public/finish.html.erb
  1 +<%#
  2 +# The Application caller, in general a noosfero api client,
  3 +# sends periodically messages events with the payload
  4 +# message requestOauthClientPluginResult to the Popup Window
  5 +# and gets notified when the oauth authentication is confirmed
  6 +#%>
1 7 <script>
2 8 window.addEventListener("message", function(ev) {
3 9 if (ev.data.message === "requestOauthClientPluginResult") {
... ...