Skip to content

Commit 31e72ef

Browse files
committed
Send id_access_token to HS for use in proxied IS requests
This passes along the `id_access_token` to the HS, which it will need when speaking v2 IS APIs to the IS. Unfortunately, some HSes seem to explode when given this new parameter, so we only pass it along for the moment if an unstable feature `m.id_access_token` is also set. Part of element-hq/element-web#10525 Defined in MSC2140
1 parent 898fa0e commit 31e72ef

File tree

2 files changed

+65
-5
lines changed

2 files changed

+65
-5
lines changed

src/base-apis.js

+9
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ function termsUrlForService(serviceType, baseUrl) {
6363
*
6464
* @param {string} opts.accessToken The access_token for this user.
6565
*
66+
* @param {Function} [opts.getIdentityAccessToken]
67+
* Optional. A callback that returns a Promise<String> of an identity access
68+
* token to supply with identity requests. If the callback is unset, no access
69+
* token will be supplied.
70+
* See also https://github.com/vector-im/riot-web/issues/10615 which seeks to
71+
* replace the previous approach of manual access tokens params with this
72+
* callback throughout the SDK.
73+
*
6674
* @param {Number=} opts.localTimeoutMs Optional. The default maximum amount of
6775
* time to wait before timing out HTTP requests. If not specified, there is no
6876
* timeout.
@@ -79,6 +87,7 @@ function MatrixBaseApis(opts) {
7987

8088
this.baseUrl = opts.baseUrl;
8189
this.idBaseUrl = opts.idBaseUrl;
90+
this.getIdentityAccessToken = opts.getIdentityAccessToken;
8291

8392
const httpOpts = {
8493
baseUrl: opts.baseUrl,

src/client.js

+56-5
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ function keyFromRecoverySession(session, decryptionKey) {
108108
*
109109
* @param {string} opts.userId The user ID for this user.
110110
*
111+
* @param {Function} [opts.getIdentityAccessToken]
112+
* Optional. A callback that returns a Promise<String> of an identity access
113+
* token to supply with identity requests. If the callback is unset, no access
114+
* token will be supplied.
115+
* See also https://github.com/vector-im/riot-web/issues/10615 which seeks to
116+
* replace the previous approach of manual access tokens params with this
117+
* callback throughout the SDK.
118+
*
111119
* @param {Object=} opts.store
112120
* The data store used for sync data from the homeserver. If not specified,
113121
* this client will not store any HTTP responses. The `createClient` helper
@@ -2438,7 +2446,12 @@ MatrixClient.prototype.inviteByEmail = function(roomId, email, callback) {
24382446
* @return {module:client.Promise} Resolves: TODO
24392447
* @return {module:http-api.MatrixError} Rejects: with an error response.
24402448
*/
2441-
MatrixClient.prototype.inviteByThreePid = function(roomId, medium, address, callback) {
2449+
MatrixClient.prototype.inviteByThreePid = async function(
2450+
roomId,
2451+
medium,
2452+
address,
2453+
callback,
2454+
) {
24422455
const path = utils.encodeUri(
24432456
"/rooms/$roomId/invite",
24442457
{ $roomId: roomId },
@@ -2451,12 +2464,23 @@ MatrixClient.prototype.inviteByThreePid = function(roomId, medium, address, call
24512464
errcode: "ORG.MATRIX.JSSDK_MISSING_PARAM",
24522465
}));
24532466
}
2454-
2455-
return this._http.authedRequest(callback, "POST", path, undefined, {
2467+
const params = {
24562468
id_server: identityServerUrl,
24572469
medium: medium,
24582470
address: address,
2459-
});
2471+
};
2472+
2473+
if (
2474+
this.getIdentityAccessToken &&
2475+
await this.doesServerAcceptIdentityAccessToken()
2476+
) {
2477+
const identityAccessToken = await this.getIdentityAccessToken();
2478+
if (identityAccessToken) {
2479+
params.id_access_token = identityAccessToken;
2480+
}
2481+
}
2482+
2483+
return this._http.authedRequest(callback, "POST", path, undefined, params);
24602484
};
24612485

24622486
/**
@@ -3423,7 +3447,7 @@ MatrixClient.prototype.requestPasswordMsisdnToken = function(phoneCountry, phone
34233447
* @param {object} params Parameters for the POST request
34243448
* @return {module:client.Promise} Resolves: As requestEmailToken
34253449
*/
3426-
MatrixClient.prototype._requestTokenFromEndpoint = function(endpoint, params) {
3450+
MatrixClient.prototype._requestTokenFromEndpoint = async function(endpoint, params) {
34273451
const postParams = Object.assign({}, params);
34283452

34293453
if (this.idBaseUrl) {
@@ -3432,6 +3456,16 @@ MatrixClient.prototype._requestTokenFromEndpoint = function(endpoint, params) {
34323456
throw new Error("Invalid ID server URL: " + this.idBaseUrl);
34333457
}
34343458
postParams.id_server = idServerUrl.host;
3459+
3460+
if (
3461+
this.getIdentityAccessToken &&
3462+
await this.doesServerAcceptIdentityAccessToken()
3463+
) {
3464+
const identityAccessToken = await this.getIdentityAccessToken();
3465+
if (identityAccessToken) {
3466+
postParams.id_access_token = identityAccessToken;
3467+
}
3468+
}
34353469
}
34363470

34373471
return this._http.request(
@@ -4092,6 +4126,23 @@ MatrixClient.prototype.doesServerRequireIdServerParam = async function() {
40924126
}
40934127
};
40944128

4129+
/*
4130+
* Query the server to see if the `id_access_token` parameter can be safely
4131+
* passed to the homeserver. Some homeservers may trigger errors if they are not
4132+
* prepared for the new parameter.
4133+
* @return {Promise<boolean>} true if id_access_token can be sent
4134+
*/
4135+
MatrixClient.prototype.doesServerAcceptIdentityAccessToken = async function() {
4136+
const response = await this.getVersions();
4137+
4138+
const unstableFeatures = response["unstable_features"];
4139+
if (unstableFeatures["m.id_access_token"] === undefined) {
4140+
return false;
4141+
}
4142+
4143+
return unstableFeatures["m.id_access_token"];
4144+
};
4145+
40954146
/*
40964147
* Get if lazy loading members is being used.
40974148
* @return {boolean} Whether or not members are lazy loaded by this client

0 commit comments

Comments
 (0)