@@ -33,6 +33,33 @@ export default class ProxyFrameOAuth extends OAuthBase {
3333 this . _defaultProxyRetry = defaultProxyRetry ;
3434
3535 this . _reducer = getProxyFrameOAuthReducer ( this . actionTypes ) ;
36+
37+ this . _loggedIn = false ;
38+ }
39+
40+ _onStateChange ( ) {
41+ super . _onStateChange ( ) ;
42+ if ( this . _auth . loggedIn === this . _loggedIn ) {
43+ return ;
44+ }
45+ this . _loggedIn = this . _auth . loggedIn ;
46+ if ( this . _loggedIn && this . _auth . isImplicit ) {
47+ console . log ( 'new login, start refresh token timeout' ) ;
48+ this . _createImplicitRefreshTimeout ( ) ;
49+ }
50+ if ( ! this . _loggedIn && this . _auth . isImplicit ) {
51+ this . _clearImplicitRefreshIframe ( ) ;
52+ if ( this . _implicitRefreshTimeoutId ) {
53+ clearTimeout ( this . _implicitRefreshTimeoutId ) ;
54+ }
55+ }
56+ }
57+
58+ async _handleCallbackUri ( options ) {
59+ await super . _handleCallbackUri ( options ) ;
60+ if ( this . _auth . isImplicit && this . _auth . loggedIn ) {
61+ this . _createImplicitRefreshTimeout ( ) ;
62+ }
3663 }
3764
3865 get name ( ) {
@@ -137,4 +164,62 @@ export default class ProxyFrameOAuth extends OAuthBase {
137164 } , '*' ) ;
138165 }
139166 }
167+
168+ _createImplicitRefreshIframe ( ) {
169+ this . _clearImplicitRefreshIframe ( ) ;
170+ this . _implicitRefreshFrame = document . createElement ( 'iframe' ) ;
171+ this . _implicitRefreshFrame . src = this . implictRefreshOAuthUri ;
172+ this . _implicitRefreshFrame . style . display = 'none' ;
173+ document . body . appendChild ( this . _implicitRefreshFrame ) ;
174+ // eslint-disable-next-line
175+ this . _implictitRefreshCallBack = ( { origin, data } ) => {
176+ const { refreshCallbackUri } = data ;
177+ if ( refreshCallbackUri && this . _auth . loggedIn ) {
178+ this . _handleCallbackUri ( refreshCallbackUri , true ) ;
179+ this . _clearImplicitRefreshIframe ( ) ;
180+ }
181+ } ;
182+ window . addEventListener ( 'message' , this . _implictitRefreshCallBack ) ;
183+ }
184+
185+ _clearImplicitRefreshIframe ( ) {
186+ if ( this . _implicitRefreshFrame ) {
187+ document . body . removeChild ( this . _implicitRefreshFrame ) ;
188+ this . _implicitRefreshFrame = null ;
189+ window . removeEventListener ( 'message' , this . _implictitRefreshCallBack ) ;
190+ this . _callbackHandler = null ;
191+ }
192+ }
193+
194+ // create a time out to refresh implicit flow token
195+ _createImplicitRefreshTimeout ( ) {
196+ if ( this . _implicitRefreshTimeoutId ) {
197+ clearTimeout ( this . _implicitRefreshTimeoutId ) ;
198+ }
199+ const authData = this . _auth . token ;
200+ const refreshTokenExpiresIn = authData . expiresIn ;
201+ const { expireTime } = authData ;
202+ if ( ! refreshTokenExpiresIn || ! expireTime ) {
203+ return ;
204+ }
205+ // set refresh time to (token exposre time) / 3
206+ let refreshTokenTimeoutTime = ( parseInt ( refreshTokenExpiresIn , 10 ) * 1000 ) / 3 ;
207+ if ( refreshTokenTimeoutTime + Date . now ( ) > expireTime ) {
208+ refreshTokenTimeoutTime = expireTime - Date . now ( ) - 5000 ;
209+ if ( refreshTokenTimeoutTime < 0 ) {
210+ return ;
211+ }
212+ }
213+ this . _implicitRefreshTimeoutId = setTimeout ( ( ) => {
214+ if ( ! this . _auth . loggedIn ) {
215+ return ;
216+ }
217+ if ( this . _tabManager && ! this . _tabManager . active ) {
218+ this . _createImplicitRefreshTimeout ( ) ;
219+ return ;
220+ }
221+ this . _createImplicitRefreshIframe ( ) ;
222+ this . _implicitRefreshTimeoutId = null ;
223+ } , refreshTokenTimeoutTime ) ;
224+ }
140225}
0 commit comments