A platform for community discussion. Free, open, simple.
FEATURE: pass email to external auth on signup (#33559)
Régis Hanol36157496f69c
diff --git a/app/assets/javascripts/discourse/app/lib/preload-store.js b/app/assets/javascripts/discourse/app/lib/preload-store.jsindex 3562b1da6ef..f7cb507e48c 100644--- a/app/assets/javascripts/discourse/app/lib/preload-store.js+++ b/app/assets/javascripts/discourse/app/lib/preload-store.js@@ -40,6 +40,10 @@ export default {return Promise.resolve(null);},+ has(key) {+ return this.data.has(key);+ },+get(key) {return this.data.get(key);},diff --git a/app/assets/javascripts/discourse/app/models/login-method.js b/app/assets/javascripts/discourse/app/models/login-method.jsindex f2df2adc2c2..abce9931ede 100644--- a/app/assets/javascripts/discourse/app/models/login-method.js+++ b/app/assets/javascripts/discourse/app/models/login-method.js@@ -8,7 +8,7 @@ import Site from "discourse/models/site";import { i18n } from "discourse-i18n";export default class LoginMethod extends EmberObject {- static buildPostForm(url) {+ static buildPostForm(url, params = {}) {// Login always happens in an anonymous context, with no CSRF token// So we need to fetch it before sending a POST requestreturn updateCsrfToken().then(() => {@@ -17,10 +17,17 @@ export default class LoginMethod extends EmberObject {form.setAttribute("method", "post");form.setAttribute("action", url);- const input = document.createElement("input");- input.setAttribute("name", "authenticity_token");- input.setAttribute("value", Session.currentProp("csrfToken"));- form.appendChild(input);+ const csrfInput = document.createElement("input");+ csrfInput.setAttribute("name", "authenticity_token");+ csrfInput.setAttribute("value", Session.currentProp("csrfToken"));+ form.appendChild(csrfInput);++ Object.keys(params).forEach((key) => {+ const input = document.createElement("input");+ input.setAttribute("name", key);+ input.setAttribute("value", params[key]);+ form.appendChild(input);+ });document.body.appendChild(form);@@ -57,25 +64,22 @@ export default class LoginMethod extends EmberObject {return Promise.resolve();}- let authUrl = getURL(`/auth/${this.name}`);-if (reconnect) {- params["reconnect"] = true;+ params.reconnect = true;}if (signup) {- params["signup"] = true;- }+ params.signup = true;- const paramKeys = Object.keys(params);- if (paramKeys.length > 0) {- authUrl += "?";- authUrl += paramKeys- .map((k) => `${encodeURIComponent(k)}=${encodeURIComponent(params[k])}`)- .join("&");+ const email = Session.currentProp("email");+ if (email) {+ params.email = email;+ }}- return LoginMethod.buildPostForm(authUrl).then((form) => form.submit());+ return LoginMethod.buildPostForm(getURL(`/auth/${this.name}`), params).then(+ (form) => form.submit()+ );}}diff --git a/app/assets/javascripts/discourse/tests/acceptance/create-account-test.js b/app/assets/javascripts/discourse/tests/acceptance/create-account-test.jsindex 327bcf7a755..a1f1db8c124 100644--- a/app/assets/javascripts/discourse/tests/acceptance/create-account-test.js+++ b/app/assets/javascripts/discourse/tests/acceptance/create-account-test.js@@ -2,6 +2,7 @@ import { click, fillIn, visit } from "@ember/test-helpers";import { test } from "qunit";import sinon from "sinon";import LoginMethod from "discourse/models/login-method";+import Session from "discourse/models/session";import Site from "discourse/models/site";import pretender, {parsePostData,@@ -56,9 +57,7 @@ acceptance("Create Account", function () {});test("validate username", async function (assert) {- await visit("/");- await click("header .sign-up-button");-+ await visit("/signup");await fillIn("#new-account-email", "[email protected]");await click(".signup-fullpage .btn-primary");@@ -68,8 +67,7 @@ acceptance("Create Account", function () {});test("hidden instructions", async function (assert) {- await visit("/");- await click("header .sign-up-button");+ await visit("/signup");assert.dom("#account-email-validation-more-info")@@ -84,8 +82,7 @@ acceptance("Create Account", function () {this.siteSettings.show_signup_form_password_instructions = true;this.siteSettings.show_signup_form_full_name_instructions = true;- await visit("/");- await click("header .sign-up-button");+ await visit("/signup");assert.dom("#username-validation-more-info")@@ -113,21 +110,34 @@ acceptance("Create Account", function () {});test("can sign in using a third-party auth", async function (assert) {- sinon.stub(LoginMethod, "buildPostForm").callsFake((url) => {+ sinon.stub(LoginMethod, "buildPostForm").callsFake((url, params) => {assert.step("buildPostForm");- assert.strictEqual(url, "/auth/facebook?signup=true");+ assert.strictEqual(url, "/auth/facebook");+ assert.deepEqual(params, { signup: true });});- await visit("/");- await click("header .sign-up-button");+ await visit("/signup");+ await click("#login-buttons button");++ assert.verifySteps(["buildPostForm"]);+ });++ test("it passes the email if it's stored in the session", async function (assert) {+ Session.current().email = "[email protected]";++ sinon.stub(LoginMethod, "buildPostForm").callsFake((_url, { email }) => {+ assert.step("buildPostForm");+ assert.strictEqual(email, "[email protected]");+ });++ await visit("/signup");await click("#login-buttons button");assert.verifySteps(["buildPostForm"]);});test("does not show passkeys button", async function (assert) {- await visit("/");- await click("header .sign-up-button");+ await visit("/signup");assert.dom(".signup-fullpage .btn-primary")@@ -143,8 +153,7 @@ acceptance("Create Account - full name requirement", function () {site.set("full_name_required_for_signup", true);site.set("full_name_visible_in_signup", true);- await visit("/");- await click("header .sign-up-button");+ await visit("/signup");await fillIn("#new-account-email", "[email protected]");await fillIn("#new-account-username", "good-tuna");@@ -178,8 +187,7 @@ acceptance("Create Account - full name requirement", function () {site.set("full_name_required_for_signup", false);site.set("full_name_visible_in_signup", false);- await visit("/");- await click("header .sign-up-button");+ await visit("/signup");assert.dom("#new-account-name").doesNotExist();@@ -209,8 +217,7 @@ acceptance("Create Account - full name requirement", function () {site.set("full_name_required_for_signup", false);site.set("full_name_visible_in_signup", true);- await visit("/");- await click("header .sign-up-button");+ await visit("/signup");assert.dom("#new-account-name").exists();diff --git a/app/assets/javascripts/discourse/tests/unit/lib/preload-store-test.js b/app/assets/javascripts/discourse/tests/unit/lib/preload-store-test.jsindex a42cf9509e7..3ee797a5076 100644--- a/app/assets/javascripts/discourse/tests/unit/lib/preload-store-test.js+++ b/app/assets/javascripts/discourse/tests/unit/lib/preload-store-test.js@@ -19,6 +19,11 @@ module("Unit | Utility | preload-store", function (hooks) {);});+ test("has", function (assert) {+ assert.false(PreloadStore.has("joker"), "returns false for a missing key");+ assert.true(PreloadStore.has("bane"), "returns true for an existing key");+ });+test("remove", function (assert) {PreloadStore.remove("bane");assert.blank(diff --git a/lib/auth/discourse_id_authenticator.rb b/lib/auth/discourse_id_authenticator.rbindex d3b98a334a8..34cedd6e7be 100644--- a/lib/auth/discourse_id_authenticator.rb+++ b/lib/auth/discourse_id_authenticator.rb@@ -7,7 +7,12 @@ class Auth::DiscourseIdAuthenticator < Auth::ManagedAuthenticatoroption :client_options, auth_scheme: :basic_authdef authorize_params- super.tap { _1[:intent] = "signup" if request.params["signup"] == "true" }+ super.tap do |params|+ if request.params["signup"] == "true"+ params[:intent] = "signup"+ params[:email] = request.params["email"] if request.params["email"].present?+ end+ endenddef callback_urldiff --git a/spec/lib/auth/discourse_id_authenticator_spec.rb b/spec/lib/auth/discourse_id_authenticator_spec.rbindex 05476a4809f..a6dd9e2f83c 100644--- a/spec/lib/auth/discourse_id_authenticator_spec.rb+++ b/spec/lib/auth/discourse_id_authenticator_spec.rb@@ -81,6 +81,66 @@ describe Auth::DiscourseIdAuthenticator doit "defines callback_url" doexpect(strategy.callback_url).to eq("http://test.localhost/auth/discourse_id/callback")end++ describe "#authorize_params" do+ let(:env) { Rack::MockRequest.env_for("http://example.com/auth/discourse_id", params:) }+ let(:request) { Rack::Request.new(env) }+ let(:strategy) do+ strategy = Auth::DiscourseIdAuthenticator::DiscourseIdStrategy.new({})+ allow(strategy).to receive(:request).and_return(request)+ strategy+ end++ before do+ allow_any_instance_of(OmniAuth::Strategies::OAuth2).to receive(+ :authorize_params,+ ).and_return({})+ end++ context "when just email parameter is present" do+ let(:params) { { "email" => "[email protected]" } }++ it "does not include email in authorize params" do+ expect(strategy.authorize_params[:email]).to be_nil+ end+ end++ context "when signup parameter is true" do+ let(:params) { { "signup" => "true" } }++ it "includes signup intent in authorize params" do+ expect(strategy.authorize_params[:intent]).to eq("signup")+ end+ end++ context "when signup parameter is false" do+ let(:params) { { "signup" => "false" } }++ it "does not include signup intent in authorize params" do+ expect(strategy.authorize_params[:intent]).to be_nil+ end+ end++ context "when signup is false and email is present" do+ let(:params) { { "email" => "[email protected]", "signup" => "false" } }++ it "does not include anything in authorize params" do+ params = strategy.authorize_params+ expect(params[:email]).to be_nil+ expect(params[:intent]).to be_nil+ end+ end++ context "when signup is true and email is present" do+ let(:params) { { "email" => "[email protected]", "signup" => "true" } }++ it "includes both email and intent in authorize params" do+ params = strategy.authorize_params+ expect(params[:email]).to eq("[email protected]")+ expect(params[:intent]).to eq("signup")+ end+ end+ endendlet(:hash) dodiff --git a/spec/system/discourse_id_spec.rb b/spec/system/discourse_id_spec.rbindex 8c7c4b163cb..b4b25c49a67 100644--- a/spec/system/discourse_id_spec.rb+++ b/spec/system/discourse_id_spec.rb@@ -18,8 +18,6 @@ describe "discourse login client auth" dousername: OmniauthHelpers::USERNAME,),)-- Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[:github]endafter { reset_omniauth_config(:discourse_id) }