From 0b334c0bbca7c8f4a89a78bc1d748726d3a4ade6 Mon Sep 17 00:00:00 2001 From: David Baker Date: Tue, 8 Oct 2019 18:56:13 +0100 Subject: [PATCH 1/6] Support UI Auth on adding email address --- src/AddThreepid.js | 43 ++++++++++++++++--- .../views/settings/account/EmailAddresses.js | 2 +- src/i18n/strings/en_EN.json | 1 + 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/src/AddThreepid.js b/src/AddThreepid.js index 8f19815210..307f09cd5c 100644 --- a/src/AddThreepid.js +++ b/src/AddThreepid.js @@ -17,6 +17,8 @@ limitations under the License. */ import MatrixClientPeg from './MatrixClientPeg'; +import sdk from './index'; +import Modal from './Modal'; import { _t } from './languageHandler'; import IdentityAuthClient from './IdentityAuthClient'; @@ -155,10 +157,10 @@ export default class AddThreepid { * the request failed. */ async checkEmailLinkClicked() { - const identityServerDomain = MatrixClientPeg.get().idBaseUrl.split("://")[1]; try { if (await MatrixClientPeg.get().doesServerSupportSeparateAddAndBind()) { if (this.bind) { + const identityServerDomain = MatrixClientPeg.get().idBaseUrl.split("://")[1]; const authClient = new IdentityAuthClient(); const identityAccessToken = await authClient.getAccessToken(); await MatrixClientPeg.get().bindThreePid({ @@ -168,12 +170,31 @@ export default class AddThreepid { id_access_token: identityAccessToken, }); } else { - await MatrixClientPeg.get().addThreePidOnly({ - sid: this.sessionId, - client_secret: this.clientSecret, - }); + try { + await this._makeAddThreepidOnlyRequest(); + + // The spec has always required this to use UI auth but synapse briefly + // implemented it without, so this may just succeed and that's OK. + return; + } catch (e) { + if (e.httpStatus !== 401 || !e.data || !e.data.flows) { + // doesn't look like an interactive-auth failure + throw e; + } + + // pop up an interactive auth dialog + const InteractiveAuthDialog = sdk.getComponent("dialogs.InteractiveAuthDialog"); + + Modal.createTrackedDialog('Add Email', '', InteractiveAuthDialog, { + title: _t("Add Email Address"), + matrixClient: MatrixClientPeg.get(), + authData: e.data, + makeRequest: this._makeAddThreepidOnlyRequest, + }); + } } } else { + const identityServerDomain = MatrixClientPeg.get().idBaseUrl.split("://")[1]; await MatrixClientPeg.get().addThreePid({ sid: this.sessionId, client_secret: this.clientSecret, @@ -190,6 +211,18 @@ export default class AddThreepid { } } + /** + * @param {Object} auth UI auth object + * @return {Promise} Response from /3pid/add call (in current spec, an empty object) + */ + _makeAddThreepidOnlyRequest = (auth) => { + return MatrixClientPeg.get().addThreePidOnly({ + sid: this.sessionId, + client_secret: this.clientSecret, + auth, + }); + } + /** * Takes a phone number verification code as entered by the user and validates * it with the ID server, then if successful, adds the phone number. diff --git a/src/components/views/settings/account/EmailAddresses.js b/src/components/views/settings/account/EmailAddresses.js index 30fc805f6d..e9bf79b005 100644 --- a/src/components/views/settings/account/EmailAddresses.js +++ b/src/components/views/settings/account/EmailAddresses.js @@ -195,7 +195,7 @@ export default class EmailAddresses extends React.Component { this.setState({continueDisabled: false}); if (err.errcode !== 'M_THREEPID_AUTH_FAILED') { const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog"); - console.error("Unable to verify email address: " + err); + console.error("Unable to verify email address: ", err); Modal.createTrackedDialog('Unable to verify email address', '', ErrorDialog, { title: _t("Unable to verify email address."), description: ((err && err.message) ? err.message : _t("Operation failed")), diff --git a/src/i18n/strings/en_EN.json b/src/i18n/strings/en_EN.json index fb52e07eab..bcd43968d6 100644 --- a/src/i18n/strings/en_EN.json +++ b/src/i18n/strings/en_EN.json @@ -1,6 +1,7 @@ { "This email address is already in use": "This email address is already in use", "This phone number is already in use": "This phone number is already in use", + "Add Email Address": "Add Email Address", "Failed to verify email address: make sure you clicked the link in the email": "Failed to verify email address: make sure you clicked the link in the email", "The platform you're on": "The platform you're on", "The version of Riot.im": "The version of Riot.im", From f4a2d216c3e1c78fa74eeed5eed50a4f58a69899 Mon Sep 17 00:00:00 2001 From: David Baker Date: Tue, 8 Oct 2019 19:07:39 +0100 Subject: [PATCH 2/6] Support UI auth on adding phone numbers --- src/AddThreepid.js | 29 ++++++++++++++++++++++++----- src/i18n/strings/en_EN.json | 1 + 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/AddThreepid.js b/src/AddThreepid.js index 307f09cd5c..8f4e3883d3 100644 --- a/src/AddThreepid.js +++ b/src/AddThreepid.js @@ -254,9 +254,9 @@ export default class AddThreepid { throw result; } - const identityServerDomain = MatrixClientPeg.get().idBaseUrl.split("://")[1]; if (await MatrixClientPeg.get().doesServerSupportSeparateAddAndBind()) { if (this.bind) { + const identityServerDomain = MatrixClientPeg.get().idBaseUrl.split("://")[1]; await MatrixClientPeg.get().bindThreePid({ sid: this.sessionId, client_secret: this.clientSecret, @@ -264,12 +264,31 @@ export default class AddThreepid { id_access_token: await authClient.getAccessToken(), }); } else { - await MatrixClientPeg.get().addThreePidOnly({ - sid: this.sessionId, - client_secret: this.clientSecret, - }); + try { + await this._makeAddThreepidOnlyRequest(); + + // The spec has always required this to use UI auth but synapse briefly + // implemented it without, so this may just succeed and that's OK. + return; + } catch (e) { + if (e.httpStatus !== 401 || !e.data || !e.data.flows) { + // doesn't look like an interactive-auth failure + throw e; + } + + // pop up an interactive auth dialog + const InteractiveAuthDialog = sdk.getComponent("dialogs.InteractiveAuthDialog"); + + Modal.createTrackedDialog('Add MSISDN', '', InteractiveAuthDialog, { + title: _t("Add Phone Number"), + matrixClient: MatrixClientPeg.get(), + authData: e.data, + makeRequest: this._makeAddThreepidOnlyRequest, + }); + } } } else { + const identityServerDomain = MatrixClientPeg.get().idBaseUrl.split("://")[1]; await MatrixClientPeg.get().addThreePid({ sid: this.sessionId, client_secret: this.clientSecret, diff --git a/src/i18n/strings/en_EN.json b/src/i18n/strings/en_EN.json index bcd43968d6..f6ed69a201 100644 --- a/src/i18n/strings/en_EN.json +++ b/src/i18n/strings/en_EN.json @@ -3,6 +3,7 @@ "This phone number is already in use": "This phone number is already in use", "Add Email Address": "Add Email Address", "Failed to verify email address: make sure you clicked the link in the email": "Failed to verify email address: make sure you clicked the link in the email", + "Add Phone Number": "Add Phone Number", "The platform you're on": "The platform you're on", "The version of Riot.im": "The version of Riot.im", "Whether or not you're logged in (we don't record your username)": "Whether or not you're logged in (we don't record your username)", From a653fea02071e1a1da409e6228a57f39d9565f52 Mon Sep 17 00:00:00 2001 From: David Baker Date: Wed, 9 Oct 2019 10:33:29 +0100 Subject: [PATCH 3/6] Removed unused lines Moved these around to avpid NPEs but they went away and I didn't notice when merging. --- src/AddThreepid.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/AddThreepid.js b/src/AddThreepid.js index b13cb0c9f3..f4a2a88b36 100644 --- a/src/AddThreepid.js +++ b/src/AddThreepid.js @@ -164,7 +164,6 @@ export default class AddThreepid { try { if (await MatrixClientPeg.get().doesServerSupportSeparateAddAndBind()) { if (this.bind) { - const identityServerDomain = MatrixClientPeg.get().idBaseUrl.split("://")[1]; const authClient = new IdentityAuthClient(); const identityAccessToken = await authClient.getAccessToken(); await MatrixClientPeg.get().bindThreePid({ @@ -198,7 +197,6 @@ export default class AddThreepid { } } } else { - const identityServerDomain = MatrixClientPeg.get().idBaseUrl.split("://")[1]; await MatrixClientPeg.get().addThreePid({ sid: this.sessionId, client_secret: this.clientSecret, @@ -260,7 +258,6 @@ export default class AddThreepid { if (await MatrixClientPeg.get().doesServerSupportSeparateAddAndBind()) { if (this.bind) { - const identityServerDomain = MatrixClientPeg.get().idBaseUrl.split("://")[1]; await MatrixClientPeg.get().bindThreePid({ sid: this.sessionId, client_secret: this.clientSecret, @@ -292,7 +289,6 @@ export default class AddThreepid { } } } else { - const identityServerDomain = MatrixClientPeg.get().idBaseUrl.split("://")[1]; await MatrixClientPeg.get().addThreePid({ sid: this.sessionId, client_secret: this.clientSecret, From 2040587826ff56372cd783d72ed9c8bab53f53fc Mon Sep 17 00:00:00 2001 From: David Baker Date: Wed, 9 Oct 2019 10:47:34 +0100 Subject: [PATCH 4/6] Return promises --- src/AddThreepid.js | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/src/AddThreepid.js b/src/AddThreepid.js index f4a2a88b36..0f09264295 100644 --- a/src/AddThreepid.js +++ b/src/AddThreepid.js @@ -188,11 +188,20 @@ export default class AddThreepid { // pop up an interactive auth dialog const InteractiveAuthDialog = sdk.getComponent("dialogs.InteractiveAuthDialog"); - Modal.createTrackedDialog('Add Email', '', InteractiveAuthDialog, { - title: _t("Add Email Address"), - matrixClient: MatrixClientPeg.get(), - authData: e.data, - makeRequest: this._makeAddThreepidOnlyRequest, + return new Promise((resolve, reject) => { + Modal.createTrackedDialog('Add Email', '', InteractiveAuthDialog, { + title: _t("Add Email Address"), + matrixClient: MatrixClientPeg.get(), + authData: e.data, + makeRequest: this._makeAddThreepidOnlyRequest, + onFinished: (success) => { + if (success) { + resolve(); + } else { + reject(); + } + }, + }); }); } } @@ -280,11 +289,20 @@ export default class AddThreepid { // pop up an interactive auth dialog const InteractiveAuthDialog = sdk.getComponent("dialogs.InteractiveAuthDialog"); - Modal.createTrackedDialog('Add MSISDN', '', InteractiveAuthDialog, { - title: _t("Add Phone Number"), - matrixClient: MatrixClientPeg.get(), - authData: e.data, - makeRequest: this._makeAddThreepidOnlyRequest, + return new Promise((resolve, reject) => { + Modal.createTrackedDialog('Add MSISDN', '', InteractiveAuthDialog, { + title: _t("Add Phone Number"), + matrixClient: MatrixClientPeg.get(), + authData: e.data, + makeRequest: this._makeAddThreepidOnlyRequest, + onFinished: (success) => { + if (success) { + resolve(); + } else { + reject(); + } + }, + }); }); } } From 1c207f3b5545d3e97a7e7e5f105461ca8821d25b Mon Sep 17 00:00:00 2001 From: David Baker Date: Wed, 9 Oct 2019 11:28:16 +0100 Subject: [PATCH 5/6] Revert 2040587826ff56372cd783d72ed9c8bab53f53fc --- src/AddThreepid.js | 38 ++++++++++---------------------------- 1 file changed, 10 insertions(+), 28 deletions(-) diff --git a/src/AddThreepid.js b/src/AddThreepid.js index 0f09264295..f4a2a88b36 100644 --- a/src/AddThreepid.js +++ b/src/AddThreepid.js @@ -188,20 +188,11 @@ export default class AddThreepid { // pop up an interactive auth dialog const InteractiveAuthDialog = sdk.getComponent("dialogs.InteractiveAuthDialog"); - return new Promise((resolve, reject) => { - Modal.createTrackedDialog('Add Email', '', InteractiveAuthDialog, { - title: _t("Add Email Address"), - matrixClient: MatrixClientPeg.get(), - authData: e.data, - makeRequest: this._makeAddThreepidOnlyRequest, - onFinished: (success) => { - if (success) { - resolve(); - } else { - reject(); - } - }, - }); + Modal.createTrackedDialog('Add Email', '', InteractiveAuthDialog, { + title: _t("Add Email Address"), + matrixClient: MatrixClientPeg.get(), + authData: e.data, + makeRequest: this._makeAddThreepidOnlyRequest, }); } } @@ -289,20 +280,11 @@ export default class AddThreepid { // pop up an interactive auth dialog const InteractiveAuthDialog = sdk.getComponent("dialogs.InteractiveAuthDialog"); - return new Promise((resolve, reject) => { - Modal.createTrackedDialog('Add MSISDN', '', InteractiveAuthDialog, { - title: _t("Add Phone Number"), - matrixClient: MatrixClientPeg.get(), - authData: e.data, - makeRequest: this._makeAddThreepidOnlyRequest, - onFinished: (success) => { - if (success) { - resolve(); - } else { - reject(); - } - }, - }); + Modal.createTrackedDialog('Add MSISDN', '', InteractiveAuthDialog, { + title: _t("Add Phone Number"), + matrixClient: MatrixClientPeg.get(), + authData: e.data, + makeRequest: this._makeAddThreepidOnlyRequest, }); } } From 93aebf9e68ce304497c81cca9a371b9e2630dbe7 Mon Sep 17 00:00:00 2001 From: David Baker Date: Wed, 9 Oct 2019 11:30:44 +0100 Subject: [PATCH 6/6] Return promises a better way --- src/AddThreepid.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/AddThreepid.js b/src/AddThreepid.js index f4a2a88b36..548ed4ce48 100644 --- a/src/AddThreepid.js +++ b/src/AddThreepid.js @@ -188,12 +188,13 @@ export default class AddThreepid { // pop up an interactive auth dialog const InteractiveAuthDialog = sdk.getComponent("dialogs.InteractiveAuthDialog"); - Modal.createTrackedDialog('Add Email', '', InteractiveAuthDialog, { + const { finished } = Modal.createTrackedDialog('Add Email', '', InteractiveAuthDialog, { title: _t("Add Email Address"), matrixClient: MatrixClientPeg.get(), authData: e.data, makeRequest: this._makeAddThreepidOnlyRequest, }); + return finished; } } } else { @@ -280,12 +281,13 @@ export default class AddThreepid { // pop up an interactive auth dialog const InteractiveAuthDialog = sdk.getComponent("dialogs.InteractiveAuthDialog"); - Modal.createTrackedDialog('Add MSISDN', '', InteractiveAuthDialog, { + const { finished } = Modal.createTrackedDialog('Add MSISDN', '', InteractiveAuthDialog, { title: _t("Add Phone Number"), matrixClient: MatrixClientPeg.get(), authData: e.data, makeRequest: this._makeAddThreepidOnlyRequest, }); + return finished; } } } else {