@@ -143,17 +143,15 @@ export class TeamsProvider extends MsalProvider {
143143
144144 teams . initialize ( ) ;
145145
146- // msal checks for the window.opener.msal to check if this is a popup authentication
147- // and gets a false positive since teams opens a popup for the authentication.
148- // in reality, we are doing a redirect authentication and need to act as if this is the
149- // window initiating the authentication
150- if ( window . opener ) {
151- window . opener . msal = null ;
146+ // if we were signing out before, then we are done
147+ if ( sessionStorage . getItem ( this . _sessionStorageLogoutInProgress ) ) {
148+ teams . authentication . notifySuccess ( ) ;
152149 }
153150
154151 const url = new URL ( window . location . href ) ;
152+ const isSignOut = url . searchParams . get ( 'signout' ) ;
155153
156- const paramsString = sessionStorage . getItem ( this . _sessionStorageParametersKey ) ;
154+ const paramsString = localStorage . getItem ( this . _localStorageParametersKey ) ;
157155 let authParams : AuthParams ;
158156
159157 if ( paramsString ) {
@@ -162,14 +160,6 @@ export class TeamsProvider extends MsalProvider {
162160 authParams = { } ;
163161 }
164162
165- if ( ! authParams . clientId ) {
166- authParams . clientId = url . searchParams . get ( 'clientId' ) ;
167- authParams . scopes = url . searchParams . get ( 'scopes' ) ;
168- authParams . loginHint = url . searchParams . get ( 'loginHint' ) ;
169-
170- sessionStorage . setItem ( this . _sessionStorageParametersKey , JSON . stringify ( authParams ) ) ;
171- }
172-
173163 if ( ! authParams . clientId ) {
174164 teams . authentication . notifyFailure ( 'no clientId provided' ) ;
175165 return ;
@@ -181,8 +171,7 @@ export class TeamsProvider extends MsalProvider {
181171 clientId : authParams . clientId ,
182172 options : {
183173 auth : {
184- clientId : authParams . clientId ,
185- redirectUri : url . protocol + '//' + url . host + url . pathname
174+ clientId : authParams . clientId
186175 } ,
187176 system : {
188177 loadFrameTimeout : 10000
@@ -200,17 +189,30 @@ export class TeamsProvider extends MsalProvider {
200189 // how do we handle when user can't sign in
201190 // change to promise and return status
202191 if ( provider . state === ProviderState . SignedOut ) {
203- provider . login ( {
204- loginHint : authParams . loginHint ,
205- scopes : scopes || provider . scopes
206- } ) ;
192+ if ( isSignOut ) {
193+ teams . authentication . notifySuccess ( ) ;
194+ return ;
195+ }
196+
197+ // make sure we are calling login only once
198+ if ( ! sessionStorage . getItem ( this . _sessionStorageLoginInProgress ) ) {
199+ sessionStorage . setItem ( this . _sessionStorageLoginInProgress , 'true' ) ;
200+ provider . login ( {
201+ loginHint : authParams . loginHint ,
202+ scopes : scopes || provider . scopes
203+ } ) ;
204+ }
207205 } else if ( provider . state === ProviderState . SignedIn ) {
206+ if ( isSignOut ) {
207+ sessionStorage . setItem ( this . _sessionStorageLogoutInProgress , 'true' ) ;
208+ await provider . logout ( ) ;
209+ return ;
210+ }
211+
208212 try {
209213 const accessToken = await provider . getAccessTokenForScopes ( ...provider . scopes ) ;
210- sessionStorage . removeItem ( this . _sessionStorageParametersKey ) ;
211214 teams . authentication . notifySuccess ( accessToken ) ;
212215 } catch ( e ) {
213- sessionStorage . removeItem ( this . _sessionStorageParametersKey ) ;
214216 teams . authentication . notifyFailure ( e ) ;
215217 }
216218 }
@@ -220,15 +222,9 @@ export class TeamsProvider extends MsalProvider {
220222 handleProviderState ( ) ;
221223 }
222224
223- private static _sessionStorageParametersKey = 'msg-teamsprovider-auth-parameters' ;
224-
225- /**
226- * Scopes used for authentication
227- *
228- * @type {string[] }
229- * @memberof TeamsProvider
230- */
231- public scopes : string [ ] ;
225+ private static _localStorageParametersKey = 'msg-teamsprovider-auth-parameters' ;
226+ private static _sessionStorageLoginInProgress = 'msg-teamsprovider-login-in-progress' ;
227+ private static _sessionStorageLogoutInProgress = 'msg-teamsprovider-logout-in-progress' ;
232228
233229 private teamsContext ;
234230 private _authPopupUrl : string ;
@@ -261,24 +257,54 @@ export class TeamsProvider extends MsalProvider {
261257 teams . getContext ( context => {
262258 this . teamsContext = context ;
263259
264- const url = new URL ( this . _authPopupUrl , new URL ( window . location . href ) ) ;
265- url . searchParams . append ( 'clientId' , this . clientId ) ;
260+ const authParams : AuthParams = {
261+ clientId : this . clientId ,
262+ loginHint : context . loginHint ,
263+ scopes : this . scopes . join ( ',' )
264+ } ;
266265
267- if ( context . loginHint ) {
268- url . searchParams . append ( 'loginHint' , context . loginHint ) ;
269- }
266+ localStorage . setItem ( TeamsProvider . _localStorageParametersKey , JSON . stringify ( authParams ) ) ;
270267
271- if ( this . scopes ) {
272- url . searchParams . append ( 'scopes' , this . scopes . join ( ',' ) ) ;
273- }
268+ const url = new URL ( this . _authPopupUrl , new URL ( window . location . href ) ) ;
274269
275270 teams . authentication . authenticate ( {
276271 failureCallback : reason => {
277272 this . setState ( ProviderState . SignedOut ) ;
278273 reject ( ) ;
279274 } ,
280275 successCallback : result => {
281- this . setState ( ProviderState . SignedIn ) ;
276+ this . trySilentSignIn ( ) ;
277+ resolve ( ) ;
278+ } ,
279+ url : url . href
280+ } ) ;
281+ } ) ;
282+ } ) ;
283+ }
284+
285+ /**
286+ * sign out user
287+ *
288+ * @returns {Promise<void> }
289+ * @memberof MsalProvider
290+ */
291+ public async logout ( ) : Promise < void > {
292+ const teams = TeamsHelper . microsoftTeamsLib ;
293+
294+ return new Promise ( ( resolve , reject ) => {
295+ teams . getContext ( context => {
296+ this . teamsContext = context ;
297+
298+ const url = new URL ( this . _authPopupUrl , new URL ( window . location . href ) ) ;
299+ url . searchParams . append ( 'signout' , 'true' ) ;
300+
301+ teams . authentication . authenticate ( {
302+ failureCallback : reason => {
303+ this . trySilentSignIn ( ) ;
304+ reject ( ) ;
305+ } ,
306+ successCallback : result => {
307+ this . trySilentSignIn ( ) ;
282308 resolve ( ) ;
283309 } ,
284310 url : url . href
@@ -295,8 +321,9 @@ export class TeamsProvider extends MsalProvider {
295321 * @memberof TeamsProvider
296322 */
297323 public async getAccessToken ( options : AuthenticationProviderOptions ) : Promise < string > {
298- if ( ! this . teamsContext ) {
324+ if ( ! this . teamsContext && TeamsHelper . microsoftTeamsLib ) {
299325 const teams = TeamsHelper . microsoftTeamsLib ;
326+ teams . initialize ( ) ;
300327 this . teamsContext = await teams . getContext ( ) ;
301328 }
302329
@@ -309,17 +336,10 @@ export class TeamsProvider extends MsalProvider {
309336 accessTokenRequest . loginHint = this . teamsContext . loginHint ;
310337 }
311338
312- const currentParent = window . parent ;
313- if ( document . referrer . startsWith ( 'https://teams.microsoft.com/' ) ) {
314- ( window as any ) . parent = window ;
315- }
316-
317339 try {
318340 const response = await this . _userAgentApplication . acquireTokenSilent ( accessTokenRequest ) ;
319- ( window as any ) . parent = currentParent ;
320341 return response . accessToken ;
321342 } catch ( e ) {
322- ( window as any ) . parent = currentParent ;
323343 if ( this . requiresInteraction ( e ) ) {
324344 // nothing we can do now until we can do incremental consent
325345 return null ;
0 commit comments