From 95164d08d5785c20de0315b1cd319a66062871e2 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Mon, 9 Mar 2020 17:06:10 -0600 Subject: [PATCH 1/2] Remove v1 identity server fallbacks Fixes https://github.com/vector-im/riot-web/issues/10443 **Review with https://github.com/matrix-org/matrix-react-sdk/pull/4191** --- src/base-apis.js | 208 +++++++++++++---------------------------------- src/http-api.js | 1 + 2 files changed, 58 insertions(+), 151 deletions(-) diff --git a/src/base-apis.js b/src/base-apis.js index becaa20029a..ef96873d685 100644 --- a/src/base-apis.js +++ b/src/base-apis.js @@ -28,13 +28,7 @@ import {SERVICE_TYPES} from './service-types'; import {logger} from './logger'; import {PushProcessor} from "./pushprocessor"; import * as utils from "./utils"; -import { - MatrixHttpApi, - PREFIX_IDENTITY_V1, - PREFIX_IDENTITY_V2, - PREFIX_R0, - PREFIX_UNSTABLE, -} from "./http-api"; +import {MatrixHttpApi, PREFIX_IDENTITY_V2, PREFIX_R0, PREFIX_UNSTABLE,} from "./http-api"; function termsUrlForService(serviceType, baseUrl) { switch (serviceType) { @@ -1895,28 +1889,10 @@ MatrixBaseApis.prototype.requestEmailToken = async function( next_link: nextLink, }; - try { - const response = await this._http.idServerRequest( - undefined, "POST", "/validate/email/requestToken", - params, PREFIX_IDENTITY_V2, identityAccessToken, - ); - // TODO: Fold callback into above call once v1 path below is removed - if (callback) callback(null, response); - return response; - } catch (err) { - if (err.cors === "rejected" || err.httpStatus === 404) { - // Fall back to deprecated v1 API for now - // TODO: Remove this path once v2 is only supported version - // See https://github.com/vector-im/riot-web/issues/10443 - logger.warn("IS doesn't support v2, falling back to deprecated v1"); - return await this._http.idServerRequest( - callback, "POST", "/validate/email/requestToken", - params, PREFIX_IDENTITY_V1, - ); - } - if (callback) callback(err); - throw err; - } + return await this._http.idServerRequest( + callback, "POST", "/validate/email/requestToken", + params, PREFIX_IDENTITY_V2, identityAccessToken, + ); }; /** @@ -1963,28 +1939,10 @@ MatrixBaseApis.prototype.requestMsisdnToken = async function( next_link: nextLink, }; - try { - const response = await this._http.idServerRequest( - undefined, "POST", "/validate/msisdn/requestToken", - params, PREFIX_IDENTITY_V2, identityAccessToken, - ); - // TODO: Fold callback into above call once v1 path below is removed - if (callback) callback(null, response); - return response; - } catch (err) { - if (err.cors === "rejected" || err.httpStatus === 404) { - // Fall back to deprecated v1 API for now - // TODO: Remove this path once v2 is only supported version - // See https://github.com/vector-im/riot-web/issues/10443 - logger.warn("IS doesn't support v2, falling back to deprecated v1"); - return await this._http.idServerRequest( - callback, "POST", "/validate/msisdn/requestToken", - params, PREFIX_IDENTITY_V1, - ); - } - if (callback) callback(err); - throw err; - } + return await this._http.idServerRequest( + callback, "POST", "/validate/msisdn/requestToken", + params, PREFIX_IDENTITY_V2, identityAccessToken, + ); }; /** @@ -2018,24 +1976,10 @@ MatrixBaseApis.prototype.submitMsisdnToken = async function( token: msisdnToken, }; - try { - return await this._http.idServerRequest( - undefined, "POST", "/validate/msisdn/submitToken", - params, PREFIX_IDENTITY_V2, identityAccessToken, - ); - } catch (err) { - if (err.cors === "rejected" || err.httpStatus === 404) { - // Fall back to deprecated v1 API for now - // TODO: Remove this path once v2 is only supported version - // See https://github.com/vector-im/riot-web/issues/10443 - logger.warn("IS doesn't support v2, falling back to deprecated v1"); - return await this._http.idServerRequest( - undefined, "POST", "/validate/msisdn/submitToken", - params, PREFIX_IDENTITY_V1, - ); - } - throw err; - } + return await this._http.idServerRequest( + undefined, "POST", "/validate/msisdn/submitToken", + params, PREFIX_IDENTITY_V2, identityAccessToken, + ); }; /** @@ -2190,53 +2134,32 @@ MatrixBaseApis.prototype.lookupThreePid = async function( callback, identityAccessToken, ) { - try { - // Note: we're using the V2 API by calling this function, but our - // function contract requires a V1 response. We therefore have to - // convert it manually. - const response = await this.identityHashedLookup( - [[address, medium]], identityAccessToken, - ); - const result = response.find(p => p.address === address); - if (!result) { - // TODO: Fold callback into above call once v1 path below is removed - if (callback) callback(null, {}); - return {}; - } - - const mapping = { - address, - medium, - mxid: result.mxid, - - // We can't reasonably fill these parameters: - // not_before - // not_after - // ts - // signatures - }; - - // TODO: Fold callback into above call once v1 path below is removed - if (callback) callback(null, mapping); - return mapping; - } catch (err) { - if (err.cors === "rejected" || err.httpStatus === 404) { - // Fall back to deprecated v1 API for now - // TODO: Remove this path once v2 is only supported version - // See https://github.com/vector-im/riot-web/issues/10443 - const params = { - medium: medium, - address: address, - }; - logger.warn("IS doesn't support v2, falling back to deprecated v1"); - return await this._http.idServerRequest( - callback, "GET", "/lookup", - params, PREFIX_IDENTITY_V1, - ); - } - if (callback) callback(err, undefined); - throw err; + // Note: we're using the V2 API by calling this function, but our + // function contract requires a V1 response. We therefore have to + // convert it manually. + const response = await this.identityHashedLookup( + [[address, medium]], identityAccessToken, + ); + const result = response.find(p => p.address === address); + if (!result) { + if (callback) callback(null, {}); + return {}; } + + const mapping = { + address, + medium, + mxid: result.mxid, + + // We can't reasonably fill these parameters: + // not_before + // not_after + // ts + // signatures + }; + + if (callback) callback(null, mapping); + return mapping; }; /** @@ -2254,46 +2177,29 @@ MatrixBaseApis.prototype.bulkLookupThreePids = async function( query, identityAccessToken, ) { - try { - // Note: we're using the V2 API by calling this function, but our - // function contract requires a V1 response. We therefore have to - // convert it manually. - const response = await this.identityHashedLookup( - // We have to reverse the query order to get [address, medium] pairs - query.map(p => [p[1], p[0]]), identityAccessToken, - ); - - const v1results = []; - for (const mapping of response) { - const originalQuery = query.find(p => p[1] === mapping.address); - if (!originalQuery) { - throw new Error("Identity sever returned unexpected results"); - } + // Note: we're using the V2 API by calling this function, but our + // function contract requires a V1 response. We therefore have to + // convert it manually. + const response = await this.identityHashedLookup( + // We have to reverse the query order to get [address, medium] pairs + query.map(p => [p[1], p[0]]), identityAccessToken, + ); - v1results.push([ - originalQuery[0], // medium - mapping.address, - mapping.mxid, - ]); + const v1results = []; + for (const mapping of response) { + const originalQuery = query.find(p => p[1] === mapping.address); + if (!originalQuery) { + throw new Error("Identity sever returned unexpected results"); } - return {threepids: v1results}; - } catch (err) { - if (err.cors === "rejected" || err.httpStatus === 404) { - // Fall back to deprecated v1 API for now - // TODO: Remove this path once v2 is only supported version - // See https://github.com/vector-im/riot-web/issues/10443 - const params = { - threepids: query, - }; - logger.warn("IS doesn't support v2, falling back to deprecated v1"); - return await this._http.idServerRequest( - undefined, "POST", "/bulk_lookup", params, - PREFIX_IDENTITY_V1, identityAccessToken, - ); - } - throw err; + v1results.push([ + originalQuery[0], // medium + mapping.address, + mapping.mxid, + ]); } + + return {threepids: v1results}; }; /** diff --git a/src/http-api.js b/src/http-api.js index b3ef4effa21..d297c308463 100644 --- a/src/http-api.js +++ b/src/http-api.js @@ -47,6 +47,7 @@ export const PREFIX_UNSTABLE = "/_matrix/client/unstable"; /** * URI path for v1 of the the identity API + * @deprecated Use v2. */ export const PREFIX_IDENTITY_V1 = "/_matrix/identity/api/v1"; From 800441e0edaf6ac78a4989ea13ba991a58993496 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Mon, 9 Mar 2020 17:10:37 -0600 Subject: [PATCH 2/2] Appease the linter --- src/base-apis.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/base-apis.js b/src/base-apis.js index ef96873d685..a5ef7e258d3 100644 --- a/src/base-apis.js +++ b/src/base-apis.js @@ -28,7 +28,7 @@ import {SERVICE_TYPES} from './service-types'; import {logger} from './logger'; import {PushProcessor} from "./pushprocessor"; import * as utils from "./utils"; -import {MatrixHttpApi, PREFIX_IDENTITY_V2, PREFIX_R0, PREFIX_UNSTABLE,} from "./http-api"; +import {MatrixHttpApi, PREFIX_IDENTITY_V2, PREFIX_R0, PREFIX_UNSTABLE} from "./http-api"; function termsUrlForService(serviceType, baseUrl) { switch (serviceType) {