Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove v1 identity server fallbacks #1253

Merged
merged 2 commits into from
Mar 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
208 changes: 57 additions & 151 deletions src/base-apis.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
);
};

/**
Expand Down Expand Up @@ -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,
);
};

/**
Expand Down Expand Up @@ -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,
);
};

/**
Expand Down Expand Up @@ -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;
};

/**
Expand All @@ -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};
};

/**
Expand Down
1 change: 1 addition & 0 deletions src/http-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down