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