Commit ddc5125d8d3ec0a5dcd8087ec806e95ee515bff6
1 parent
9e014471
Refactors old plugin behavior
Signed-off-by: Gabriel Silva <gabriel93.silva@gmail.com>
Showing
4 changed files
with
83 additions
and
57 deletions
Show diff stats
plugins/oauth_client/controllers/public/oauth_client_plugin_public_controller.rb
| @@ -3,14 +3,14 @@ class OauthClientPluginPublicController < PublicController | @@ -3,14 +3,14 @@ class OauthClientPluginPublicController < PublicController | ||
| 3 | skip_before_filter :login_required | 3 | skip_before_filter :login_required |
| 4 | 4 | ||
| 5 | def callback | 5 | def callback |
| 6 | - auth = request.env["omniauth.auth"] | ||
| 7 | - | 6 | + auth_data = request.env["omniauth.auth"] |
| 8 | oauth_params = request.env["omniauth.params"] | 7 | oauth_params = request.env["omniauth.params"] |
| 8 | + | ||
| 9 | if oauth_params && oauth_params["action"] == "external_login" | 9 | if oauth_params && oauth_params["action"] == "external_login" |
| 10 | - external_person_login(auth) | 10 | + external_person_login(auth_data) |
| 11 | else | 11 | else |
| 12 | - auth_user = environment.users.where(email: auth.info.email).first | ||
| 13 | - if auth_user then login(auth_user.person) else signup(auth) end | 12 | + auth_user = environment.users.where(email: auth_data.info.email).first |
| 13 | + if auth_user then login(auth_user.person) else signup(auth_data) end | ||
| 14 | end | 14 | end |
| 15 | end | 15 | end |
| 16 | 16 | ||
| @@ -26,48 +26,45 @@ class OauthClientPluginPublicController < PublicController | @@ -26,48 +26,45 @@ class OauthClientPluginPublicController < PublicController | ||
| 26 | 26 | ||
| 27 | protected | 27 | protected |
| 28 | 28 | ||
| 29 | - def external_person_login(auth) | 29 | + def external_person_login(auth_data) |
| 30 | provider = OauthClientPlugin::Provider.find(session[:provider_id]) | 30 | provider = OauthClientPlugin::Provider.find(session[:provider_id]) |
| 31 | 31 | ||
| 32 | - if provider.enabled? | ||
| 33 | - user = User.new(email: auth.info.email, login: auth.info.name.to_slug) | ||
| 34 | - webfinger = OpenStruct.new( | ||
| 35 | - identifier: auth.info.nickname || user.login, | ||
| 36 | - name: auth.info.name, | ||
| 37 | - created_at: Time.now, | ||
| 38 | - domain: provider.site || auth.provider, | ||
| 39 | - email: user.email) | ||
| 40 | - person = ExternalPerson.get_or_create(webfinger) | ||
| 41 | - user.external_person_id = person.id | 32 | + user = User.new(email: auth_data.info.email, login: auth_data.info.name.to_slug) |
| 33 | + person_data = OpenStruct.new( | ||
| 34 | + identifier: auth_data.info.nickname || user.login, | ||
| 35 | + name: auth_data.info.name, | ||
| 36 | + created_at: Time.now, | ||
| 37 | + domain: provider.site || auth_data.provider, | ||
| 38 | + email: user.email) | ||
| 39 | + person = ExternalPerson.get_or_create(person_data) | ||
| 40 | + user.external_person_id = person.id | ||
| 42 | 41 | ||
| 43 | - oauth_auth = person.oauth_auth | ||
| 44 | - if oauth_auth.nil? | ||
| 45 | - auth_data = { profile: person, provider: provider, enabled: true, | ||
| 46 | - external_person_uid: auth.uid, external_person_image_url: auth.info.image } | ||
| 47 | - oauth_auth = OauthClientPlugin::Auth.create_for_strategy(provider.strategy, auth_data) | ||
| 48 | - end | ||
| 49 | - self.current_user = user if oauth_auth.enabled? | ||
| 50 | - else | ||
| 51 | - session[:notice] = _("Can't login with %s") % provider.name | ||
| 52 | - end | 42 | + oauth_auth = person.oauth_auth |
| 43 | + oauth_data = { profile: person, provider: provider, enabled: true, | ||
| 44 | + external_person_uid: auth_data.uid, external_person_image_url: auth_data.info.image } | ||
| 45 | + oauth_auth ||= OauthClientPlugin::Auth.create_for_strategy(provider.strategy, oauth_data) | ||
| 46 | + create_session(user, oauth_auth) | ||
| 47 | + end | ||
| 53 | 48 | ||
| 54 | - redirect_to :controller => :account, :action => :login | 49 | + def signup(auth_data) |
| 50 | + session[:oauth_data] = auth_data | ||
| 51 | + username = auth_data.info.email.split('@').first | ||
| 52 | + name = auth_data.info.name | ||
| 53 | + name ||= auth_data.extra && auth_data.extra.raw_info ? auth_data.extra.raw_info.name : '' | ||
| 54 | + redirect_to :controller => :account, :action => :signup, :user => {:login => username, :email => auth_data.info.email}, :profile_data => {:name => name} | ||
| 55 | end | 55 | end |
| 56 | 56 | ||
| 57 | - def signup(auth) | ||
| 58 | - login = auth.info.email.split('@').first | ||
| 59 | - session[:oauth_data] = auth | ||
| 60 | - name = auth.info.name | ||
| 61 | - name ||= auth.extra && auth.extra.raw_info ? auth.extra.raw_info.name : '' | ||
| 62 | - redirect_to :controller => :account, :action => :signup, :user => {:login => login, :email => auth.info.email}, :profile_data => {:name => name} | 57 | + def login(person) |
| 58 | + auth = person.oauth_auths.find_or_create_by(profile: person, | ||
| 59 | + provider_id: session[:provider_id]) | ||
| 60 | + create_session(person.user, auth) | ||
| 63 | end | 61 | end |
| 64 | 62 | ||
| 65 | - def login person | 63 | + def create_session(user, oauth_auth) |
| 66 | provider = OauthClientPlugin::Provider.find(session[:provider_id]) | 64 | provider = OauthClientPlugin::Provider.find(session[:provider_id]) |
| 67 | - auth = person.oauth_auths.where(provider_id: provider.id).first | ||
| 68 | - auth ||= person.oauth_auths.create! profile: person, provider: provider, enabled: true | ||
| 69 | - if auth.enabled? && provider.enabled? | ||
| 70 | - self.current_user = person.user | 65 | + |
| 66 | + if oauth_auth.allow_login? | ||
| 67 | + self.current_user = user | ||
| 71 | else | 68 | else |
| 72 | session[:notice] = _("Can't login with %s") % provider.name | 69 | session[:notice] = _("Can't login with %s") % provider.name |
| 73 | end | 70 | end |
plugins/oauth_client/db/migrate/20160720165808_add_external_profile_to_oauth_auth.rb
| 1 | class AddExternalProfileToOauthAuth < ActiveRecord::Migration | 1 | class AddExternalProfileToOauthAuth < ActiveRecord::Migration |
| 2 | def up | 2 | def up |
| 3 | add_column :oauth_client_plugin_auths, :profile_type, :string | 3 | add_column :oauth_client_plugin_auths, :profile_type, :string |
| 4 | + add_index :oauth_client_plugin_auths, :profile_type | ||
| 5 | + | ||
| 4 | add_column :oauth_client_plugin_auths, :external_person_uid, :string | 6 | add_column :oauth_client_plugin_auths, :external_person_uid, :string |
| 5 | add_column :oauth_client_plugin_auths, :external_person_image_url, :string | 7 | add_column :oauth_client_plugin_auths, :external_person_image_url, :string |
| 6 | - add_index :oauth_client_plugin_auths, :profile_type | 8 | + |
| 9 | + change_column_default :oauth_client_plugin_auths, :enabled, true | ||
| 7 | end | 10 | end |
| 8 | 11 | ||
| 9 | def down | 12 | def down |
| 10 | remove_index :oauth_client_plugin_auths, :profile_type | 13 | remove_index :oauth_client_plugin_auths, :profile_type |
| 11 | remove_column :oauth_client_plugin_auths, :profile_type | 14 | remove_column :oauth_client_plugin_auths, :profile_type |
| 15 | + | ||
| 12 | remove_column :oauth_client_plugin_auths, :external_person_uid | 16 | remove_column :oauth_client_plugin_auths, :external_person_uid |
| 13 | remove_column :oauth_client_plugin_auths, :external_person_image_url | 17 | remove_column :oauth_client_plugin_auths, :external_person_image_url |
| 18 | + | ||
| 19 | + change_column_default :oauth_client_plugin_auths, :enabled, nil | ||
| 14 | end | 20 | end |
| 15 | end | 21 | end |
plugins/oauth_client/models/oauth_client_plugin/auth.rb
| 1 | class OauthClientPlugin::Auth < ApplicationRecord | 1 | class OauthClientPlugin::Auth < ApplicationRecord |
| 2 | 2 | ||
| 3 | - attr_accessible :profile, :provider, :enabled, :access_token, | ||
| 4 | - :expires_in, :type, :external_person_uid, | 3 | + attr_accessible :profile, :provider, :provider_id, :enabled, |
| 4 | + :access_token, :expires_in, :type, :external_person_uid, | ||
| 5 | :external_person_image_url | 5 | :external_person_image_url |
| 6 | 6 | ||
| 7 | belongs_to :profile, polymorphic: true | 7 | belongs_to :profile, polymorphic: true |
| @@ -27,6 +27,10 @@ class OauthClientPlugin::Auth < ApplicationRecord | @@ -27,6 +27,10 @@ class OauthClientPlugin::Auth < ApplicationRecord | ||
| 27 | not self.expired? | 27 | not self.expired? |
| 28 | end | 28 | end |
| 29 | 29 | ||
| 30 | + def allow_login? | ||
| 31 | + self.enabled? && self.provider.enabled? | ||
| 32 | + end | ||
| 33 | + | ||
| 30 | def self.create_for_strategy(strategy, args = {}) | 34 | def self.create_for_strategy(strategy, args = {}) |
| 31 | namespace = self.name.split("::")[0] | 35 | namespace = self.name.split("::")[0] |
| 32 | class_name = "#{namespace}::#{strategy.camelize}Auth" | 36 | class_name = "#{namespace}::#{strategy.camelize}Auth" |
plugins/oauth_client/test/functional/oauth_client_plugin_public_controller_test.rb
| @@ -25,7 +25,39 @@ class OauthClientPluginPublicControllerTest < ActionController::TestCase | @@ -25,7 +25,39 @@ class OauthClientPluginPublicControllerTest < ActionController::TestCase | ||
| 25 | assert_match /.*\/account\/signup/, @response.redirect_url | 25 | assert_match /.*\/account\/signup/, @response.redirect_url |
| 26 | end | 26 | end |
| 27 | 27 | ||
| 28 | - should 'login using provider when url param is present' do | 28 | + should 'login when user already signed up' do |
| 29 | + create_user(@auth.info.name, email: @auth.info.email) | ||
| 30 | + | ||
| 31 | + get :callback | ||
| 32 | + assert session[:user].present? | ||
| 33 | + end | ||
| 34 | + | ||
| 35 | + should 'not login when user already signed up and the provider is disabled' do | ||
| 36 | + create_user(@auth.info.name, email: @auth.info.email) | ||
| 37 | + provider.update_attribute(:enabled, false) | ||
| 38 | + | ||
| 39 | + get :callback | ||
| 40 | + assert session[:user].nil? | ||
| 41 | + end | ||
| 42 | + | ||
| 43 | + should 'not login when user already signed up and the provider is disabled for him' do | ||
| 44 | + create_user(@auth.info.name, email: @auth.info.email) | ||
| 45 | + OauthClientPlugin::Auth.any_instance.stubs(:enabled?).returns(false) | ||
| 46 | + | ||
| 47 | + get :callback | ||
| 48 | + assert session[:user].nil? | ||
| 49 | + end | ||
| 50 | + | ||
| 51 | + should 'not duplicate oauth_auths when the same provider is used several times' do | ||
| 52 | + user = create_user(@auth.info.name, email: @auth.info.email) | ||
| 53 | + | ||
| 54 | + get :callback | ||
| 55 | + assert_no_difference 'user.oauth_auths.count' do | ||
| 56 | + 3.times { get :callback } | ||
| 57 | + end | ||
| 58 | + end | ||
| 59 | + | ||
| 60 | + should 'perform external login using provider when url param is present' do | ||
| 29 | request.env["omniauth.params"] = {"action" => "external_login"} | 61 | request.env["omniauth.params"] = {"action" => "external_login"} |
| 30 | 62 | ||
| 31 | get :callback | 63 | get :callback |
| @@ -33,7 +65,7 @@ class OauthClientPluginPublicControllerTest < ActionController::TestCase | @@ -33,7 +65,7 @@ class OauthClientPluginPublicControllerTest < ActionController::TestCase | ||
| 33 | assert session[:external].present? | 65 | assert session[:external].present? |
| 34 | end | 66 | end |
| 35 | 67 | ||
| 36 | - should 'not login when the provider is disabled' do | 68 | + should 'not perform external login when the provider is disabled' do |
| 37 | request.env["omniauth.params"] = {"action" => "external_login"} | 69 | request.env["omniauth.params"] = {"action" => "external_login"} |
| 38 | provider.update_attribute(:enabled, false) | 70 | provider.update_attribute(:enabled, false) |
| 39 | 71 | ||
| @@ -42,7 +74,7 @@ class OauthClientPluginPublicControllerTest < ActionController::TestCase | @@ -42,7 +74,7 @@ class OauthClientPluginPublicControllerTest < ActionController::TestCase | ||
| 42 | assert session[:external].nil? | 74 | assert session[:external].nil? |
| 43 | end | 75 | end |
| 44 | 76 | ||
| 45 | - should 'not login when the provider is disabled for a user' do | 77 | + should 'not perform external login when the provider is disabled for a user' do |
| 46 | request.env["omniauth.params"] = {"action" => "external_login"} | 78 | request.env["omniauth.params"] = {"action" => "external_login"} |
| 47 | OauthClientPlugin::GithubAuth.any_instance.stubs(:enabled?).returns(false) | 79 | OauthClientPlugin::GithubAuth.any_instance.stubs(:enabled?).returns(false) |
| 48 | 80 | ||
| @@ -58,17 +90,4 @@ class OauthClientPluginPublicControllerTest < ActionController::TestCase | @@ -58,17 +90,4 @@ class OauthClientPluginPublicControllerTest < ActionController::TestCase | ||
| 58 | external_person = ExternalPerson.find_by(identifier: auth.info.nickname) | 90 | external_person = ExternalPerson.find_by(identifier: auth.info.nickname) |
| 59 | assert_equal provider, external_person.oauth_auth.provider | 91 | assert_equal provider, external_person.oauth_auth.provider |
| 60 | end | 92 | end |
| 61 | - | ||
| 62 | -# should 'do not duplicate relations between an user and a provider when the same provider was used again in a login' do | ||
| 63 | -# user = create_user | ||
| 64 | -# auth.info.stubs(:email).returns(user.email) | ||
| 65 | -# auth.info.stubs(:name).returns(user.name) | ||
| 66 | -# session[:provider_id] = provider.id | ||
| 67 | -# | ||
| 68 | -# get :callback | ||
| 69 | -# assert_no_difference 'user.oauth_auths.count' do | ||
| 70 | -# 3.times { get :callback } | ||
| 71 | -# end | ||
| 72 | -# end | ||
| 73 | -# | ||
| 74 | end | 93 | end |