@@ -16,18 +16,17 @@ See the License for the specific language governing permissions and
1616limitations under the License.
1717*/
1818
19- import { IAuthData , IRequestMsisdnTokenResponse , IRequestTokenResponse } from "matrix-js-sdk/src/matrix" ;
19+ import { IAuthData , IRequestMsisdnTokenResponse , IRequestTokenResponse , MatrixClient } from "matrix-js-sdk/src/matrix" ;
2020import { MatrixError , HTTPError } from "matrix-js-sdk/src/matrix" ;
2121
22- import { MatrixClientPeg } from "./MatrixClientPeg" ;
2322import Modal from "./Modal" ;
2423import { _t , UserFriendlyError } from "./languageHandler" ;
2524import IdentityAuthClient from "./IdentityAuthClient" ;
2625import { SSOAuthEntry } from "./components/views/auth/InteractiveAuthEntryComponents" ;
2726import InteractiveAuthDialog from "./components/views/dialogs/InteractiveAuthDialog" ;
2827
29- function getIdServerDomain ( ) : string {
30- const idBaseUrl = MatrixClientPeg . get ( ) . getIdentityServerUrl ( true ) ;
28+ function getIdServerDomain ( matrixClient : MatrixClient ) : string {
29+ const idBaseUrl = matrixClient . getIdentityServerUrl ( true ) ;
3130 if ( ! idBaseUrl ) {
3231 throw new UserFriendlyError ( "Identity server not set" ) ;
3332 }
@@ -55,11 +54,11 @@ export type Binding = {
5554export default class AddThreepid {
5655 private sessionId : string ;
5756 private submitUrl ?: string ;
58- private clientSecret : string ;
5957 private bind = false ;
58+ private readonly clientSecret : string ;
6059
61- public constructor ( ) {
62- this . clientSecret = MatrixClientPeg . get ( ) . generateClientSecret ( ) ;
60+ public constructor ( private readonly matrixClient : MatrixClient ) {
61+ this . clientSecret = matrixClient . generateClientSecret ( ) ;
6362 }
6463
6564 /**
@@ -70,7 +69,7 @@ export default class AddThreepid {
7069 */
7170 public async addEmailAddress ( emailAddress : string ) : Promise < IRequestTokenResponse > {
7271 try {
73- const res = await MatrixClientPeg . get ( ) . requestAdd3pidEmailToken ( emailAddress , this . clientSecret , 1 ) ;
72+ const res = await this . matrixClient . requestAdd3pidEmailToken ( emailAddress , this . clientSecret , 1 ) ;
7473 this . sessionId = res . sid ;
7574 return res ;
7675 } catch ( err ) {
@@ -90,12 +89,12 @@ export default class AddThreepid {
9089 */
9190 public async bindEmailAddress ( emailAddress : string ) : Promise < IRequestTokenResponse > {
9291 this . bind = true ;
93- if ( await MatrixClientPeg . get ( ) . doesServerSupportSeparateAddAndBind ( ) ) {
92+ if ( await this . matrixClient . doesServerSupportSeparateAddAndBind ( ) ) {
9493 // For separate bind, request a token directly from the IS.
9594 const authClient = new IdentityAuthClient ( ) ;
9695 const identityAccessToken = ( await authClient . getAccessToken ( ) ) ?? undefined ;
9796 try {
98- const res = await MatrixClientPeg . get ( ) . requestEmailToken (
97+ const res = await this . matrixClient . requestEmailToken (
9998 emailAddress ,
10099 this . clientSecret ,
101100 1 ,
@@ -126,7 +125,7 @@ export default class AddThreepid {
126125 */
127126 public async addMsisdn ( phoneCountry : string , phoneNumber : string ) : Promise < IRequestMsisdnTokenResponse > {
128127 try {
129- const res = await MatrixClientPeg . get ( ) . requestAdd3pidMsisdnToken (
128+ const res = await this . matrixClient . requestAdd3pidMsisdnToken (
130129 phoneCountry ,
131130 phoneNumber ,
132131 this . clientSecret ,
@@ -153,12 +152,12 @@ export default class AddThreepid {
153152 */
154153 public async bindMsisdn ( phoneCountry : string , phoneNumber : string ) : Promise < IRequestMsisdnTokenResponse > {
155154 this . bind = true ;
156- if ( await MatrixClientPeg . get ( ) . doesServerSupportSeparateAddAndBind ( ) ) {
155+ if ( await this . matrixClient . doesServerSupportSeparateAddAndBind ( ) ) {
157156 // For separate bind, request a token directly from the IS.
158157 const authClient = new IdentityAuthClient ( ) ;
159158 const identityAccessToken = ( await authClient . getAccessToken ( ) ) ?? undefined ;
160159 try {
161- const res = await MatrixClientPeg . get ( ) . requestMsisdnToken (
160+ const res = await this . matrixClient . requestMsisdnToken (
162161 phoneCountry ,
163162 phoneNumber ,
164163 this . clientSecret ,
@@ -189,17 +188,17 @@ export default class AddThreepid {
189188 */
190189 public async checkEmailLinkClicked ( ) : Promise < [ success ?: boolean , result ?: IAuthData | Error | null ] > {
191190 try {
192- if ( await MatrixClientPeg . get ( ) . doesServerSupportSeparateAddAndBind ( ) ) {
191+ if ( await this . matrixClient . doesServerSupportSeparateAddAndBind ( ) ) {
193192 if ( this . bind ) {
194193 const authClient = new IdentityAuthClient ( ) ;
195194 const identityAccessToken = await authClient . getAccessToken ( ) ;
196195 if ( ! identityAccessToken ) {
197196 throw new UserFriendlyError ( "No identity access token found" ) ;
198197 }
199- await MatrixClientPeg . get ( ) . bindThreePid ( {
198+ await this . matrixClient . bindThreePid ( {
200199 sid : this . sessionId ,
201200 client_secret : this . clientSecret ,
202- id_server : getIdServerDomain ( ) ,
201+ id_server : getIdServerDomain ( this . matrixClient ) ,
203202 id_access_token : identityAccessToken ,
204203 } ) ;
205204 } else {
@@ -233,7 +232,7 @@ export default class AddThreepid {
233232 } ;
234233 const { finished } = Modal . createDialog ( InteractiveAuthDialog , {
235234 title : _t ( "Add Email Address" ) ,
236- matrixClient : MatrixClientPeg . get ( ) ,
235+ matrixClient : this . matrixClient ,
237236 authData : err . data ,
238237 makeRequest : this . makeAddThreepidOnlyRequest ,
239238 aestheticsForStagePhases : {
@@ -245,11 +244,11 @@ export default class AddThreepid {
245244 }
246245 }
247246 } else {
248- await MatrixClientPeg . get ( ) . addThreePid (
247+ await this . matrixClient . addThreePid (
249248 {
250249 sid : this . sessionId ,
251250 client_secret : this . clientSecret ,
252- id_server : getIdServerDomain ( ) ,
251+ id_server : getIdServerDomain ( this . matrixClient ) ,
253252 } ,
254253 this . bind ,
255254 ) ;
@@ -272,7 +271,7 @@ export default class AddThreepid {
272271 * @return {Promise<Object> } Response from /3pid/add call (in current spec, an empty object)
273272 */
274273 private makeAddThreepidOnlyRequest = ( auth ?: { type : string ; session ?: string } ) : Promise < { } > => {
275- return MatrixClientPeg . get ( ) . addThreePidOnly ( {
274+ return this . matrixClient . addThreePidOnly ( {
276275 sid : this . sessionId ,
277276 client_secret : this . clientSecret ,
278277 auth,
@@ -291,18 +290,18 @@ export default class AddThreepid {
291290 msisdnToken : string ,
292291 ) : Promise < [ success ?: boolean , result ?: IAuthData | Error | null ] | undefined > {
293292 const authClient = new IdentityAuthClient ( ) ;
294- const supportsSeparateAddAndBind = await MatrixClientPeg . get ( ) . doesServerSupportSeparateAddAndBind ( ) ;
293+ const supportsSeparateAddAndBind = await this . matrixClient . doesServerSupportSeparateAddAndBind ( ) ;
295294
296295 let result : { success : boolean } | MatrixError ;
297296 if ( this . submitUrl ) {
298- result = await MatrixClientPeg . get ( ) . submitMsisdnTokenOtherUrl (
297+ result = await this . matrixClient . submitMsisdnTokenOtherUrl (
299298 this . submitUrl ,
300299 this . sessionId ,
301300 this . clientSecret ,
302301 msisdnToken ,
303302 ) ;
304303 } else if ( this . bind || ! supportsSeparateAddAndBind ) {
305- result = await MatrixClientPeg . get ( ) . submitMsisdnToken (
304+ result = await this . matrixClient . submitMsisdnToken (
306305 this . sessionId ,
307306 this . clientSecret ,
308307 msisdnToken ,
@@ -317,10 +316,10 @@ export default class AddThreepid {
317316
318317 if ( supportsSeparateAddAndBind ) {
319318 if ( this . bind ) {
320- await MatrixClientPeg . get ( ) . bindThreePid ( {
319+ await this . matrixClient . bindThreePid ( {
321320 sid : this . sessionId ,
322321 client_secret : this . clientSecret ,
323- id_server : getIdServerDomain ( ) ,
322+ id_server : getIdServerDomain ( this . matrixClient ) ,
324323 id_access_token : await authClient . getAccessToken ( ) ,
325324 } ) ;
326325 } else {
@@ -354,7 +353,7 @@ export default class AddThreepid {
354353 } ;
355354 const { finished } = Modal . createDialog ( InteractiveAuthDialog , {
356355 title : _t ( "Add Phone Number" ) ,
357- matrixClient : MatrixClientPeg . get ( ) ,
356+ matrixClient : this . matrixClient ,
358357 authData : err . data ,
359358 makeRequest : this . makeAddThreepidOnlyRequest ,
360359 aestheticsForStagePhases : {
@@ -366,11 +365,11 @@ export default class AddThreepid {
366365 }
367366 }
368367 } else {
369- await MatrixClientPeg . get ( ) . addThreePid (
368+ await this . matrixClient . addThreePid (
370369 {
371370 sid : this . sessionId ,
372371 client_secret : this . clientSecret ,
373- id_server : getIdServerDomain ( ) ,
372+ id_server : getIdServerDomain ( this . matrixClient ) ,
374373 } ,
375374 this . bind ,
376375 ) ;
0 commit comments