You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-11-25 05:23:13 +03:00
Merge remote-tracking branch 'origin/develop' into dbkr/cross_signing
This commit is contained in:
708
src/base-apis.js
708
src/base-apis.js
@@ -1,6 +1,8 @@
|
||||
/*
|
||||
Copyright 2015, 2016 OpenMarket Ltd
|
||||
Copyright 2017 Vector Creations Ltd
|
||||
Copyright 2019 The Matrix.org Foundation C.I.C.
|
||||
Copyright 2019 Michael Telatynski <7t3chguy@gmail.com>
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
@@ -23,8 +25,23 @@ limitations under the License.
|
||||
* @module base-apis
|
||||
*/
|
||||
|
||||
import { SERVICE_TYPES } from './service-types';
|
||||
import logger from './logger';
|
||||
|
||||
const httpApi = require("./http-api");
|
||||
const utils = require("./utils");
|
||||
const PushProcessor = require("./pushprocessor");
|
||||
|
||||
function termsUrlForService(serviceType, baseUrl) {
|
||||
switch (serviceType) {
|
||||
case SERVICE_TYPES.IS:
|
||||
return baseUrl + httpApi.PREFIX_IDENTITY_V2 + '/terms';
|
||||
case SERVICE_TYPES.IM:
|
||||
return baseUrl + '/_matrix/integrations/v1/terms';
|
||||
default:
|
||||
throw new Error('Unsupported service type');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Low-level wrappers for the Matrix APIs
|
||||
@@ -46,6 +63,15 @@ const utils = require("./utils");
|
||||
*
|
||||
* @param {string} opts.accessToken The access_token for this user.
|
||||
*
|
||||
* @param {IdentityServerProvider} [opts.identityServer]
|
||||
* Optional. A provider object with one function `getAccessToken`, which is a
|
||||
* callback that returns a Promise<String> of an identity access token to supply
|
||||
* with identity requests. If the object is unset, no access token will be
|
||||
* supplied.
|
||||
* See also https://github.com/vector-im/riot-web/issues/10615 which seeks to
|
||||
* replace the previous approach of manual access tokens params with this
|
||||
* callback throughout the SDK.
|
||||
*
|
||||
* @param {Number=} opts.localTimeoutMs Optional. The default maximum amount of
|
||||
* time to wait before timing out HTTP requests. If not specified, there is no
|
||||
* timeout.
|
||||
@@ -62,6 +88,7 @@ function MatrixBaseApis(opts) {
|
||||
|
||||
this.baseUrl = opts.baseUrl;
|
||||
this.idBaseUrl = opts.idBaseUrl;
|
||||
this.identityServer = opts.identityServer;
|
||||
|
||||
const httpOpts = {
|
||||
baseUrl: opts.baseUrl,
|
||||
@@ -100,6 +127,15 @@ MatrixBaseApis.prototype.getIdentityServerUrl = function(stripProto=false) {
|
||||
return this.idBaseUrl;
|
||||
};
|
||||
|
||||
/**
|
||||
* Set the Identity Server URL of this client
|
||||
* @param {string} url New Identity Server URL
|
||||
*/
|
||||
MatrixBaseApis.prototype.setIdentityServerUrl = function(url) {
|
||||
this.idBaseUrl = utils.ensureNoTrailingSlash(url);
|
||||
this._http.setIdBaseUrl(this.idBaseUrl);
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the access token associated with this account.
|
||||
* @return {?String} The access_token or null
|
||||
@@ -391,9 +427,8 @@ MatrixBaseApis.prototype.deactivateAccount = function(auth, erase) {
|
||||
body.erase = erase;
|
||||
}
|
||||
|
||||
return this._http.authedRequestWithPrefix(
|
||||
return this._http.authedRequest(
|
||||
undefined, "POST", '/account/deactivate', undefined, body,
|
||||
httpApi.PREFIX_R0,
|
||||
);
|
||||
};
|
||||
|
||||
@@ -438,6 +473,37 @@ MatrixBaseApis.prototype.createRoom = function(options, callback) {
|
||||
callback, "POST", "/createRoom", undefined, options,
|
||||
);
|
||||
};
|
||||
/**
|
||||
* Fetches relations for a given event
|
||||
* @param {string} roomId the room of the event
|
||||
* @param {string} eventId the id of the event
|
||||
* @param {string} relationType the rel_type of the relations requested
|
||||
* @param {string} eventType the event type of the relations requested
|
||||
* @param {Object} opts options with optional values for the request.
|
||||
* @param {Object} opts.from the pagination token returned from a previous request as `next_batch` to return following relations.
|
||||
* @return {Object} the response, with chunk and next_batch.
|
||||
*/
|
||||
MatrixBaseApis.prototype.fetchRelations =
|
||||
async function(roomId, eventId, relationType, eventType, opts) {
|
||||
const queryParams = {};
|
||||
if (opts.from) {
|
||||
queryParams.from = opts.from;
|
||||
}
|
||||
const queryString = utils.encodeParams(queryParams);
|
||||
const path = utils.encodeUri(
|
||||
"/rooms/$roomId/relations/$eventId/$relationType/$eventType?" + queryString, {
|
||||
$roomId: roomId,
|
||||
$eventId: eventId,
|
||||
$relationType: relationType,
|
||||
$eventType: eventType,
|
||||
});
|
||||
const response = await this._http.authedRequest(
|
||||
undefined, "GET", path, null, null, {
|
||||
prefix: httpApi.PREFIX_UNSTABLE,
|
||||
},
|
||||
);
|
||||
return response;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {string} roomId
|
||||
@@ -927,10 +993,13 @@ MatrixBaseApis.prototype.roomInitialSync = function(roomId, limit, callback) {
|
||||
* @param {string} rrEventId ID of the event tracked by the read receipt. This is here
|
||||
* for convenience because the RR and the RM are commonly updated at the same time as
|
||||
* each other. Optional.
|
||||
* @param {object} opts Options for the read markers.
|
||||
* @param {object} opts.hidden True to hide the read receipt from other users. <b>This
|
||||
* property is currently unstable and may change in the future.</b>
|
||||
* @return {module:client.Promise} Resolves: the empty object, {}.
|
||||
*/
|
||||
MatrixBaseApis.prototype.setRoomReadMarkersHttpRequest =
|
||||
function(roomId, rmEventId, rrEventId) {
|
||||
function(roomId, rmEventId, rrEventId, opts) {
|
||||
const path = utils.encodeUri("/rooms/$roomId/read_markers", {
|
||||
$roomId: roomId,
|
||||
});
|
||||
@@ -938,6 +1007,7 @@ MatrixBaseApis.prototype.setRoomReadMarkersHttpRequest =
|
||||
const content = {
|
||||
"m.fully_read": rmEventId,
|
||||
"m.read": rrEventId,
|
||||
"m.hidden": Boolean(opts ? opts.hidden : false),
|
||||
};
|
||||
|
||||
return this._http.authedRequest(
|
||||
@@ -1269,10 +1339,16 @@ MatrixBaseApis.prototype.getThreePids = function(callback) {
|
||||
};
|
||||
|
||||
/**
|
||||
* Add a 3PID to your homeserver account and optionally bind it to an identity
|
||||
* server as well. An identity server is required as part of the `creds` object.
|
||||
*
|
||||
* This API is deprecated, and you should instead use `addThreePidOnly`
|
||||
* for homeservers that support it.
|
||||
*
|
||||
* @param {Object} creds
|
||||
* @param {boolean} bind
|
||||
* @param {module:client.callback} callback Optional.
|
||||
* @return {module:client.Promise} Resolves: TODO
|
||||
* @return {module:client.Promise} Resolves: on success
|
||||
* @return {module:http-api.MatrixError} Rejects: with an error response.
|
||||
*/
|
||||
MatrixBaseApis.prototype.addThreePid = function(creds, bind, callback) {
|
||||
@@ -1286,6 +1362,75 @@ MatrixBaseApis.prototype.addThreePid = function(creds, bind, callback) {
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* Add a 3PID to your homeserver account. This API does not use an identity
|
||||
* server, as the homeserver is expected to handle 3PID ownership validation.
|
||||
*
|
||||
* You can check whether a homeserver supports this API via
|
||||
* `doesServerSupportSeparateAddAndBind`.
|
||||
*
|
||||
* @param {Object} data A object with 3PID validation data from having called
|
||||
* `account/3pid/<medium>/requestToken` on the homeserver.
|
||||
* @return {module:client.Promise} Resolves: on success
|
||||
* @return {module:http-api.MatrixError} Rejects: with an error response.
|
||||
*/
|
||||
MatrixBaseApis.prototype.addThreePidOnly = function(data) {
|
||||
const path = "/account/3pid/add";
|
||||
return this._http.authedRequest(
|
||||
undefined, "POST", path, null, data, {
|
||||
prefix: httpApi.PREFIX_UNSTABLE,
|
||||
},
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* Bind a 3PID for discovery onto an identity server via the homeserver. The
|
||||
* identity server handles 3PID ownership validation and the homeserver records
|
||||
* the new binding to track where all 3PIDs for the account are bound.
|
||||
*
|
||||
* You can check whether a homeserver supports this API via
|
||||
* `doesServerSupportSeparateAddAndBind`.
|
||||
*
|
||||
* @param {Object} data A object with 3PID validation data from having called
|
||||
* `validate/<medium>/requestToken` on the identity server. It should also
|
||||
* contain `id_server` and `id_access_token` fields as well.
|
||||
* @return {module:client.Promise} Resolves: on success
|
||||
* @return {module:http-api.MatrixError} Rejects: with an error response.
|
||||
*/
|
||||
MatrixBaseApis.prototype.bindThreePid = function(data) {
|
||||
const path = "/account/3pid/bind";
|
||||
return this._http.authedRequest(
|
||||
undefined, "POST", path, null, data, {
|
||||
prefix: httpApi.PREFIX_UNSTABLE,
|
||||
},
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* Unbind a 3PID for discovery on an identity server via the homeserver. The
|
||||
* homeserver removes its record of the binding to keep an updated record of
|
||||
* where all 3PIDs for the account are bound.
|
||||
*
|
||||
* @param {string} medium The threepid medium (eg. 'email')
|
||||
* @param {string} address The threepid address (eg. 'bob@example.com')
|
||||
* this must be as returned by getThreePids.
|
||||
* @return {module:client.Promise} Resolves: on success
|
||||
* @return {module:http-api.MatrixError} Rejects: with an error response.
|
||||
*/
|
||||
MatrixBaseApis.prototype.unbindThreePid = function(medium, address) {
|
||||
const path = "/account/3pid/unbind";
|
||||
const data = {
|
||||
medium,
|
||||
address,
|
||||
id_server: this.getIdentityServerUrl(true),
|
||||
};
|
||||
return this._http.authedRequest(
|
||||
undefined, "POST", path, null, data, {
|
||||
prefix: httpApi.PREFIX_UNSTABLE,
|
||||
},
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {string} medium The threepid medium (eg. 'email')
|
||||
* @param {string} address The threepid address (eg. 'bob@example.com')
|
||||
@@ -1300,9 +1445,7 @@ MatrixBaseApis.prototype.deleteThreePid = function(medium, address) {
|
||||
'medium': medium,
|
||||
'address': address,
|
||||
};
|
||||
return this._http.authedRequestWithPrefix(
|
||||
undefined, "POST", path, null, data, httpApi.PREFIX_UNSTABLE,
|
||||
);
|
||||
return this._http.authedRequest(undefined, "POST", path, null, data);
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -1335,10 +1478,8 @@ MatrixBaseApis.prototype.setPassword = function(authDict, newPassword, callback)
|
||||
* @return {module:http-api.MatrixError} Rejects: with an error response.
|
||||
*/
|
||||
MatrixBaseApis.prototype.getDevices = function() {
|
||||
const path = "/devices";
|
||||
return this._http.authedRequestWithPrefix(
|
||||
undefined, "GET", path, undefined, undefined,
|
||||
httpApi.PREFIX_UNSTABLE,
|
||||
return this._http.authedRequest(
|
||||
undefined, 'GET', "/devices", undefined, undefined,
|
||||
);
|
||||
};
|
||||
|
||||
@@ -1355,11 +1496,7 @@ MatrixBaseApis.prototype.setDeviceDetails = function(device_id, body) {
|
||||
$device_id: device_id,
|
||||
});
|
||||
|
||||
|
||||
return this._http.authedRequestWithPrefix(
|
||||
undefined, "PUT", path, undefined, body,
|
||||
httpApi.PREFIX_UNSTABLE,
|
||||
);
|
||||
return this._http.authedRequest(undefined, "PUT", path, undefined, body);
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -1381,10 +1518,7 @@ MatrixBaseApis.prototype.deleteDevice = function(device_id, auth) {
|
||||
body.auth = auth;
|
||||
}
|
||||
|
||||
return this._http.authedRequestWithPrefix(
|
||||
undefined, "DELETE", path, undefined, body,
|
||||
httpApi.PREFIX_UNSTABLE,
|
||||
);
|
||||
return this._http.authedRequest(undefined, "DELETE", path, undefined, body);
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -1402,10 +1536,8 @@ MatrixBaseApis.prototype.deleteMultipleDevices = function(devices, auth) {
|
||||
body.auth = auth;
|
||||
}
|
||||
|
||||
return this._http.authedRequestWithPrefix(
|
||||
undefined, "POST", "/delete_devices", undefined, body,
|
||||
httpApi.PREFIX_UNSTABLE,
|
||||
);
|
||||
const path = "/delete_devices";
|
||||
return this._http.authedRequest(undefined, "POST", path, undefined, body);
|
||||
};
|
||||
|
||||
|
||||
@@ -1447,7 +1579,9 @@ MatrixBaseApis.prototype.setPusher = function(pusher, callback) {
|
||||
* @return {module:http-api.MatrixError} Rejects: with an error response.
|
||||
*/
|
||||
MatrixBaseApis.prototype.getPushRules = function(callback) {
|
||||
return this._http.authedRequest(callback, "GET", "/pushrules/");
|
||||
return this._http.authedRequest(callback, "GET", "/pushrules/").then(rules => {
|
||||
return PushProcessor.rewriteDefaultRules(rules);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -1581,9 +1715,7 @@ MatrixBaseApis.prototype.uploadKeysRequest = function(content, opts, callback) {
|
||||
} else {
|
||||
path = "/keys/upload";
|
||||
}
|
||||
return this._http.authedRequestWithPrefix(
|
||||
callback, "POST", path, undefined, content, httpApi.PREFIX_UNSTABLE,
|
||||
);
|
||||
return this._http.authedRequest(callback, "POST", path, undefined, content);
|
||||
};
|
||||
|
||||
MatrixBaseApis.prototype.uploadKeySignatures = function(content) {
|
||||
@@ -1625,10 +1757,7 @@ MatrixBaseApis.prototype.downloadKeysForUsers = function(userIds, opts) {
|
||||
content.device_keys[u] = {};
|
||||
});
|
||||
|
||||
return this._http.authedRequestWithPrefix(
|
||||
undefined, "POST", "/keys/query", undefined, content,
|
||||
httpApi.PREFIX_UNSTABLE,
|
||||
);
|
||||
return this._http.authedRequest(undefined, "POST", "/keys/query", undefined, content);
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -1656,10 +1785,8 @@ MatrixBaseApis.prototype.claimOneTimeKeys = function(devices, key_algorithm) {
|
||||
query[deviceId] = key_algorithm;
|
||||
}
|
||||
const content = {one_time_keys: queries};
|
||||
return this._http.authedRequestWithPrefix(
|
||||
undefined, "POST", "/keys/claim", undefined, content,
|
||||
httpApi.PREFIX_UNSTABLE,
|
||||
);
|
||||
const path = "/keys/claim";
|
||||
return this._http.authedRequest(undefined, "POST", path, undefined, content);
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -1678,10 +1805,8 @@ MatrixBaseApis.prototype.getKeyChanges = function(oldToken, newToken) {
|
||||
to: newToken,
|
||||
};
|
||||
|
||||
return this._http.authedRequestWithPrefix(
|
||||
undefined, "GET", "/keys/changes", qps, undefined,
|
||||
httpApi.PREFIX_UNSTABLE,
|
||||
);
|
||||
const path = "/keys/changes";
|
||||
return this._http.authedRequest(undefined, "GET", path, qps, undefined);
|
||||
};
|
||||
|
||||
MatrixBaseApis.prototype.uploadDeviceSigningKeys = function(auth, keys) {
|
||||
@@ -1696,10 +1821,36 @@ MatrixBaseApis.prototype.uploadDeviceSigningKeys = function(auth, keys) {
|
||||
// ==========================
|
||||
|
||||
/**
|
||||
* Requests an email verification token directly from an Identity Server.
|
||||
* Register with an Identity Server using the OpenID token from the user's
|
||||
* Homeserver, which can be retrieved via
|
||||
* {@link module:client~MatrixClient#getOpenIdToken}.
|
||||
*
|
||||
* Note that the Home Server offers APIs to proxy this API for specific
|
||||
* situations, allowing for better feedback to the user.
|
||||
* Note that the `/account/register` endpoint (as well as IS authentication in
|
||||
* general) was added as part of the v2 API version.
|
||||
*
|
||||
* @param {object} hsOpenIdToken
|
||||
* @return {module:client.Promise} Resolves: with object containing an Identity
|
||||
* Server access token.
|
||||
* @return {module:http-api.MatrixError} Rejects: with an error response.
|
||||
*/
|
||||
MatrixBaseApis.prototype.registerWithIdentityServer = function(hsOpenIdToken) {
|
||||
if (!this.idBaseUrl) {
|
||||
throw new Error("No Identity Server base URL set");
|
||||
}
|
||||
|
||||
const uri = this.idBaseUrl + httpApi.PREFIX_IDENTITY_V2 + "/account/register";
|
||||
return this._http.requestOtherUrl(
|
||||
undefined, "POST", uri,
|
||||
null, hsOpenIdToken,
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* Requests an email verification token directly from an identity server.
|
||||
*
|
||||
* This API is used as part of binding an email for discovery on an identity
|
||||
* server. The validation data that results should be passed to the
|
||||
* `bindThreePid` method to complete the binding process.
|
||||
*
|
||||
* @param {string} email The email address to request a token for
|
||||
* @param {string} clientSecret A secret binary string generated by the client.
|
||||
@@ -1711,26 +1862,122 @@ MatrixBaseApis.prototype.uploadDeviceSigningKeys = function(auth, keys) {
|
||||
* @param {string} nextLink Optional If specified, the client will be redirected
|
||||
* to this link after validation.
|
||||
* @param {module:client.callback} callback Optional.
|
||||
* @param {string} identityAccessToken The `access_token` field of the identity
|
||||
* server `/account/register` response (see {@link registerWithIdentityServer}).
|
||||
*
|
||||
* @return {module:client.Promise} Resolves: TODO
|
||||
* @return {module:http-api.MatrixError} Rejects: with an error response.
|
||||
* @throws Error if No ID server is set
|
||||
* @throws Error if no identity server is set
|
||||
*/
|
||||
MatrixBaseApis.prototype.requestEmailToken = function(email, clientSecret,
|
||||
sendAttempt, nextLink, callback) {
|
||||
MatrixBaseApis.prototype.requestEmailToken = async function(
|
||||
email,
|
||||
clientSecret,
|
||||
sendAttempt,
|
||||
nextLink,
|
||||
callback,
|
||||
identityAccessToken,
|
||||
) {
|
||||
const params = {
|
||||
client_secret: clientSecret,
|
||||
email: email,
|
||||
send_attempt: sendAttempt,
|
||||
next_link: nextLink,
|
||||
};
|
||||
return this._http.idServerRequest(
|
||||
callback, "POST", "/validate/email/requestToken",
|
||||
params, httpApi.PREFIX_IDENTITY_V1,
|
||||
);
|
||||
|
||||
try {
|
||||
const response = await this._http.idServerRequest(
|
||||
undefined, "POST", "/validate/email/requestToken",
|
||||
params, httpApi.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, httpApi.PREFIX_IDENTITY_V1,
|
||||
);
|
||||
}
|
||||
if (callback) callback(err);
|
||||
throw err;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Submits an MSISDN token to the identity server
|
||||
* Requests a MSISDN verification token directly from an identity server.
|
||||
*
|
||||
* This API is used as part of binding a MSISDN for discovery on an identity
|
||||
* server. The validation data that results should be passed to the
|
||||
* `bindThreePid` method to complete the binding process.
|
||||
*
|
||||
* @param {string} phoneCountry The ISO 3166-1 alpha-2 code for the country in
|
||||
* which phoneNumber should be parsed relative to.
|
||||
* @param {string} phoneNumber The phone number, in national or international
|
||||
* format
|
||||
* @param {string} clientSecret A secret binary string generated by the client.
|
||||
* It is recommended this be around 16 ASCII characters.
|
||||
* @param {number} sendAttempt If an identity server sees a duplicate request
|
||||
* with the same sendAttempt, it will not send another SMS.
|
||||
* To request another SMS to be sent, use a larger value for
|
||||
* the sendAttempt param as was used in the previous request.
|
||||
* @param {string} nextLink Optional If specified, the client will be redirected
|
||||
* to this link after validation.
|
||||
* @param {module:client.callback} callback Optional.
|
||||
* @param {string} identityAccessToken The `access_token` field of the Identity
|
||||
* Server `/account/register` response (see {@link registerWithIdentityServer}).
|
||||
*
|
||||
* @return {module:client.Promise} Resolves: TODO
|
||||
* @return {module:http-api.MatrixError} Rejects: with an error response.
|
||||
* @throws Error if no identity server is set
|
||||
*/
|
||||
MatrixBaseApis.prototype.requestMsisdnToken = async function(
|
||||
phoneCountry,
|
||||
phoneNumber,
|
||||
clientSecret,
|
||||
sendAttempt,
|
||||
nextLink,
|
||||
callback,
|
||||
identityAccessToken,
|
||||
) {
|
||||
const params = {
|
||||
client_secret: clientSecret,
|
||||
country: phoneCountry,
|
||||
phone_number: phoneNumber,
|
||||
send_attempt: sendAttempt,
|
||||
next_link: nextLink,
|
||||
};
|
||||
|
||||
try {
|
||||
const response = await this._http.idServerRequest(
|
||||
undefined, "POST", "/validate/msisdn/requestToken",
|
||||
params, httpApi.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, httpApi.PREFIX_IDENTITY_V1,
|
||||
);
|
||||
}
|
||||
if (callback) callback(err);
|
||||
throw err;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Submits a MSISDN token to the identity server
|
||||
*
|
||||
* This is used when submitting the code sent by SMS to a phone number.
|
||||
* The ID server has an equivalent API for email but the js-sdk does
|
||||
@@ -1740,46 +1987,323 @@ MatrixBaseApis.prototype.requestEmailToken = function(email, clientSecret,
|
||||
* @param {string} sid The sid given in the response to requestToken
|
||||
* @param {string} clientSecret A secret binary string generated by the client.
|
||||
* This must be the same value submitted in the requestToken call.
|
||||
* @param {string} token The token, as enetered by the user.
|
||||
* @param {string} msisdnToken The MSISDN token, as enetered by the user.
|
||||
* @param {string} identityAccessToken The `access_token` field of the Identity
|
||||
* Server `/account/register` response (see {@link registerWithIdentityServer}).
|
||||
*
|
||||
* @return {module:client.Promise} Resolves: Object, currently with no parameters.
|
||||
* @return {module:http-api.MatrixError} Rejects: with an error response.
|
||||
* @throws Error if No ID server is set
|
||||
*/
|
||||
MatrixBaseApis.prototype.submitMsisdnToken = function(sid, clientSecret, token) {
|
||||
MatrixBaseApis.prototype.submitMsisdnToken = async function(
|
||||
sid,
|
||||
clientSecret,
|
||||
msisdnToken,
|
||||
identityAccessToken,
|
||||
) {
|
||||
const params = {
|
||||
sid: sid,
|
||||
client_secret: clientSecret,
|
||||
token: token,
|
||||
token: msisdnToken,
|
||||
};
|
||||
return this._http.idServerRequest(
|
||||
undefined, "POST", "/validate/msisdn/submitToken",
|
||||
params, httpApi.PREFIX_IDENTITY_V1,
|
||||
|
||||
try {
|
||||
return await this._http.idServerRequest(
|
||||
undefined, "POST", "/validate/msisdn/submitToken",
|
||||
params, httpApi.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, httpApi.PREFIX_IDENTITY_V1,
|
||||
);
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Submits a MSISDN token to an arbitrary URL.
|
||||
*
|
||||
* This is used when submitting the code sent by SMS to a phone number in the
|
||||
* newer 3PID flow where the homeserver validates 3PID ownership (as part of
|
||||
* `requestAdd3pidMsisdnToken`). The homeserver response may include a
|
||||
* `submit_url` to specify where the token should be sent, and this helper can
|
||||
* be used to pass the token to this URL.
|
||||
*
|
||||
* @param {string} url The URL to submit the token to
|
||||
* @param {string} sid The sid given in the response to requestToken
|
||||
* @param {string} clientSecret A secret binary string generated by the client.
|
||||
* This must be the same value submitted in the requestToken call.
|
||||
* @param {string} msisdnToken The MSISDN token, as enetered by the user.
|
||||
*
|
||||
* @return {module:client.Promise} Resolves: Object, currently with no parameters.
|
||||
* @return {module:http-api.MatrixError} Rejects: with an error response.
|
||||
*/
|
||||
MatrixBaseApis.prototype.submitMsisdnTokenOtherUrl = function(
|
||||
url,
|
||||
sid,
|
||||
clientSecret,
|
||||
msisdnToken,
|
||||
) {
|
||||
const params = {
|
||||
sid: sid,
|
||||
client_secret: clientSecret,
|
||||
token: msisdnToken,
|
||||
};
|
||||
|
||||
return this._http.requestOtherUrl(
|
||||
undefined, "POST", url, undefined, params,
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets the V2 hashing information from the identity server. Primarily useful for
|
||||
* lookups.
|
||||
* @param {string} identityAccessToken The access token for the identity server.
|
||||
* @returns {Promise<object>} The hashing information for the identity server.
|
||||
*/
|
||||
MatrixBaseApis.prototype.getIdentityHashDetails = function(identityAccessToken) {
|
||||
return this._http.idServerRequest(
|
||||
undefined, "GET", "/hash_details",
|
||||
null, httpApi.PREFIX_IDENTITY_V2, identityAccessToken,
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* Performs a hashed lookup of addresses against the identity server. This is
|
||||
* only supported on identity servers which have at least the version 2 API.
|
||||
* @param {Array<Array<string,string>>} addressPairs An array of 2 element arrays.
|
||||
* The first element of each pair is the address, the second is the 3PID medium.
|
||||
* Eg: ["email@example.org", "email"]
|
||||
* @param {string} identityAccessToken The access token for the identity server.
|
||||
* @returns {Promise<Array<{address, mxid}>>} A collection of address mappings to
|
||||
* found MXIDs. Results where no user could be found will not be listed.
|
||||
*/
|
||||
MatrixBaseApis.prototype.identityHashedLookup = async function(
|
||||
addressPairs, // [["email@example.org", "email"], ["10005550000", "msisdn"]]
|
||||
identityAccessToken,
|
||||
) {
|
||||
const params = {
|
||||
// addresses: ["email@example.org", "10005550000"],
|
||||
// algorithm: "sha256",
|
||||
// pepper: "abc123"
|
||||
};
|
||||
|
||||
// Get hash information first before trying to do a lookup
|
||||
const hashes = await this.getIdentityHashDetails(identityAccessToken);
|
||||
if (!hashes || !hashes['lookup_pepper'] || !hashes['algorithms']) {
|
||||
throw new Error("Unsupported identity server: bad response");
|
||||
}
|
||||
|
||||
params['pepper'] = hashes['lookup_pepper'];
|
||||
|
||||
const localMapping = {
|
||||
// hashed identifier => plain text address
|
||||
// For use in this function's return format
|
||||
};
|
||||
|
||||
// When picking an algorithm, we pick the hashed over no hashes
|
||||
if (hashes['algorithms'].includes('sha256')) {
|
||||
// Abuse the olm hashing
|
||||
const olmutil = new global.Olm.Utility();
|
||||
params["addresses"] = addressPairs.map(p => {
|
||||
const addr = p[0].toLowerCase(); // lowercase to get consistent hashes
|
||||
const med = p[1].toLowerCase();
|
||||
const hashed = olmutil.sha256(`${addr} ${med} ${params['pepper']}`)
|
||||
.replace(/\+/g, '-').replace(/\//g, '_'); // URL-safe base64
|
||||
// Map the hash to a known (case-sensitive) address. We use the case
|
||||
// sensitive version because the caller might be expecting that.
|
||||
localMapping[hashed] = p[0];
|
||||
return hashed;
|
||||
});
|
||||
params["algorithm"] = "sha256";
|
||||
} else if (hashes['algorithms'].includes('none')) {
|
||||
params["addresses"] = addressPairs.map(p => {
|
||||
const addr = p[0].toLowerCase(); // lowercase to get consistent hashes
|
||||
const med = p[1].toLowerCase();
|
||||
const unhashed = `${addr} ${med}`;
|
||||
// Map the unhashed values to a known (case-sensitive) address. We use
|
||||
// the case sensitive version because the caller might be expecting that.
|
||||
localMapping[unhashed] = p[0];
|
||||
return unhashed;
|
||||
});
|
||||
params["algorithm"] = "none";
|
||||
} else {
|
||||
throw new Error("Unsupported identity server: unknown hash algorithm");
|
||||
}
|
||||
|
||||
const response = await this._http.idServerRequest(
|
||||
undefined, "POST", "/lookup",
|
||||
params, httpApi.PREFIX_IDENTITY_V2, identityAccessToken,
|
||||
);
|
||||
|
||||
if (!response || !response['mappings']) return []; // no results
|
||||
|
||||
const foundAddresses = [/* {address: "plain@example.org", mxid} */];
|
||||
for (const hashed of Object.keys(response['mappings'])) {
|
||||
const mxid = response['mappings'][hashed];
|
||||
const plainAddress = localMapping[hashed];
|
||||
if (!plainAddress) {
|
||||
throw new Error("Identity server returned more results than expected");
|
||||
}
|
||||
|
||||
foundAddresses.push({address: plainAddress, mxid});
|
||||
}
|
||||
return foundAddresses;
|
||||
};
|
||||
|
||||
/**
|
||||
* Looks up the public Matrix ID mapping for a given 3rd party
|
||||
* identifier from the Identity Server
|
||||
*
|
||||
* @param {string} medium The medium of the threepid, eg. 'email'
|
||||
* @param {string} address The textual address of the threepid
|
||||
* @param {module:client.callback} callback Optional.
|
||||
* @param {string} identityAccessToken The `access_token` field of the Identity
|
||||
* Server `/account/register` response (see {@link registerWithIdentityServer}).
|
||||
*
|
||||
* @return {module:client.Promise} Resolves: A threepid mapping
|
||||
* object or the empty object if no mapping
|
||||
* exists
|
||||
* @return {module:http-api.MatrixError} Rejects: with an error response.
|
||||
*/
|
||||
MatrixBaseApis.prototype.lookupThreePid = function(medium, address, callback) {
|
||||
const params = {
|
||||
medium: medium,
|
||||
address: address,
|
||||
};
|
||||
return this._http.idServerRequest(
|
||||
callback, "GET", "/lookup",
|
||||
params, httpApi.PREFIX_IDENTITY_V1,
|
||||
);
|
||||
MatrixBaseApis.prototype.lookupThreePid = async function(
|
||||
medium,
|
||||
address,
|
||||
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, httpApi.PREFIX_IDENTITY_V1,
|
||||
);
|
||||
}
|
||||
if (callback) callback(err, undefined);
|
||||
throw err;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Looks up the public Matrix ID mappings for multiple 3PIDs.
|
||||
*
|
||||
* @param {Array.<Array.<string>>} query Array of arrays containing
|
||||
* [medium, address]
|
||||
* @param {string} identityAccessToken The `access_token` field of the Identity
|
||||
* Server `/account/register` response (see {@link registerWithIdentityServer}).
|
||||
*
|
||||
* @return {module:client.Promise} Resolves: Lookup results from IS.
|
||||
* @return {module:http-api.MatrixError} Rejects: with an error response.
|
||||
*/
|
||||
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");
|
||||
}
|
||||
|
||||
v1results.push([
|
||||
originalQuery[0], // medium
|
||||
mapping.address,
|
||||
mapping.mxid,
|
||||
]);
|
||||
}
|
||||
|
||||
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,
|
||||
httpApi.PREFIX_IDENTITY_V1, identityAccessToken,
|
||||
);
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Get account info from the Identity Server. This is useful as a neutral check
|
||||
* to verify that other APIs are likely to approve access by testing that the
|
||||
* token is valid, terms have been agreed, etc.
|
||||
*
|
||||
* @param {string} identityAccessToken The `access_token` field of the Identity
|
||||
* Server `/account/register` response (see {@link registerWithIdentityServer}).
|
||||
*
|
||||
* @return {module:client.Promise} Resolves: an object with account info.
|
||||
* @return {module:http-api.MatrixError} Rejects: with an error response.
|
||||
*/
|
||||
MatrixBaseApis.prototype.getIdentityAccount = function(
|
||||
identityAccessToken,
|
||||
) {
|
||||
return this._http.idServerRequest(
|
||||
undefined, "GET", "/account",
|
||||
undefined, httpApi.PREFIX_IDENTITY_V2, identityAccessToken,
|
||||
);
|
||||
};
|
||||
|
||||
// Direct-to-device messaging
|
||||
// ==========================
|
||||
@@ -1806,10 +2330,7 @@ MatrixBaseApis.prototype.sendToDevice = function(
|
||||
messages: contentMap,
|
||||
};
|
||||
|
||||
return this._http.authedRequestWithPrefix(
|
||||
undefined, "PUT", path, undefined, body,
|
||||
httpApi.PREFIX_UNSTABLE,
|
||||
);
|
||||
return this._http.authedRequest(undefined, "PUT", path, undefined, body);
|
||||
};
|
||||
|
||||
// Third party Lookup API
|
||||
@@ -1821,9 +2342,8 @@ MatrixBaseApis.prototype.sendToDevice = function(
|
||||
* @return {module:client.Promise} Resolves to the result object
|
||||
*/
|
||||
MatrixBaseApis.prototype.getThirdpartyProtocols = function() {
|
||||
return this._http.authedRequestWithPrefix(
|
||||
return this._http.authedRequest(
|
||||
undefined, "GET", "/thirdparty/protocols", undefined, undefined,
|
||||
httpApi.PREFIX_UNSTABLE,
|
||||
).then((response) => {
|
||||
// sanity check
|
||||
if (!response || typeof(response) !== 'object') {
|
||||
@@ -1848,10 +2368,7 @@ MatrixBaseApis.prototype.getThirdpartyLocation = function(protocol, params) {
|
||||
$protocol: protocol,
|
||||
});
|
||||
|
||||
return this._http.authedRequestWithPrefix(
|
||||
undefined, "GET", path, params, undefined,
|
||||
httpApi.PREFIX_UNSTABLE,
|
||||
);
|
||||
return this._http.authedRequest(undefined, "GET", path, params, undefined);
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -1867,12 +2384,45 @@ MatrixBaseApis.prototype.getThirdpartyUser = function(protocol, params) {
|
||||
$protocol: protocol,
|
||||
});
|
||||
|
||||
return this._http.authedRequestWithPrefix(
|
||||
undefined, "GET", path, params, undefined,
|
||||
httpApi.PREFIX_UNSTABLE,
|
||||
return this._http.authedRequest(undefined, "GET", path, params, undefined);
|
||||
};
|
||||
|
||||
MatrixBaseApis.prototype.getTerms = function(serviceType, baseUrl) {
|
||||
const url = termsUrlForService(serviceType, baseUrl);
|
||||
return this._http.requestOtherUrl(
|
||||
undefined, 'GET', url,
|
||||
);
|
||||
};
|
||||
|
||||
MatrixBaseApis.prototype.agreeToTerms = function(
|
||||
serviceType, baseUrl, accessToken, termsUrls,
|
||||
) {
|
||||
const url = termsUrlForService(serviceType, baseUrl);
|
||||
const headers = {
|
||||
Authorization: "Bearer " + accessToken,
|
||||
};
|
||||
return this._http.requestOtherUrl(
|
||||
undefined, 'POST', url, null, { user_accepts: termsUrls }, { headers },
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* Reports an event as inappropriate to the server, which may then notify the appropriate people.
|
||||
* @param {string} roomId The room in which the event being reported is located.
|
||||
* @param {string} eventId The event to report.
|
||||
* @param {number} score The score to rate this content as where -100 is most offensive and 0 is inoffensive.
|
||||
* @param {string} reason The reason the content is being reported. May be blank.
|
||||
* @returns {module:client.Promise} Resolves to an empty object if successful
|
||||
*/
|
||||
MatrixBaseApis.prototype.reportEvent = function(roomId, eventId, score, reason) {
|
||||
const path = utils.encodeUri("/rooms/$roomId/report/$eventId", {
|
||||
$roomId: roomId,
|
||||
$eventId: eventId,
|
||||
});
|
||||
|
||||
return this._http.authedRequest(undefined, "POST", path, null, {score, reason});
|
||||
};
|
||||
|
||||
/**
|
||||
* MatrixBaseApis object
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user