@@ -108,6 +108,15 @@ function keyFromRecoverySession(session, decryptionKey) {
108
108
*
109
109
* @param {string } opts.userId The user ID for this user.
110
110
*
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
+ *
111
120
* @param {Object= } opts.store
112
121
* The data store used for sync data from the homeserver. If not specified,
113
122
* this client will not store any HTTP responses. The `createClient` helper
@@ -2438,7 +2447,12 @@ MatrixClient.prototype.inviteByEmail = function(roomId, email, callback) {
2438
2447
* @return {module:client.Promise } Resolves: TODO
2439
2448
* @return {module:http-api.MatrixError } Rejects: with an error response.
2440
2449
*/
2441
- MatrixClient . prototype . inviteByThreePid = function ( roomId , medium , address , callback ) {
2450
+ MatrixClient . prototype . inviteByThreePid = async function (
2451
+ roomId ,
2452
+ medium ,
2453
+ address ,
2454
+ callback ,
2455
+ ) {
2442
2456
const path = utils . encodeUri (
2443
2457
"/rooms/$roomId/invite" ,
2444
2458
{ $roomId : roomId } ,
@@ -2451,12 +2465,24 @@ MatrixClient.prototype.inviteByThreePid = function(roomId, medium, address, call
2451
2465
errcode : "ORG.MATRIX.JSSDK_MISSING_PARAM" ,
2452
2466
} ) ) ;
2453
2467
}
2454
-
2455
- return this . _http . authedRequest ( callback , "POST" , path , undefined , {
2468
+ const params = {
2456
2469
id_server : identityServerUrl ,
2457
2470
medium : medium ,
2458
2471
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 ) ;
2460
2486
} ;
2461
2487
2462
2488
/**
@@ -3423,7 +3449,7 @@ MatrixClient.prototype.requestPasswordMsisdnToken = function(phoneCountry, phone
3423
3449
* @param {object } params Parameters for the POST request
3424
3450
* @return {module:client.Promise } Resolves: As requestEmailToken
3425
3451
*/
3426
- MatrixClient . prototype . _requestTokenFromEndpoint = function ( endpoint , params ) {
3452
+ MatrixClient . prototype . _requestTokenFromEndpoint = async function ( endpoint , params ) {
3427
3453
const postParams = Object . assign ( { } , params ) ;
3428
3454
3429
3455
if ( this . idBaseUrl ) {
@@ -3432,6 +3458,17 @@ MatrixClient.prototype._requestTokenFromEndpoint = function(endpoint, params) {
3432
3458
throw new Error ( "Invalid ID server URL: " + this . idBaseUrl ) ;
3433
3459
}
3434
3460
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
+ }
3435
3472
}
3436
3473
3437
3474
return this . _http . request (
@@ -4092,6 +4129,23 @@ MatrixClient.prototype.doesServerRequireIdServerParam = async function() {
4092
4129
}
4093
4130
} ;
4094
4131
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
+
4095
4149
/*
4096
4150
* Get if lazy loading members is being used.
4097
4151
* @return {boolean } Whether or not members are lazy loaded by this client
0 commit comments