Bug 1944119 - Refactor MicrosoftEntraSSOUtils.mm a=dmeehan

Currently, the delegate uses nested if statements. The patch simplifies this by replacing them with return/continues.

Original Revision: https://phabricator.services.mozilla.com/D235688

Differential Revision: https://phabricator.services.mozilla.com/D237076
This commit is contained in:
Sean 2025-02-13 00:23:03 +00:00
parent 941b037ac9
commit 3c8447886d

View file

@ -65,25 +65,38 @@ class API_AVAILABLE(macos(13.3)) MicrosoftEntraSSOUtils final {
} }
- (void)authorizationController:(ASAuthorizationController*)controller - (void)authorizationController:(ASAuthorizationController*)controller
didCompleteWithAuthorization:(ASAuthorization*)authorization { didCompleteWithAuthorization:(ASAuthorization*)authorization {
if ([authorization.credential ASAuthorizationSingleSignOnCredential* ssoCredential =
isKindOfClass:[ASAuthorizationSingleSignOnCredential class]]) { [authorization.credential
isKindOfClass:[ASAuthorizationSingleSignOnCredential class]]
? (ASAuthorizationSingleSignOnCredential*)authorization.credential
: nil;
if (!ssoCredential) {
MOZ_LOG(gMacOSWebAuthnServiceLog, mozilla::LogLevel::Debug, MOZ_LOG(gMacOSWebAuthnServiceLog, mozilla::LogLevel::Debug,
("SSORequestDelegate::didCompleteWithAuthorization: " ("SSORequestDelegate::didCompleteWithAuthorization: "
"got ASAuthorizationSingleSignOnCredential")); "should have ASAuthorizationSingleSignOnCredential"));
mozilla::glean::network_sso::entra_success.Get("no_credential"_ns).Add(1);
ASAuthorizationSingleSignOnCredential* ssoCredential = [self invokeCallbackOnMainThread];
(ASAuthorizationSingleSignOnCredential*)authorization.credential; return;
}
NSHTTPURLResponse* authenticatedResponse = NSHTTPURLResponse* authenticatedResponse =
ssoCredential.authenticatedResponse; ssoCredential.authenticatedResponse;
if (authenticatedResponse) { if (!authenticatedResponse) {
MOZ_LOG(gMacOSWebAuthnServiceLog, mozilla::LogLevel::Debug,
("SSORequestDelegate::didCompleteWithAuthorization: "
"authenticatedResponse is nil"));
mozilla::glean::network_sso::entra_success.Get("invalid_cookie"_ns).Add(1);
[self invokeCallbackOnMainThread];
return;
}
NSDictionary* headers = authenticatedResponse.allHeaderFields; NSDictionary* headers = authenticatedResponse.allHeaderFields;
NSMutableString* headersString = [NSMutableString string]; NSMutableString* headersString = [NSMutableString string];
for (NSString* key in headers) { for (NSString* key in headers) {
[headersString appendFormat:@"%@: %@\n", key, headers[key]]; [headersString appendFormat:@"%@: %@\n", key, headers[key]];
} }
MOZ_LOG( MOZ_LOG(gMacOSWebAuthnServiceLog, mozilla::LogLevel::Debug,
gMacOSWebAuthnServiceLog, mozilla::LogLevel::Debug,
("SSORequestDelegate::didCompleteWithAuthorization: " ("SSORequestDelegate::didCompleteWithAuthorization: "
"authenticatedResponse: \nStatus Code: %ld\nHeaders:\n%s", "authenticatedResponse: \nStatus Code: %ld\nHeaders:\n%s",
(long)authenticatedResponse.statusCode, [headersString UTF8String])); (long)authenticatedResponse.statusCode, [headersString UTF8String]));
@ -95,18 +108,33 @@ class API_AVAILABLE(macos(13.3)) MicrosoftEntraSSOUtils final {
// ”prt_headers":[{"header":{"x-ms-RefreshTokenCredential”:”…”}, // ”prt_headers":[{"header":{"x-ms-RefreshTokenCredential”:”…”},
// ”home_account_id”:”….”}]} // ”home_account_id”:”….”}]}
NSString* ssoCookies = headers[@"sso_cookies"]; NSString* ssoCookies = headers[@"sso_cookies"];
if (ssoCookies) { if (!ssoCookies) {
MOZ_LOG(gMacOSWebAuthnServiceLog, mozilla::LogLevel::Debug,
("SSORequestDelegate::didCompleteWithAuthorization: "
"authenticatedResponse is nil"));
mozilla::glean::network_sso::entra_success.Get("invalid_cookie"_ns).Add(1);
[self invokeCallbackOnMainThread];
return;
}
NSError* err = nil; NSError* err = nil;
NSDictionary* ssoCookiesDict = [NSJSONSerialization NSDictionary* ssoCookiesDict = [NSJSONSerialization
JSONObjectWithData:[ssoCookies JSONObjectWithData:[ssoCookies dataUsingEncoding:NSUTF8StringEncoding]
dataUsingEncoding:NSUTF8StringEncoding]
options:0 options:0
error:&err]; error:&err];
if (!err) { if (err) {
NSMutableArray* allHeaders = [NSMutableArray array]; MOZ_LOG(gMacOSWebAuthnServiceLog, mozilla::LogLevel::Debug,
("SSORequestDelegate::didCompleteWithAuthorization: Error parsing "
"JSON: %s",
[[err localizedDescription] UTF8String]));
mozilla::glean::network_sso::entra_success.Get("invalid_cookie"_ns).Add(1);
[self invokeCallbackOnMainThread];
return;
}
NSMutableArray* allHeaders = [NSMutableArray array];
nsCString entraSuccessLabel; nsCString entraSuccessLabel;
if (ssoCookiesDict[@"device_headers"]) { if (ssoCookiesDict[@"device_headers"]) {
[allHeaders addObject:ssoCookiesDict[@"device_headers"]]; [allHeaders addObject:ssoCookiesDict[@"device_headers"]];
} else { } else {
@ -131,73 +159,49 @@ class API_AVAILABLE(macos(13.3)) MicrosoftEntraSSOUtils final {
// We would like to have both device_headers and prt_headers before // We would like to have both device_headers and prt_headers before
// attaching the headers // attaching the headers
if (allHeaders.count == 2) { if (allHeaders.count != 2) {
MOZ_LOG(gMacOSWebAuthnServiceLog, mozilla::LogLevel::Debug,
("SSORequestDelegate::didCompleteWithAuthorization: "
"sso_cookies has missing headers"));
mozilla::glean::network_sso::entra_success.Get(entraSuccessLabel).Add(1);
[self invokeCallbackOnMainThread];
return;
}
// Append cookie headers retrieved from MS Broker // Append cookie headers retrieved from MS Broker
for (NSArray* headerArray in allHeaders) { for (NSArray* headerArray in allHeaders) {
if (headerArray) { if (!headerArray) {
continue;
}
for (NSDictionary* headerDict in headerArray) { for (NSDictionary* headerDict in headerArray) {
NSDictionary* headers = headerDict[@"header"]; NSDictionary* headers = headerDict[@"header"];
if (headers) { if (!headers) {
continue;
}
for (NSString* key in headers) { for (NSString* key in headers) {
NSString* value = headers[key]; NSString* value = headers[key];
if (value) { if (!value) {
continue;
}
nsAutoString nsKey; nsAutoString nsKey;
nsAutoString nsValue; nsAutoString nsValue;
mozilla::CopyNSStringToXPCOMString(key, nsKey); mozilla::CopyNSStringToXPCOMString(key, nsKey);
mozilla::CopyNSStringToXPCOMString(value, nsValue); mozilla::CopyNSStringToXPCOMString(value, nsValue);
mCallback->AddRequestHeader( mCallback->AddRequestHeader(NS_ConvertUTF16toUTF8(nsKey),
NS_ConvertUTF16toUTF8(nsKey),
NS_ConvertUTF16toUTF8(nsValue)); NS_ConvertUTF16toUTF8(nsValue));
} }
} }
} }
}
}
}
mozilla::glean::network_sso::entra_success.Get("success"_ns).Add(1); mozilla::glean::network_sso::entra_success.Get("success"_ns).Add(1);
} else {
mozilla::glean::network_sso::entra_success.Get(entraSuccessLabel)
.Add(1);
MOZ_LOG(gMacOSWebAuthnServiceLog, mozilla::LogLevel::Debug,
("SSORequestDelegate::didCompleteWithAuthorization: "
"sso_cookies has missing headers"));
}
} else {
MOZ_LOG(gMacOSWebAuthnServiceLog, mozilla::LogLevel::Debug,
("SSORequestDelegate::didCompleteWithAuthorization: "
"Failed to parse sso_cookies: %s",
[[err localizedDescription] UTF8String]));
mozilla::glean::network_sso::entra_success.Get("invalid_cookie"_ns)
.Add(1);
}
} else {
MOZ_LOG(gMacOSWebAuthnServiceLog, mozilla::LogLevel::Debug,
("SSORequestDelegate::didCompleteWithAuthorization: "
"sso_cookies is not present"));
mozilla::glean::network_sso::entra_success.Get("invalid_cookie"_ns)
.Add(1);
}
} else {
MOZ_LOG(gMacOSWebAuthnServiceLog, mozilla::LogLevel::Debug,
("SSORequestDelegate::didCompleteWithAuthorization: "
"authenticatedResponse is nil"));
mozilla::glean::network_sso::entra_success.Get("invalid_cookie"_ns)
.Add(1);
}
} else {
MOZ_LOG(gMacOSWebAuthnServiceLog, mozilla::LogLevel::Debug,
("SSORequestDelegate::didCompleteWithAuthorization: "
"should have ASAuthorizationSingleSignOnCredential"));
mozilla::glean::network_sso::entra_success.Get("no_credential"_ns).Add(1);
}
[self invokeCallbackOnMainThread];
}
- (void)invokeCallbackOnMainThread {
NS_DispatchToMainThread(NS_NewRunnableFunction( NS_DispatchToMainThread(NS_NewRunnableFunction(
"SSORequestDelegate::didCompleteWithAuthorization failure", "SSORequestDelegate::didCompleteWithAuthorization failure",
[callback(mCallback)]() { [callback(mCallback)]() { callback->InvokeCallback(); }));
MOZ_ASSERT(NS_IsMainThread());
callback->InvokeCallback();
}));
} }
- (void)authorizationController:(ASAuthorizationController*)controller - (void)authorizationController:(ASAuthorizationController*)controller
didCompleteWithError:(NSError*)error { didCompleteWithError:(NSError*)error {
nsAutoString errorDescription; nsAutoString errorDescription;