@@ -29,20 +29,43 @@ import * as Matrix from 'matrix-js-sdk';
2929// The version of the integration manager API we're intending to work with
3030const imApiVersion = "1.1" ;
3131
32- class ScalarAuthClient {
33- constructor ( ) {
32+ export default class ScalarAuthClient {
33+ constructor ( apiUrl , uiUrl ) {
34+ this . apiUrl = apiUrl ;
35+ this . uiUrl = uiUrl ;
3436 this . scalarToken = null ;
3537 // `undefined` to allow `startTermsFlow` to fallback to a default
3638 // callback if this is unset.
3739 this . termsInteractionCallback = undefined ;
40+
41+ // We try and store the token on a per-manager basis, but need a fallback
42+ // for the default manager.
43+ const configApiUrl = SdkConfig . get ( ) [ 'integrations_rest_url' ] ;
44+ const configUiUrl = SdkConfig . get ( ) [ 'integrations_ui_url' ] ;
45+ this . isDefaultManager = apiUrl === configApiUrl && configUiUrl === uiUrl ;
3846 }
3947
40- /**
41- * Determines if setting up a ScalarAuthClient is even possible
42- * @returns {boolean } true if possible, false otherwise.
43- */
44- static isPossible ( ) {
45- return SdkConfig . get ( ) [ 'integrations_rest_url' ] && SdkConfig . get ( ) [ 'integrations_ui_url' ] ;
48+ _writeTokenToStore ( ) {
49+ window . localStorage . setItem ( "mx_scalar_token_at_" + this . apiUrl , this . scalarToken ) ;
50+ if ( this . isDefaultManager ) {
51+ // We remove the old token from storage to migrate upwards. This is safe
52+ // to do because even if the user switches to /app when this is on /develop
53+ // they'll at worst register for a new token.
54+ window . localStorage . removeItem ( "mx_scalar_token" ) ; // no-op when not present
55+ }
56+ }
57+
58+ _readTokenFromStore ( ) {
59+ let token = window . localStorage . getItem ( "mx_scalar_token_at_" + this . apiUrl ) ;
60+ if ( ! token && this . isDefaultManager ) {
61+ token = window . localStorage . getItem ( "mx_scalar_token" ) ;
62+ }
63+ return token ;
64+ }
65+
66+ _readToken ( ) {
67+ if ( this . scalarToken ) return this . scalarToken ;
68+ return this . _readTokenFromStore ( ) ;
4669 }
4770
4871 setTermsInteractionCallback ( callback ) {
@@ -61,8 +84,7 @@ class ScalarAuthClient {
6184
6285 // Returns a promise that resolves to a scalar_token string
6386 getScalarToken ( ) {
64- let token = this . scalarToken ;
65- if ( ! token ) token = window . localStorage . getItem ( "mx_scalar_token" ) ;
87+ const token = this . _readToken ( ) ;
6688
6789 if ( ! token ) {
6890 return this . registerForToken ( ) ;
@@ -78,7 +100,7 @@ class ScalarAuthClient {
78100 }
79101
80102 _getAccountName ( token ) {
81- const url = SdkConfig . get ( ) . integrations_rest_url + "/account" ;
103+ const url = this . apiUrl + "/account" ;
82104
83105 return new Promise ( function ( resolve , reject ) {
84106 request ( {
@@ -111,7 +133,7 @@ class ScalarAuthClient {
111133 return token ;
112134 } ) . catch ( ( e ) => {
113135 if ( e instanceof TermsNotSignedError ) {
114- console . log ( "Integrations manager requires new terms to be agreed to" ) ;
136+ console . log ( "Integration manager requires new terms to be agreed to" ) ;
115137 // The terms endpoints are new and so live on standard _matrix prefixes,
116138 // but IM rest urls are currently configured with paths, so remove the
117139 // path from the base URL before passing it to the js-sdk
@@ -126,7 +148,7 @@ class ScalarAuthClient {
126148 // Once we've fully transitioned to _matrix URLs, we can give people
127149 // a grace period to update their configs, then use the rest url as
128150 // a regular base url.
129- const parsedImRestUrl = url . parse ( SdkConfig . get ( ) . integrations_rest_url ) ;
151+ const parsedImRestUrl = url . parse ( this . apiUrl ) ;
130152 parsedImRestUrl . path = '' ;
131153 parsedImRestUrl . pathname = '' ;
132154 return startTermsFlow ( [ new Service (
@@ -147,17 +169,18 @@ class ScalarAuthClient {
147169 return MatrixClientPeg . get ( ) . getOpenIdToken ( ) . then ( ( tokenObject ) => {
148170 // Now we can send that to scalar and exchange it for a scalar token
149171 return this . exchangeForScalarToken ( tokenObject ) ;
150- } ) . then ( ( tokenObject ) => {
172+ } ) . then ( ( token ) => {
151173 // Validate it (this mostly checks to see if the IM needs us to agree to some terms)
152- return this . _checkToken ( tokenObject ) ;
153- } ) . then ( ( tokenObject ) => {
154- window . localStorage . setItem ( "mx_scalar_token" , tokenObject ) ;
155- return tokenObject ;
174+ return this . _checkToken ( token ) ;
175+ } ) . then ( ( token ) => {
176+ this . scalarToken = token ;
177+ this . _writeTokenToStore ( ) ;
178+ return token ;
156179 } ) ;
157180 }
158181
159182 exchangeForScalarToken ( openidTokenObject ) {
160- const scalarRestUrl = SdkConfig . get ( ) . integrations_rest_url ;
183+ const scalarRestUrl = this . apiUrl ;
161184
162185 return new Promise ( function ( resolve , reject ) {
163186 request ( {
@@ -181,7 +204,7 @@ class ScalarAuthClient {
181204 }
182205
183206 getScalarPageTitle ( url ) {
184- let scalarPageLookupUrl = SdkConfig . get ( ) . integrations_rest_url + '/widgets/title_lookup' ;
207+ let scalarPageLookupUrl = this . apiUrl + '/widgets/title_lookup' ;
185208 scalarPageLookupUrl = this . getStarterLink ( scalarPageLookupUrl ) ;
186209 scalarPageLookupUrl += '&curl=' + encodeURIComponent ( url ) ;
187210
@@ -217,7 +240,7 @@ class ScalarAuthClient {
217240 * @return {Promise } Resolves on completion
218241 */
219242 disableWidgetAssets ( widgetType , widgetId ) {
220- let url = SdkConfig . get ( ) . integrations_rest_url + '/widgets/set_assets_state' ;
243+ let url = this . apiUrl + '/widgets/set_assets_state' ;
221244 url = this . getStarterLink ( url ) ;
222245 return new Promise ( ( resolve , reject ) => {
223246 request ( {
@@ -246,7 +269,7 @@ class ScalarAuthClient {
246269 getScalarInterfaceUrlForRoom ( room , screen , id ) {
247270 const roomId = room . roomId ;
248271 const roomName = room . name ;
249- let url = SdkConfig . get ( ) . integrations_ui_url ;
272+ let url = this . uiUrl ;
250273 url += "?scalar_token=" + encodeURIComponent ( this . scalarToken ) ;
251274 url += "&room_id=" + encodeURIComponent ( roomId ) ;
252275 url += "&room_name=" + encodeURIComponent ( roomName ) ;
@@ -264,5 +287,3 @@ class ScalarAuthClient {
264287 return starterLinkUrl + "?scalar_token=" + encodeURIComponent ( this . scalarToken ) ;
265288 }
266289}
267-
268- module . exports = ScalarAuthClient ;
0 commit comments