Skip to content

Commit f45fa46

Browse files
CopilotVeena11
andcommitted
Switch to broker context after profile installation completes
- After msauth://profileInstalled callback, dismiss both webviews and switch to broker - Added dismissSuspendedEmbeddedWebview method to coordinator - Dismiss suspended embedded webview after profile installation - Create MSIDBrokerInteractiveController after profile installation - Broker invokes SSO extension to handle authentication in its own webview - Response flows back to calling app through broker completion handler - Updated cleanup to properly dismiss suspended webview - Platform check for iOS (broker only available on iOS) - Proper error handling for broker controller creation failures Co-authored-by: Veena11 <9446116+Veena11@users.noreply.github.com>
1 parent c159979 commit f45fa46

File tree

3 files changed

+65
-10
lines changed

3 files changed

+65
-10
lines changed

IdentityCore/src/controllers/MSIDLocalInteractiveController.m

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -341,24 +341,51 @@ - (void)handleProfileInstallationCompletion:(NSURL *)callbackURL
341341
MSID_LOG_WITH_CTX(MSIDLogLevelInfo, self.requestParameters,
342342
@"Profile installation completed successfully (msauth://profileInstalled)");
343343

344-
// ASWebAuthenticationSession has already completed successfully
345-
// Its completion handler has fired, and the session has cleaned itself up
346-
// We should NOT call dismiss (which would try to cancel it) - just release our reference
344+
// ASWebAuthenticationSession has already completed and cleaned itself up
345+
// Just release our reference (don't call dismiss on completed session)
347346
self.transitionCoordinator.externalSessionHandler = nil;
348347

349-
// Resume the suspended embedded webview
350-
[self.transitionCoordinator resumeSuspendedEmbeddedWebview];
348+
// Dismiss the suspended embedded webview (we're switching to broker context)
349+
[self.transitionCoordinator dismissSuspendedEmbeddedWebview];
351350

352-
// The webview will continue its flow naturally
353-
// It's still alive and will process the next response from the server
354-
// We don't call completionBlock here - the webview will complete when auth finishes
351+
// After profile installation, switch to broker context
352+
// The SSO extension will handle the authentication request in its own webview
353+
MSID_LOG_WITH_CTX(MSIDLogLevelInfo, self.requestParameters,
354+
@"Switching to broker context after profile installation");
355+
356+
#if TARGET_OS_IPHONE
357+
NSError *brokerError = nil;
358+
MSIDBrokerInteractiveController *brokerController = [[MSIDBrokerInteractiveController alloc] initWithInteractiveRequestParameters:self.interactiveRequestParamaters
359+
tokenRequestProvider:self.tokenRequestProvider
360+
brokerInstallLink:nil
361+
error:&brokerError];
362+
363+
if (!brokerController)
364+
{
365+
MSID_LOG_WITH_CTX(MSIDLogLevelError, self.requestParameters,
366+
@"Failed to create broker controller after profile installation: %@", brokerError);
367+
CONDITIONAL_STOP_TELEMETRY_EVENT([self telemetryAPIEvent], brokerError);
368+
completionBlock(nil, brokerError);
369+
return;
370+
}
371+
372+
// Broker will invoke SSO extension which handles the request in its own webview
373+
// Response will be sent back to calling app through the broker completion handler
374+
[brokerController acquireToken:completionBlock];
375+
#else
376+
NSError *platformError = MSIDCreateError(MSIDErrorDomain, MSIDErrorInternal,
377+
@"Broker authentication not supported on this platform",
378+
nil, nil, nil, self.requestParameters.correlationId, nil, YES);
379+
CONDITIONAL_STOP_TELEMETRY_EVENT([self telemetryAPIEvent], platformError);
380+
completionBlock(nil, platformError);
381+
#endif
355382
}
356383
else
357384
{
358385
MSID_LOG_WITH_CTX(MSIDLogLevelWarning, self.requestParameters,
359386
@"Unexpected callback URL from profile installation: %@", callbackURL);
360387

361-
// Clean up - this will dismiss the session if still active
388+
// Clean up - this will dismiss both webviews
362389
[self.transitionCoordinator cleanup];
363390

364391
// Create error for unexpected callback

IdentityCore/src/webview/MSIDWebviewTransitionCoordinator.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ NS_ASSUME_NONNULL_BEGIN
7676
*/
7777
- (void)resumeSuspendedEmbeddedWebview;
7878

79+
/**
80+
* Dismisses the suspended embedded webview (cancels and releases it)
81+
* Use this when you need to completely abandon the embedded webview and switch to a different flow
82+
*/
83+
- (void)dismissSuspendedEmbeddedWebview;
84+
7985
/**
8086
* Dismisses the ASWebAuthenticationSession if active
8187
* NOTE: Only call this if the session needs to be canceled (error, timeout, user cancellation).

IdentityCore/src/webview/MSIDWebviewTransitionCoordinator.m

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,23 @@ - (void)resumeSuspendedEmbeddedWebview
155155
// We don't need to manually trigger anything as it's been kept alive
156156
}
157157

158+
- (void)dismissSuspendedEmbeddedWebview
159+
{
160+
if (!self.suspendedEmbeddedWebview)
161+
{
162+
MSID_LOG_WITH_CTX(MSIDLogLevelWarning, nil, @"[MSIDWebviewTransitionCoordinator] No suspended webview to dismiss");
163+
return;
164+
}
165+
166+
MSID_LOG_WITH_CTX(MSIDLogLevelInfo, nil, @"[MSIDWebviewTransitionCoordinator] Dismissing suspended embedded webview");
167+
168+
// Cancel the suspended webview to properly clean it up
169+
[self.suspendedEmbeddedWebview cancelProgrammatically];
170+
171+
// Release the reference
172+
self.suspendedEmbeddedWebview = nil;
173+
}
174+
158175
- (void)dismissExternalSession
159176
{
160177
if (self.externalSessionHandler)
@@ -173,7 +190,12 @@ - (void)cleanup
173190
{
174191
MSID_LOG_WITH_CTX(MSIDLogLevelInfo, nil, @"[MSIDWebviewTransitionCoordinator] Cleaning up coordinator state");
175192

176-
self.suspendedEmbeddedWebview = nil;
193+
// Dismiss suspended webview if exists
194+
if (self.suspendedEmbeddedWebview)
195+
{
196+
MSID_LOG_WITH_CTX(MSIDLogLevelVerbose, nil, @"[MSIDWebviewTransitionCoordinator] Dismissing suspended webview during cleanup");
197+
[self dismissSuspendedEmbeddedWebview];
198+
}
177199

178200
// Dismiss external session if still active (e.g., on error or cancellation)
179201
// If the session completed successfully, it should already be nil

0 commit comments

Comments
 (0)