@@ -30,18 +30,18 @@ const DEHYDRATION_PREFIX = "/_matrix/client/unstable/org.matrix.msc2697.v2";
30
30
31
31
type Options = {
32
32
homeserver : string ;
33
- accessToken : string ;
33
+ accessToken : BaseObservableValue < string > ;
34
34
request : RequestFunction ;
35
35
reconnector : Reconnector ;
36
36
} ;
37
37
38
38
export class HomeServerApi {
39
39
private readonly _homeserver : string ;
40
- private readonly _accessToken : string ;
40
+ private readonly _accessToken : BaseObservableValue < string > ;
41
41
private readonly _requestFn : RequestFunction ;
42
42
private readonly _reconnector : Reconnector ;
43
43
44
- constructor ( { homeserver, accessToken, request, reconnector} : Options ) {
44
+ constructor ( { homeserver, accessToken, request, reconnector } : Options ) {
45
45
// store these both in a closure somehow so it's harder to get at in case of XSS?
46
46
// one could change the homeserver as well so the token gets sent there, so both must be protected from read/write
47
47
this . _homeserver = homeserver ;
@@ -54,7 +54,7 @@ export class HomeServerApi {
54
54
return this . _homeserver + prefix + csPath ;
55
55
}
56
56
57
- private _baseRequest ( method : RequestMethod , url : string , queryParams ?: Record < string , any > , body ?: Record < string , any > , options ?: IRequestOptions , accessToken ?: string ) : IHomeServerRequest {
57
+ private _baseRequest ( method : RequestMethod , url : string , queryParams ?: Record < string , any > , body ?: Record < string , any > , options ?: IRequestOptions , accessTokenSource ?: BaseObservableValue < string > ) : IHomeServerRequest {
58
58
const queryString = encodeQueryParams ( queryParams ) ;
59
59
url = `${ url } ?${ queryString } ` ;
60
60
let log : ILogItem | undefined ;
@@ -68,9 +68,14 @@ export class HomeServerApi {
68
68
}
69
69
let encodedBody : EncodedBody [ "body" ] ;
70
70
const headers : Map < string , string | number > = new Map ( ) ;
71
+
72
+ let accessToken : string | null = null ;
71
73
if ( options ?. accessTokenOverride ) {
72
74
accessToken = options . accessTokenOverride ;
75
+ } else if ( accessTokenSource ) {
76
+ accessToken = accessTokenSource . get ( ) ;
73
77
}
78
+
74
79
if ( accessToken ) {
75
80
headers . set ( "Authorization" , `Bearer ${ accessToken } ` ) ;
76
81
}
@@ -91,7 +96,7 @@ export class HomeServerApi {
91
96
} ) ;
92
97
93
98
const hsRequest = new HomeServerRequest ( method , url , requestResult , log ) ;
94
-
99
+
95
100
if ( this . _reconnector ) {
96
101
hsRequest . response ( ) . catch ( err => {
97
102
// Some endpoints such as /sync legitimately time-out
@@ -282,11 +287,12 @@ export class HomeServerApi {
282
287
283
288
claimDehydratedDevice ( deviceId : string , options : IRequestOptions ) : IHomeServerRequest {
284
289
options . prefix = DEHYDRATION_PREFIX ;
285
- return this . _post ( `/dehydrated_device/claim` , { } , { device_id : deviceId } , options ) ;
290
+ return this . _post ( `/dehydrated_device/claim` , { } , { device_id : deviceId } , options ) ;
286
291
}
287
292
}
288
293
289
- import { Request as MockRequest } from "../../mocks/Request.js" ;
294
+ import { Request as MockRequest } from "../../mocks/Request.js" ;
295
+ import { BaseObservableValue } from "../../observable/ObservableValue" ;
290
296
291
297
export function tests ( ) {
292
298
return {
0 commit comments