diff --git a/app/controllers/users/omniauth_callbacks_controller.rb b/app/controllers/users/omniauth_callbacks_controller.rb index 61b4088f..8ea2b9fe 100644 --- a/app/controllers/users/omniauth_callbacks_controller.rb +++ b/app/controllers/users/omniauth_callbacks_controller.rb @@ -2,7 +2,7 @@ module Users class OmniauthCallbacksController < Devise::OmniauthCallbacksController skip_before_filter :verify_authenticity_token - [:novell, :google, :facebook, :twitter].each do |provider| + User.omniauth_providers.each do |provider| define_method(provider) { handle(provider) } end diff --git a/app/views/devise/registrations/edit.html.haml b/app/views/devise/registrations/edit.html.haml index 1ae3355e..c2b89f82 100644 --- a/app/views/devise/registrations/edit.html.haml +++ b/app/views/devise/registrations/edit.html.haml @@ -15,15 +15,15 @@ %br = f.inputs name: 'OpenID' do %h4 - Currently using the following openIDs: + Currently the following openIDs are associated with your account - @openids.each do |openid| - = openid.provider - = openid.email + %li= "#{openid.provider}:#{openid.email}" %br - %h4 - To add an openID to your account using a different email address, sign in with your - openID while logged in to OSEM - = render 'devise/shared/openid' + - if User.omniauth_providers.present? + %h4 + To add an openID with a different email address to your account, sign in with your + openID while logged in to OSEM + = render 'devise/shared/openid' = f.inputs name: 'Account' do = f.input :email, required: false, input_html: {autocomplete: "off"} diff --git a/app/views/devise/registrations/new.html.haml b/app/views/devise/registrations/new.html.haml index c68c958c..8c4af033 100644 --- a/app/views/devise/registrations/new.html.haml +++ b/app/views/devise/registrations/new.html.haml @@ -13,8 +13,9 @@ %br %h4 Already have an account? = render 'devise/shared/links' - .col-md-4 - .well - %h4 Or use your openID + - if resource_class.omniauth_providers.present? + .col-md-4 + .well + %h4 Or use your openID - = render 'devise/shared/openid' + = render 'devise/shared/openid' diff --git a/app/views/devise/sessions/new.html.haml b/app/views/devise/sessions/new.html.haml index c7bbe275..80d78201 100644 --- a/app/views/devise/sessions/new.html.haml +++ b/app/views/devise/sessions/new.html.haml @@ -15,7 +15,8 @@ %br = render "devise/shared/links" - .col-md-4 - .well - %h4 Or use your openID - = render 'devise/shared/openid' + - if resource_class.omniauth_providers.present? + .col-md-4 + .well + %h4 Or use your openID + = render 'devise/shared/openid' diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb index 06d3d126..a71d3cdd 100644 --- a/config/initializers/devise.rb +++ b/config/initializers/devise.rb @@ -8,7 +8,6 @@ Devise.setup do |config| # ==> openIDs configuration # Define the available openID providers that can be used to log in # Pass each provider to User model in :omniauth_providers (for open_id providers use their name) - # Include provider in users/omniauth_callbacks_controller.rb config.omniauth :open_id, name: 'novell', identifier: 'http://www.opensuse.org/openid/user' config.omniauth :google_oauth2, Rails.application.secrets.google_key, Rails.application.secrets.google_secret, { @@ -140,7 +139,7 @@ Devise.setup do |config| # The time you want to timeout the user session without activity. After this # time the user will be asked for credentials again. Default is 30 minutes. # config.timeout_in = 30.minutes - + # If true, expires auth token on session timeout. # config.expire_auth_token_on_timeout = false diff --git a/spec/features/omniauth_spec.rb b/spec/features/omniauth_spec.rb index ba36e45a..89ef91c6 100644 --- a/spec/features/omniauth_spec.rb +++ b/spec/features/omniauth_spec.rb @@ -4,15 +4,15 @@ feature Openid do let!(:participant_role) { create(:participant_role) } let!(:admin_role) { create(:admin_role) } - describe 'sign in with openid' do + shared_examples 'sign in with openid' do - it 'has option to log in with Google account' do + scenario 'has option to log in with Google account' do visit '/accounts/sign_in' expect(page.has_content?('Or use your openID')).to be true expect(page.has_content?('google')).to be true end - it 'signs in *new* user with Google account' do + scenario 'signs in *new* user with Google account' do expected_count_openid = Openid.count + 1 expected_count_user = User.count + 1 visit '/accounts/sign_in' @@ -24,7 +24,7 @@ feature Openid do expect(User.count).to eq(expected_count_user) end - it 'signs in an existing user' do + scenario 'signs in an existing user' do create(:participant, email: 'test-participant-1@google.com') expected_count_openid = Openid.count + 1 expected_count_user = User.count @@ -37,7 +37,7 @@ feature Openid do expect(User.count).to eq(expected_count_user) end - it 'can handle authentication error' do + scenario 'can handle authentication error' do OmniAuth.config.mock_auth[:google] = :invalid_credentials visit '/accounts/sign_in' expect(page.has_content?('Or use your openID')).to be true @@ -45,7 +45,7 @@ feature Openid do expect(flash).to eq("Could not authenticate you from Google because \"Invalid credentials\".") end - it 'adds openid to existing user' do + scenario 'adds openid to existing user' do # Sign in user user = create(:participant, email: 'test-participant-1@google.com') sign_in user @@ -63,7 +63,7 @@ feature Openid do expect(Openid.where(email: 'test-1@gmail.com').first.nil?).to eq(false) end - it 'signs in with openID using the same email as another associated openid' do + scenario 'signs in with openID using the same email as another associated openid' do # Sign in user create(:participant, email: 'test-participant-1@google.com') expected_count_openid = Openid.count + 1 @@ -105,4 +105,10 @@ feature Openid do expect(last_openid.email).to eq('test-1@gmail.com') end end + + describe 'omniauth' do + if User.omniauth_providers.present? + it_behaves_like 'sign in with openid' + end + end end