Skip to content

A platform for community discussion. Free, open, simple.

FIX: redirect to 'private' topic when using discourse connect (#34176)

Régis Hanol1fea84b493a4
diff --git a/app/assets/javascripts/discourse/app/routes/login.js b/app/assets/javascripts/discourse/app/routes/login.js
index 252d1e4d10b..2a10b2aa270 100644
--- a/app/assets/javascripts/discourse/app/routes/login.js
+++ b/app/assets/javascripts/discourse/app/routes/login.js
@@ -47,15 +47,6 @@ export default class extends DiscourseRoute {
postRNWebviewMessage("showLogin", true);
}
- // When Discourse Connect is enabled, redirect to the SSO endpoint
- if (auth_immediately && enable_discourse_connect) {
- const returnPath = cookie("destination_url")
- ? getURL("/")
- : encodeURIComponent(url);
- window.location = getURL(`/session/sso?return_path=${returnPath}`);
- return;
- }
-
// Automatically store the current URL (aka. the one **before** the transition)
if (!currentUser) {
if (isValidDestinationUrl(url)) {
@@ -65,6 +56,15 @@ export default class extends DiscourseRoute {
}
}
+ // When Discourse Connect is enabled, redirect to the SSO endpoint
+ if (auth_immediately && enable_discourse_connect) {
+ const returnPath = cookie("destination_url")
+ ? getURL("/")
+ : encodeURIComponent(url);
+ window.location = getURL(`/session/sso?return_path=${returnPath}`);
+ return;
+ }
+
// Automatically kick off the external login if it's the only one available
if (isOnlyOneExternalLoginMethod) {
if (auth_immediately || login_required || !from || wantsTo) {
diff --git a/app/assets/javascripts/discourse/app/routes/signup.js b/app/assets/javascripts/discourse/app/routes/signup.js
index 9afd3fcc1bb..6b4b4ed7868 100644
--- a/app/assets/javascripts/discourse/app/routes/signup.js
+++ b/app/assets/javascripts/discourse/app/routes/signup.js
@@ -60,15 +60,6 @@ export default class extends DiscourseRoute {
postRNWebviewMessage("showLogin", true);
}
- // When Discourse Connect is enabled, redirect to the SSO endpoint
- if (auth_immediately && enable_discourse_connect) {
- const returnPath = cookie("destination_url")
- ? getURL("/")
- : encodeURIComponent(url);
- window.location = getURL(`/session/sso?return_path=${returnPath}`);
- return;
- }
-
// Automatically store the current URL (aka. the one **before** the transition)
if (!currentUser) {
if (isValidDestinationUrl(url)) {
@@ -78,6 +69,15 @@ export default class extends DiscourseRoute {
}
}
+ // When Discourse Connect is enabled, redirect to the SSO endpoint
+ if (auth_immediately && enable_discourse_connect) {
+ const returnPath = cookie("destination_url")
+ ? getURL("/")
+ : encodeURIComponent(url);
+ window.location = getURL(`/session/sso?return_path=${returnPath}`);
+ return;
+ }
+
// Automatically kick off the external login if it's the only one available
if (isOnlyOneExternalLoginMethod) {
if (auth_immediately || login_required || !from || wantsTo) {
diff --git a/spec/system/discourse_connect_spec.rb b/spec/system/discourse_connect_spec.rb
new file mode 100644
index 00000000000..1c05e8f5116
--- /dev/null
+++ b/spec/system/discourse_connect_spec.rb
@@ -0,0 +1,84 @@
+# frozen_string_literal: true
+
+require "webrick"
+
+describe "Discourse Connect", type: :system do
+ let(:sso_secret) { SecureRandom.alphanumeric(32) }
+ let(:sso_port) { 9876 }
+ let(:sso_url) { "http://localhost:#{sso_port}/sso" }
+
+ fab!(:user)
+ fab!(:private_group) { Fabricate(:group, users: [user]) }
+ fab!(:private_category) { Fabricate(:private_category, group: private_group) }
+ fab!(:private_topic) { Fabricate(:topic, category: private_category) }
+ fab!(:private_post) { Fabricate(:post, topic: private_topic) }
+
+ before do
+ setup_test_sso_server
+ configure_discourse_connect
+ end
+
+ after { shutdown_test_sso_server }
+
+ context "when auth_immediately is enabled" do
+ before { SiteSetting.auth_immediately = true }
+
+ it "redirects the user back to the landing URL" do
+ visit private_topic.url
+
+ find(".login-button").click
+
+ wait_for { has_css?("#current-user") }
+
+ expect(page).to have_current_path(private_topic.relative_url)
+ end
+ end
+
+ private
+
+ def setup_test_sso_server
+ @server =
+ WEBrick::HTTPServer.new(
+ Port: sso_port,
+ Logger: WEBrick::Log.new(File.open(File::NULL, "w")),
+ AccessLog: [],
+ )
+
+ @server.mount_proc "/sso" do |req, res|
+ decoded = Base64.decode64(req.query["sso"])
+ params = Rack::Utils.parse_query(decoded)
+
+ response_sso = DiscourseConnectBase.new
+ response_sso.nonce = params["nonce"]
+ response_sso.sso_secret = sso_secret
+ response_sso.external_id = "foo-bar"
+ response_sso.email = user.email
+ response_sso.username = user.username
+
+ res.status = 302
+ res["Location"] = "#{params["return_sso_url"]}?#{response_sso.payload}"
+ end
+
+ @server_thread = Thread.new { @server.start }
+
+ sleep 0.1 until server_responding?
+ end
+
+ def shutdown_test_sso_server
+ @server&.shutdown
+ @server_thread&.kill
+ end
+
+ def server_responding?
+ Net::HTTP.get_response(URI(sso_url))
+ true
+ rescue StandardError
+ false
+ end
+
+ def configure_discourse_connect
+ SiteSetting.discourse_connect_url = sso_url
+ SiteSetting.discourse_connect_secret = sso_secret
+ SiteSetting.enable_discourse_connect = true
+ end
+end