Skip to content

Commit cf35b70

Browse files
author
Swasti Gupta
committed
Resolved comments and updated code snippets
1 parent d4c22c7 commit cf35b70

File tree

2 files changed

+84
-69
lines changed

2 files changed

+84
-69
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Microsoft Authentication Library for iOS and macOS
77
| [MSAL iOS and macOS documentation](https://learn.microsoft.com/en-us/entra/msal/objc/) | &#8226; [Microsoft Entra ID (workforce samples)](https://learn.microsoft.com/en-us/entra/identity-platform/sample-v2-code?tabs=framework#ios)<br/>&#8226; [Microsoft Entra External ID (customer samples)](https://learn.microsoft.com/en-us/entra/external-id/customers/samples-ciam-all?tabs=apptype#mobile) | [ SDK reference](https://azuread.github.io/microsoft-authentication-library-for-objc/) | [Get support](README.md#community-help-and-support) | [Feedback](https://forms.office.com/r/xuBV0CzEih) |
88

99
> ⚠️ **MSAL 2.x Released with Breaking Changes**
10-
> Please review the [MSAL 2.x for iOS/macOS – Major Release Announcement & Migration Guide](docs/MSAL_2x_Migration_Guide.md) for full details on what changed and how to migrate.
10+
> Review the [MSAL 2.x for iOS/macOS – Major Release Announcement & Migration Guide](docs/MSAL_2x_Migration_Guide.md) for a complete overview of the changes and step-by-step migration instructions.
1111
1212
The Microsoft Authentication Library (MSAL) for iOS and macOS is an auth SDK that can be used to seamlessly integrate authentication into your apps using industry standard OAuth2 and OpenID Connect. It allows you to sign in users or apps with Microsoft identities. These identities include Microsoft Entra ID work and school accounts, personal Microsoft accounts, social accounts, and customer accounts.
1313

docs/MSAL_2x_Migration_Guide.md

Lines changed: 83 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ This guide will help you:
1414

1515
#### What Changed
1616

17-
In MSAL 2.x, all enterprise (AAD) applications must specify a valid redirect URI in the format: `msauth.[BUNDLE_ID]://auth`.
17+
In **MSAL 2.x**, all enterprise **(AAD)** applications must specify a valid redirect URI in the format: `msauth.[BUNDLE_ID]://auth`.
1818

19-
For applications migrating from ADAL, redirect URIs formatted as `<scheme>://[BUNDLE_ID]` remain valid and are still supported in MSAL 2.x.
19+
For applications migrating from **ADAL**, redirect URIs formatted as `<scheme>://[BUNDLE_ID]` remain valid and are still supported in **MSAL 2.x**.
2020

2121
📖 For more information, see: [MSAL Redirect URI Format Requirements](https://learn.microsoft.com/en-us/entra/msal/objc/redirect-uris-ios#msal-redirect-uri-format-requirements)
2222

2323
#### Why It Matters
2424

25-
This standardization enables secure and valid redirection to brokered authentication with Microsoft Authenticator or Company Portal.
25+
This standardization enables secure and valid redirection to brokered authentication with **Microsoft Authenticator** or **Company Portal**.
2626

2727
#### How to Migrate
2828

@@ -32,7 +32,7 @@ In the Azure Portal under-App Registrations > Authentication, configure a redire
3232

3333
Note: If migrating from ADAL, the `<scheme>://[BUNDLE_ID]` format is still supported.
3434

35-
⚠️ Important: Ensure this redirect URI is configured across all app targets and extensions (such as Share Extensions) to enable smooth brokered authentication.
35+
⚠️ Important: Ensure this redirect URI is configured across all **app targets and extensions** (such as Share Extensions) to enable smooth brokered authentication.
3636

3737
##### 2. Update Info.plist
3838

@@ -66,38 +66,33 @@ Include the following in Info.plist under LSApplicationQueriesSchemes:
6666

6767
Objective-C:
6868
```objc
69-
MSALPublicClientApplicationConfig *config =
70-
[[MSALPublicClientApplicationConfig alloc] initWithClientId:@"your-client-id"
71-
redirectUri:@"msauth.your.bundle.id://auth"
72-
authority:authority];
69+
MSALPublicClientApplicationConfig *config = [[MSALPublicClientApplicationConfig alloc] initWithClientId:@"your-client-id"
70+
redirectUri:@"msauth.your.bundle.id://auth"
71+
authority:authority];
7372

74-
NSError *error = nil;
75-
MSALPublicClientApplication *application =
76-
[[MSALPublicClientApplication alloc] initWithConfiguration:config error:&error];
73+
NSError *error = nil;
74+
MSALPublicClientApplication *application = [[MSALPublicClientApplication alloc] initWithConfiguration:config
75+
error:&error];
7776

78-
if (error)
79-
{
80-
NSLog(@"Error initializing MSAL: %@", error.localizedDescription);
81-
return;
77+
if (error)
78+
{
79+
NSLog(@"Error initializing MSAL: %@", error.localizedDescription);
80+
return;
8281
}
8382
```
8483
8584
Swift:
8685
```swift
87-
MSALPublicClientApplicationConfig *config =
88-
[[MSALPublicClientApplicationConfig alloc] initWithClientId:@"your-client-id"
89-
redirectUri:@"msauth.your.bundle.id://auth"
90-
authority:authority];
91-
92-
NSError *error = nil;
93-
MSALPublicClientApplication *application =
94-
[[MSALPublicClientApplication alloc] initWithConfiguration:config error:&error];
86+
let config = MSALPublicClientApplicationConfig(clientId: "your-client-id",
87+
redirectUri: "msauth.your.bundle.id://auth",
88+
authority: authority)
9589
96-
if (error)
97-
{
98-
NSLog(@"Error initializing MSAL: %@", error.localizedDescription);
99-
return;
100-
}
90+
do {
91+
let application = try MSALPublicClientApplication(configuration: config)
92+
// Proceed with application
93+
} catch let error as NSError {
94+
print("Error initializing MSAL: \(error.localizedDescription)")
95+
}
10196
```
10297
Note: Remember to replace any placeholder values with your actual app-specific values
10398

@@ -133,21 +128,20 @@ If an invalid redirect URI is provided for enterprise (AAD) scenarios, MSAL will
133128
Starting with **MSAL 2.x** for **iOS** and **macOS**, providing a valid parent view controller is **mandatory** for any interactive authentication flow.
134129
In **MSAL 1.x**, it was optional for macOS.
135130

136-
A valid parent view controller must be **non-nil** and its view must be attached to a window (i.e., `parentViewController.view.window != nil`).
131+
A valid parent view controller must be **non-nil** and its view must be attached to a valid **window** (i.e., `parentViewController.view.window != nil`).
137132

138133
#### Why It Matters
139134

140135
This ensures that the authentication UI can be correctly presented over the app's visible window and prevents runtime presentation issues.
141136

142137
#### How to Migrate
143138

144-
##### 1. Initialize MSALPublicClientApplication using the configured redirect URI
139+
##### 1. Create MSALWebviewParameters with a valid parent view controller with its view attached to a valid window
145140

146141
Objective-C:
147142
```objc
148143
MSALViewController *viewController = ...;
149-
MSALWebviewParameters *webParameters = [[MSALWebviewParameters alloc]
150-
initWithAuthPresentationViewController:viewController];
144+
MSALWebviewParameters *webParameters = [[MSALWebviewParameters alloc] initWithAuthPresentationViewController:viewController];
151145
```
152146
153147
Swift:
@@ -160,9 +154,8 @@ let webviewParameters = MSALWebviewParameters(authPresentationViewController: vi
160154

161155
Objective-C:
162156
```objc
163-
MSALInteractiveTokenParameters *parameters =
164-
[[MSALInteractiveTokenParameters alloc] initWithScopes:scopes
165-
webviewParameters:webParameters];
157+
MSALInteractiveTokenParameters *parameters = [[MSALInteractiveTokenParameters alloc] initWithScopes:scopes
158+
webviewParameters:webParameters];
166159
```
167160
168161
Swift:
@@ -206,7 +199,7 @@ Starting with **MSAL 2.x**, all properties previously declared in the `MSALAccou
206199

207200
#### Why It Matters
208201

209-
This change **consolidates all account-related properties** into a single protocol, making it easier to mock and abstract in tests or when building protocol-based systems. It removes the need to rely on category headers or internal implementation details.
202+
This **consolidates all account-related properties** into a single protocol, enabling mocking and protocol-based abstraction without exposing internal implementation.
210203

211204
#### How to Migrate
212205

@@ -243,7 +236,7 @@ if account.isSSOAccount {
243236

244237
#### What Changed
245238

246-
All deprecated APIs from MSAL 1.x are removed in 2.x. This includes deprecated initializers, account management methods, token acquisition methods, logging and telemetry interfaces.
239+
All deprecated APIs from **MSAL 1.x** are removed in **MSAL 2.x**. This includes deprecated initializers, account management methods, token acquisition methods, logging and telemetry interfaces.
247240

248241
#### Why It Matters
249242

@@ -263,28 +256,34 @@ This removes reliance on outdated methods, streamlines code maintenance, and ens
263256

264257
Objective-C – Before (Deprecated):
265258
```objc
266-
MSALPublicClientApplication *application =
267-
[[MSALPublicClientApplication alloc] initWithClientId:@"your-client-id"
268-
authority:authority
269-
error:nil];
259+
MSALPublicClientApplication *application = [[MSALPublicClientApplication alloc] initWithClientId:@"your-client-id"
260+
authority:authority
261+
error:nil];
270262
```
271263
272264
Objective-C – After:
273265
```objc
274-
MSALPublicClientApplicationConfig *config =
275-
[[MSALPublicClientApplicationConfig alloc] initWithClientId:@"your-client-id"];
276-
config.redirectUri = @"your-redirect-uri";
277-
config.knownAuthorities = @[authority];
278-
MSALPublicClientApplication *application =
279-
[[MSALPublicClientApplication alloc] initWithConfiguration:config error:nil];
266+
MSALPublicClientApplicationConfig *config = [[MSALPublicClientApplicationConfig alloc] initWithClientId:@"your-client-id"
267+
redirectUri:@"your-redirect-uri"
268+
authority:authority];
269+
MSALPublicClientApplication *application = [[MSALPublicClientApplication alloc] initWithConfiguration:config
270+
error:nil];
280271
```
281272

282273
Swift – After:
283274
```swift
284-
let config = MSALPublicClientApplicationConfig(clientId: "your-client-id")
285-
config.redirectUri = "your-redirect-uri"
286-
config.knownAuthorities = [authority]
287-
let application = try! MSALPublicClientApplication(configuration: config)
275+
let config = MSALPublicClientApplicationConfig(
276+
clientId: "your-client-id",
277+
redirectUri: "your-redirect-uri",
278+
authority: authority
279+
)
280+
281+
do {
282+
let application = try MSALPublicClientApplication(configuration: config)
283+
// Use `application`
284+
} catch {
285+
print("Failed to initialize MSAL: \(error.localizedDescription)")
286+
}
288287
```
289288

290289
##### 2. Token Acquisition (Silent)
@@ -301,17 +300,20 @@ Objective-C – Before (Deprecated):
301300
[application acquireTokenSilentForScopes:@[@"user.read"]
302301
account:account
303302
authority:authority
304-
completionBlock:^(MSALResult *result, NSError *error) {
303+
completionBlock:^(MSALResult *result, NSError *error)
304+
{
305305
// Handle result
306306
}];
307307
```
308308
309309
Objective-C – After:
310310
```objc
311-
MSALSilentTokenParameters *params =
312-
[[MSALSilentTokenParameters alloc] initWithScopes:@[@"user.read"] account:account];
311+
MSALSilentTokenParameters *params = [[MSALSilentTokenParameters alloc] initWithScopes:@[@"user.read"]
312+
account:account];
313313
params.authority = authority;
314-
[application acquireTokenSilentWithParameters:params completionBlock:^(MSALResult *result, NSError *error) {
314+
[application acquireTokenSilentWithParameters:params
315+
completionBlock:^(MSALResult *result, NSError *error)
316+
{
315317
// Handle result
316318
}];
317319
```
@@ -337,18 +339,20 @@ application.acquireTokenSilent(with: parameters) { (result, error) in
337339
Objective-C – Before (Deprecated):
338340
```objc
339341
[application acquireTokenForScopes:@[@"user.read"]
340-
completionBlock:^(MSALResult *result, NSError *error) {
342+
completionBlock:^(MSALResult *result, NSError *error)
343+
{
341344
// Handle result
342345
}];
343346
```
344347
345348
Objective-C – After:
346349
```objc
347-
MSALInteractiveTokenParameters *parameters =
348-
[[MSALInteractiveTokenParameters alloc] initWithScopes:@[@"user.read"]
349-
webviewParameters:webviewParams];
350+
MSALInteractiveTokenParameters *parameters = [[MSALInteractiveTokenParameters alloc] initWithScopes:@[@"user.read"]
351+
webviewParameters:webviewParams];
350352
parameters.promptType = MSALPromptTypeSelectAccount;
351-
[application acquireTokenWithParameters:parameters completionBlock:^(MSALResult *result, NSError *error) {
353+
[application acquireTokenWithParameters:parameters
354+
completionBlock:^(MSALResult *result, NSError *error)
355+
{
352356
// Handle result
353357
}];
354358
```
@@ -375,24 +379,29 @@ application.acquireToken(with: parameters) { (result, error) in
375379
Objective-C – Before (Deprecated):
376380
```objc
377381
NSError *error = nil;
378-
MSALAccount *account = [application accountForHomeAccountId:@"homeAccountId" error:&error];
382+
MSALAccount *account = [application accountForHomeAccountId:@"homeAccountId"
383+
error:&error];
379384

380385
// Deprecated method to fetch accounts filtered by authority
381-
[application allAccountsFilteredByAuthority:^(NSArray<MSALAccount *> *accounts, NSError *error) {
386+
[application allAccountsFilteredByAuthority:^(NSArray<MSALAccount *> *accounts, NSError *error)
387+
{
382388
// Handle accounts
383389
}];
384390
```
385391
386392
Objective-C – After:
387393
```objc
388394
NSError *error = nil;
389-
MSALAccount *account = [application accountForIdentifier:@"accountId" error:&error];
395+
MSALAccount *account = [application accountForIdentifier:@"accountId"
396+
error:&error];
390397
391398
// Recommended modern asynchronous way to fetch accounts
392-
[application accountsFromDeviceWithCompletionBlock:^(NSArray<MSALAccount *> *accounts, NSError *error) {
399+
[application accountsFromDeviceWithCompletionBlock:^(NSArray<MSALAccount *> *accounts, NSError *error)
400+
{
393401
// Handle accounts
394402
}];
395403
```
404+
396405
Swift – Before:
397406
```swift
398407
do {
@@ -440,15 +449,17 @@ Migration Examples – MSALLogger
440449
Objective-C – Before (Deprecated):
441450
```objc
442451
[MSALLogger sharedLogger].level = MSALLogLevelVerbose;
443-
[[MSALLogger sharedLogger] setCallback:^(MSALLogLevel level, NSString *message, BOOL containsPII) {
452+
[[MSALLogger sharedLogger] setCallback:^(MSALLogLevel level, NSString *message, BOOL containsPII)
453+
{
444454
NSLog(@"%@", message);
445455
}];
446456
```
447457
448458
Objective-C – After:
449459
```objc
450460
MSALGlobalConfig.loggerConfig.logLevel = MSALLogLevelVerbose;
451-
[MSALGlobalConfig.loggerConfig setLogCallback:^(MSALLogLevel level, NSString *message, BOOL containsPII) {
461+
[MSALGlobalConfig.loggerConfig setLogCallback:^(MSALLogLevel level, NSString *message, BOOL containsPII)
462+
{
452463
NSLog(@"%@", message);
453464
}];
454465
```
@@ -496,7 +507,8 @@ Objective-C – Before (Deprecated):
496507
```objc
497508
[MSALTelemetry sharedInstance].piiEnabled = YES;
498509
[MSALTelemetry sharedInstance].notifyOnFailureOnly = NO;
499-
[[MSALTelemetry sharedInstance] setTelemetryCallback:^(MSALTelemetryEvent *event) {
510+
[[MSALTelemetry sharedInstance] setTelemetryCallback:^(MSALTelemetryEvent *event)
511+
{
500512
NSLog(@"%@", event.name);
501513
}];
502514
```
@@ -505,7 +517,8 @@ Objective-C – After:
505517
```objc
506518
MSALGlobalConfig.telemetryConfig.piiEnabled = YES;
507519
MSALGlobalConfig.telemetryConfig.notifyOnFailureOnly = NO;
508-
MSALGlobalConfig.telemetryConfig.telemetryCallback = ^(MSALTelemetryEvent *event) {
520+
MSALGlobalConfig.telemetryConfig.telemetryCallback = ^(MSALTelemetryEvent *event)
521+
{
509522
NSLog(@"%@", event.name);
510523
};
511524
```
@@ -544,6 +557,8 @@ Before upgrading to MSAL 2.x, make sure to:
544557
- ✅ Validate redirect URIs in both the Azure portal and Info.plist
545558
- ✅ Update all deprecated API calls to supported alternatives
546559
- ✅ Review and migrate telemetry and logging configurations
560+
- ✅ Add unit tests or integration tests to validate authentication flows after migration
561+
- ✅ Thoroughly test in staging before deploying changes to production.
547562

548563
### Resources
549564

0 commit comments

Comments
 (0)