forked from decidim/decidim
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix issues with customized Omniauth callback handling
If a 3rd party Decidim authentication module customizes the Omniauth callback flow by extending the OmniauthRegistrationsController, it would cause exceptions/errors due to the assumptions made that the core controller is the only one handling the callback flow.
- Loading branch information
Showing
8 changed files
with
155 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
decidim-dev/spec/controllers/third_party_omniauth_registrations_controller_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
module Decidim::Dev | ||
class OmniauthCallbacksController < Decidim::Devise::OmniauthRegistrationsController | ||
def callback; end | ||
end | ||
end | ||
|
||
describe "ThirdPartyOmniauthRegistrationsController" do | ||
routes { Decidim::Dev::Engine.routes } | ||
|
||
let(:organization) { create(:organization) } | ||
|
||
controller(Decidim::Dev::OmniauthCallbacksController) {} | ||
|
||
before do | ||
request.env["decidim.current_organization"] = organization | ||
request.env["devise.mapping"] = Devise.mappings[:user] | ||
end | ||
|
||
describe "POST callback" do | ||
let(:provider) { "custom" } | ||
let(:uid) { "12345" } | ||
let(:email) { "[email protected]" } | ||
let!(:user) { create(:user, organization:, email:) } | ||
|
||
before do | ||
request.env["omniauth.auth"] = { | ||
provider:, | ||
uid:, | ||
info: { | ||
name: "Custom Auth", | ||
nickname: "custom_auth", | ||
email: | ||
} | ||
} | ||
end | ||
|
||
describe "after_sign_in_path_for" do | ||
subject { controller.after_sign_in_path_for(user) } | ||
|
||
context "when the user is admin who has a pending password change" do | ||
let(:user) { build(:user, :admin, organization:, sign_in_count: 1, password_updated_at: 1.year.ago) } | ||
|
||
it { is_expected.to eq("/change_password") } | ||
end | ||
end | ||
end | ||
end |
79 changes: 79 additions & 0 deletions
79
decidim-dev/spec/requests/third_party_omniauth_callback_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
module Decidim::Dev | ||
# This controller simulates a customized Omniauth callback flow that would be | ||
# used by 3rd party login providers. Such providers may need to customize how | ||
# the login callback is handled based on conditions returned by the Omniauth | ||
# provider. Customizing the Omniauth strategy is not enough when the login | ||
# provider needs access to the Decidim context. | ||
class OmniauthCallbacksController < Decidim::Devise::OmniauthRegistrationsController | ||
def dev_callback | ||
create | ||
end | ||
end | ||
end | ||
|
||
RSpec.describe "Omniauth callback" do | ||
subject { response.body } | ||
|
||
let(:organization) { create(:organization) } | ||
|
||
let(:user) { create(:user, :confirmed, organization:, email: "[email protected]", password: "decidim123456789") } | ||
|
||
let(:oauth_hash) do | ||
{ | ||
provider: "dev", | ||
uid:, | ||
info: { | ||
name: "Custom Auth", | ||
nickname: "custom_auth", | ||
email: | ||
} | ||
} | ||
end | ||
|
||
before do | ||
host! organization.host | ||
end | ||
|
||
describe "POST callback" do | ||
let(:request_path) { "/users/auth/dev/callback" } | ||
|
||
let(:uid) { "12345" } | ||
let(:email) { "[email protected]" } | ||
|
||
context "with a new user" do | ||
it "shows the create an account form" do | ||
get(request_path, env: { "omniauth.auth" => oauth_hash }) | ||
|
||
expect(response).to have_http_status(:ok) | ||
expect(response.body).to include("Create an account") | ||
expect(response.body).to include("Terms of Service") | ||
end | ||
end | ||
|
||
context "with existing user" do | ||
let!(:user) { create(:user, organization:, email:) } | ||
|
||
it "redirects to root" do | ||
get(request_path, env: { "omniauth.auth" => oauth_hash }) | ||
|
||
expect(response).to have_http_status(:redirect) | ||
expect(response).to redirect_to("/") | ||
end | ||
end | ||
|
||
context "when the user is admin with a pending password change" do | ||
let!(:user) { create(:user, :confirmed, :admin, organization:, email:, sign_in_count: 1, password_updated_at: 1.year.ago) } | ||
|
||
it "redirects to the /change_password path" do | ||
get(request_path, env: { "omniauth.auth" => oauth_hash }) | ||
|
||
expect(response).to have_http_status(:redirect) | ||
expect(response).to redirect_to("/change_password") | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters