forked from mirrors/gecko-dev
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:
parent
941b037ac9
commit
3c8447886d
1 changed files with 127 additions and 123 deletions
|
|
@ -65,25 +65,38 @@ class API_AVAILABLE(macos(13.3)) MicrosoftEntraSSOUtils final {
|
|||
}
|
||||
- (void)authorizationController:(ASAuthorizationController*)controller
|
||||
didCompleteWithAuthorization:(ASAuthorization*)authorization {
|
||||
if ([authorization.credential
|
||||
isKindOfClass:[ASAuthorizationSingleSignOnCredential class]]) {
|
||||
ASAuthorizationSingleSignOnCredential* ssoCredential =
|
||||
[authorization.credential
|
||||
isKindOfClass:[ASAuthorizationSingleSignOnCredential class]]
|
||||
? (ASAuthorizationSingleSignOnCredential*)authorization.credential
|
||||
: nil;
|
||||
|
||||
if (!ssoCredential) {
|
||||
MOZ_LOG(gMacOSWebAuthnServiceLog, mozilla::LogLevel::Debug,
|
||||
("SSORequestDelegate::didCompleteWithAuthorization: "
|
||||
"got ASAuthorizationSingleSignOnCredential"));
|
||||
|
||||
ASAuthorizationSingleSignOnCredential* ssoCredential =
|
||||
(ASAuthorizationSingleSignOnCredential*)authorization.credential;
|
||||
"should have ASAuthorizationSingleSignOnCredential"));
|
||||
mozilla::glean::network_sso::entra_success.Get("no_credential"_ns).Add(1);
|
||||
[self invokeCallbackOnMainThread];
|
||||
return;
|
||||
}
|
||||
|
||||
NSHTTPURLResponse* 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;
|
||||
NSMutableString* headersString = [NSMutableString string];
|
||||
for (NSString* key in headers) {
|
||||
[headersString appendFormat:@"%@: %@\n", key, headers[key]];
|
||||
}
|
||||
MOZ_LOG(
|
||||
gMacOSWebAuthnServiceLog, mozilla::LogLevel::Debug,
|
||||
MOZ_LOG(gMacOSWebAuthnServiceLog, mozilla::LogLevel::Debug,
|
||||
("SSORequestDelegate::didCompleteWithAuthorization: "
|
||||
"authenticatedResponse: \nStatus Code: %ld\nHeaders:\n%s",
|
||||
(long)authenticatedResponse.statusCode, [headersString UTF8String]));
|
||||
|
|
@ -95,18 +108,33 @@ class API_AVAILABLE(macos(13.3)) MicrosoftEntraSSOUtils final {
|
|||
// ”prt_headers":[{"header":{"x-ms-RefreshTokenCredential”:”…”},
|
||||
// ”home_account_id”:”….”}]}
|
||||
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;
|
||||
NSDictionary* ssoCookiesDict = [NSJSONSerialization
|
||||
JSONObjectWithData:[ssoCookies
|
||||
dataUsingEncoding:NSUTF8StringEncoding]
|
||||
JSONObjectWithData:[ssoCookies dataUsingEncoding:NSUTF8StringEncoding]
|
||||
options:0
|
||||
error:&err];
|
||||
|
||||
if (!err) {
|
||||
NSMutableArray* allHeaders = [NSMutableArray array];
|
||||
if (err) {
|
||||
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;
|
||||
|
||||
if (ssoCookiesDict[@"device_headers"]) {
|
||||
[allHeaders addObject:ssoCookiesDict[@"device_headers"]];
|
||||
} 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
|
||||
// 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
|
||||
for (NSArray* headerArray in allHeaders) {
|
||||
if (headerArray) {
|
||||
if (!headerArray) {
|
||||
continue;
|
||||
}
|
||||
for (NSDictionary* headerDict in headerArray) {
|
||||
NSDictionary* headers = headerDict[@"header"];
|
||||
if (headers) {
|
||||
if (!headers) {
|
||||
continue;
|
||||
}
|
||||
for (NSString* key in headers) {
|
||||
NSString* value = headers[key];
|
||||
if (value) {
|
||||
if (!value) {
|
||||
continue;
|
||||
}
|
||||
nsAutoString nsKey;
|
||||
nsAutoString nsValue;
|
||||
mozilla::CopyNSStringToXPCOMString(key, nsKey);
|
||||
mozilla::CopyNSStringToXPCOMString(value, nsValue);
|
||||
mCallback->AddRequestHeader(
|
||||
NS_ConvertUTF16toUTF8(nsKey),
|
||||
mCallback->AddRequestHeader(NS_ConvertUTF16toUTF8(nsKey),
|
||||
NS_ConvertUTF16toUTF8(nsValue));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
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(
|
||||
"SSORequestDelegate::didCompleteWithAuthorization failure",
|
||||
[callback(mCallback)]() {
|
||||
MOZ_ASSERT(NS_IsMainThread());
|
||||
callback->InvokeCallback();
|
||||
}));
|
||||
[callback(mCallback)]() { callback->InvokeCallback(); }));
|
||||
}
|
||||
|
||||
- (void)authorizationController:(ASAuthorizationController*)controller
|
||||
didCompleteWithError:(NSError*)error {
|
||||
nsAutoString errorDescription;
|
||||
|
|
|
|||
Loading…
Reference in a new issue