1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-25 05:23:13 +03:00

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**
This commit is contained in:
Travis Ralston
2020-03-09 17:06:10 -06:00
parent acba31bd6d
commit 95164d08d5
2 changed files with 58 additions and 151 deletions

View File

@@ -28,13 +28,7 @@ import {SERVICE_TYPES} from './service-types';
import {logger} from './logger'; import {logger} from './logger';
import {PushProcessor} from "./pushprocessor"; import {PushProcessor} from "./pushprocessor";
import * as utils from "./utils"; import * as utils from "./utils";
import { import {MatrixHttpApi, PREFIX_IDENTITY_V2, PREFIX_R0, PREFIX_UNSTABLE,} from "./http-api";
MatrixHttpApi,
PREFIX_IDENTITY_V1,
PREFIX_IDENTITY_V2,
PREFIX_R0,
PREFIX_UNSTABLE,
} from "./http-api";
function termsUrlForService(serviceType, baseUrl) { function termsUrlForService(serviceType, baseUrl) {
switch (serviceType) { switch (serviceType) {
@@ -1895,28 +1889,10 @@ MatrixBaseApis.prototype.requestEmailToken = async function(
next_link: nextLink, next_link: nextLink,
}; };
try { return await this._http.idServerRequest(
const response = await this._http.idServerRequest( callback, "POST", "/validate/email/requestToken",
undefined, "POST", "/validate/email/requestToken", params, PREFIX_IDENTITY_V2, identityAccessToken,
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;
}
}; };
/** /**
@@ -1963,28 +1939,10 @@ MatrixBaseApis.prototype.requestMsisdnToken = async function(
next_link: nextLink, next_link: nextLink,
}; };
try { return await this._http.idServerRequest(
const response = await this._http.idServerRequest( callback, "POST", "/validate/msisdn/requestToken",
undefined, "POST", "/validate/msisdn/requestToken", params, PREFIX_IDENTITY_V2, identityAccessToken,
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;
}
}; };
/** /**
@@ -2018,24 +1976,10 @@ MatrixBaseApis.prototype.submitMsisdnToken = async function(
token: msisdnToken, token: msisdnToken,
}; };
try { return await this._http.idServerRequest(
return await this._http.idServerRequest( undefined, "POST", "/validate/msisdn/submitToken",
undefined, "POST", "/validate/msisdn/submitToken", params, PREFIX_IDENTITY_V2, identityAccessToken,
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;
}
}; };
/** /**
@@ -2190,53 +2134,32 @@ MatrixBaseApis.prototype.lookupThreePid = async function(
callback, callback,
identityAccessToken, identityAccessToken,
) { ) {
try { // Note: we're using the V2 API by calling this function, but our
// Note: we're using the V2 API by calling this function, but our // function contract requires a V1 response. We therefore have to
// function contract requires a V1 response. We therefore have to // convert it manually.
// convert it manually. const response = await this.identityHashedLookup(
const response = await this.identityHashedLookup( [[address, medium]], identityAccessToken,
[[address, medium]], identityAccessToken, );
); const result = response.find(p => p.address === address);
const result = response.find(p => p.address === address); if (!result) {
if (!result) { if (callback) callback(null, {});
// TODO: Fold callback into above call once v1 path below is removed return {};
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;
} }
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, query,
identityAccessToken, identityAccessToken,
) { ) {
try { // Note: we're using the V2 API by calling this function, but our
// Note: we're using the V2 API by calling this function, but our // function contract requires a V1 response. We therefore have to
// function contract requires a V1 response. We therefore have to // convert it manually.
// convert it manually. const response = await this.identityHashedLookup(
const response = await this.identityHashedLookup( // We have to reverse the query order to get [address, medium] pairs
// We have to reverse the query order to get [address, medium] pairs query.map(p => [p[1], p[0]]), identityAccessToken,
query.map(p => [p[1], p[0]]), identityAccessToken, );
);
const v1results = []; const v1results = [];
for (const mapping of response) { for (const mapping of response) {
const originalQuery = query.find(p => p[1] === mapping.address); const originalQuery = query.find(p => p[1] === mapping.address);
if (!originalQuery) { if (!originalQuery) {
throw new Error("Identity sever returned unexpected results"); throw new Error("Identity sever returned unexpected results");
}
v1results.push([
originalQuery[0], // medium
mapping.address,
mapping.mxid,
]);
} }
return {threepids: v1results}; v1results.push([
} catch (err) { originalQuery[0], // medium
if (err.cors === "rejected" || err.httpStatus === 404) { mapping.address,
// Fall back to deprecated v1 API for now mapping.mxid,
// 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;
} }
return {threepids: v1results};
}; };
/** /**

View File

@@ -47,6 +47,7 @@ export const PREFIX_UNSTABLE = "/_matrix/client/unstable";
/** /**
* URI path for v1 of the the identity API * URI path for v1 of the the identity API
* @deprecated Use v2.
*/ */
export const PREFIX_IDENTITY_V1 = "/_matrix/identity/api/v1"; export const PREFIX_IDENTITY_V1 = "/_matrix/identity/api/v1";