Skip to content

Commit 09b0033

Browse files
authored
Merge pull request #1022 from matrix-org/jryans/is-token-to-hs
Send id_access_token to HS for use in proxied IS requests
2 parents 3e2ffb2 + 3d27481 commit 09b0033

File tree

2 files changed

+69
-5
lines changed

2 files changed

+69
-5
lines changed

src/base-apis.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,15 @@ function termsUrlForService(serviceType, baseUrl) {
6363
*
6464
* @param {string} opts.accessToken The access_token for this user.
6565
*
66+
* @param {IdentityServerProvider} [opts.identityServer]
67+
* Optional. A provider object with one function `getAccessToken`, which is a
68+
* callback that returns a Promise<String> of an identity access token to supply
69+
* with identity requests. If the object is unset, no access token will be
70+
* supplied.
71+
* See also https://github.com/vector-im/riot-web/issues/10615 which seeks to
72+
* replace the previous approach of manual access tokens params with this
73+
* callback throughout the SDK.
74+
*
6675
* @param {Number=} opts.localTimeoutMs Optional. The default maximum amount of
6776
* time to wait before timing out HTTP requests. If not specified, there is no
6877
* timeout.
@@ -79,6 +88,7 @@ function MatrixBaseApis(opts) {
7988

8089
this.baseUrl = opts.baseUrl;
8190
this.idBaseUrl = opts.idBaseUrl;
91+
this.identityServer = opts.identityServer;
8292

8393
const httpOpts = {
8494
baseUrl: opts.baseUrl,

src/client.js

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@ function keyFromRecoverySession(session, decryptionKey) {
108108
*
109109
* @param {string} opts.userId The user ID for this user.
110110
*
111+
* @param {IdentityServerProvider} [opts.identityServer]
112+
* Optional. A provider object with one function `getAccessToken`, which is a
113+
* callback that returns a Promise<String> of an identity access token to supply
114+
* with identity requests. If the object is unset, no access token will be
115+
* supplied.
116+
* See also https://github.com/vector-im/riot-web/issues/10615 which seeks to
117+
* replace the previous approach of manual access tokens params with this
118+
* callback throughout the SDK.
119+
*
111120
* @param {Object=} opts.store
112121
* The data store used for sync data from the homeserver. If not specified,
113122
* this client will not store any HTTP responses. The `createClient` helper
@@ -2438,7 +2447,12 @@ MatrixClient.prototype.inviteByEmail = function(roomId, email, callback) {
24382447
* @return {module:client.Promise} Resolves: TODO
24392448
* @return {module:http-api.MatrixError} Rejects: with an error response.
24402449
*/
2441-
MatrixClient.prototype.inviteByThreePid = function(roomId, medium, address, callback) {
2450+
MatrixClient.prototype.inviteByThreePid = async function(
2451+
roomId,
2452+
medium,
2453+
address,
2454+
callback,
2455+
) {
24422456
const path = utils.encodeUri(
24432457
"/rooms/$roomId/invite",
24442458
{ $roomId: roomId },
@@ -2451,12 +2465,24 @@ MatrixClient.prototype.inviteByThreePid = function(roomId, medium, address, call
24512465
errcode: "ORG.MATRIX.JSSDK_MISSING_PARAM",
24522466
}));
24532467
}
2454-
2455-
return this._http.authedRequest(callback, "POST", path, undefined, {
2468+
const params = {
24562469
id_server: identityServerUrl,
24572470
medium: medium,
24582471
address: address,
2459-
});
2472+
};
2473+
2474+
if (
2475+
this.identityServer &&
2476+
this.identityServer.getAccessToken &&
2477+
await this.doesServerAcceptIdentityAccessToken()
2478+
) {
2479+
const identityAccessToken = await this.identityServer.getAccessToken();
2480+
if (identityAccessToken) {
2481+
params.id_access_token = identityAccessToken;
2482+
}
2483+
}
2484+
2485+
return this._http.authedRequest(callback, "POST", path, undefined, params);
24602486
};
24612487

24622488
/**
@@ -3423,7 +3449,7 @@ MatrixClient.prototype.requestPasswordMsisdnToken = function(phoneCountry, phone
34233449
* @param {object} params Parameters for the POST request
34243450
* @return {module:client.Promise} Resolves: As requestEmailToken
34253451
*/
3426-
MatrixClient.prototype._requestTokenFromEndpoint = function(endpoint, params) {
3452+
MatrixClient.prototype._requestTokenFromEndpoint = async function(endpoint, params) {
34273453
const postParams = Object.assign({}, params);
34283454

34293455
if (this.idBaseUrl) {
@@ -3432,6 +3458,17 @@ MatrixClient.prototype._requestTokenFromEndpoint = function(endpoint, params) {
34323458
throw new Error("Invalid ID server URL: " + this.idBaseUrl);
34333459
}
34343460
postParams.id_server = idServerUrl.host;
3461+
3462+
if (
3463+
this.identityServer &&
3464+
this.identityServer.getAccessToken &&
3465+
await this.doesServerAcceptIdentityAccessToken()
3466+
) {
3467+
const identityAccessToken = await this.identityServer.getAccessToken();
3468+
if (identityAccessToken) {
3469+
postParams.id_access_token = identityAccessToken;
3470+
}
3471+
}
34353472
}
34363473

34373474
return this._http.request(
@@ -4092,6 +4129,23 @@ MatrixClient.prototype.doesServerRequireIdServerParam = async function() {
40924129
}
40934130
};
40944131

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

0 commit comments

Comments
 (0)