Skip to content

Commit ff12350

Browse files
cassianodialpadWilliamDenniss
authored andcommitted
Fixed compatibility with ASWebAuthenticationSession/SFAuthenticationSession when in guided access mode
ASWebAuthenticationSession and SFAuthenticationSession are not supported in guided access mode (rdar://40809553).
1 parent 1f2aa58 commit ff12350

File tree

1 file changed

+76
-69
lines changed

1 file changed

+76
-69
lines changed

Source/iOS/OIDExternalUserAgentIOS.m

Lines changed: 76 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -61,82 +61,93 @@ - (BOOL)presentExternalUserAgentRequest:(id<OIDExternalUserAgentRequest>)request
6161

6262
_externalUserAgentFlowInProgress = YES;
6363
_session = session;
64-
BOOL openedSafari = NO;
64+
BOOL openedUserAgent = NO;
6565
NSURL *requestURL = [request externalUserAgentRequestURL];
6666

6767
// iOS 12 and later, use ASWebAuthenticationSession
6868
if (@available(iOS 12.0, *)) {
69-
__weak OIDExternalUserAgentIOS *weakSelf = self;
70-
NSString *redirectScheme = request.redirectScheme;
71-
ASWebAuthenticationSession *authenticationVC =
72-
[[ASWebAuthenticationSession alloc] initWithURL:requestURL
73-
callbackURLScheme:redirectScheme
74-
completionHandler:^(NSURL * _Nullable callbackURL,
75-
NSError * _Nullable error) {
76-
__strong OIDExternalUserAgentIOS *strongSelf = weakSelf;
77-
if (!strongSelf) {
78-
return;
79-
}
80-
strongSelf->_webAuthenticationVC = nil;
81-
if (callbackURL) {
82-
[strongSelf->_session resumeExternalUserAgentFlowWithURL:callbackURL];
83-
} else {
84-
NSError *safariError =
85-
[OIDErrorUtilities errorWithCode:OIDErrorCodeUserCanceledAuthorizationFlow
86-
underlyingError:error
87-
description:nil];
88-
[strongSelf->_session failExternalUserAgentFlowWithError:safariError];
89-
}
90-
}];
91-
_webAuthenticationVC = authenticationVC;
92-
openedSafari = [authenticationVC start];
69+
// ASWebAuthenticationSession doesn't work with guided access (rdar://40809553)
70+
if (!UIAccessibilityIsGuidedAccessEnabled()) {
71+
__weak OIDExternalUserAgentIOS *weakSelf = self;
72+
NSString *redirectScheme = request.redirectScheme;
73+
ASWebAuthenticationSession *authenticationVC =
74+
[[ASWebAuthenticationSession alloc] initWithURL:requestURL
75+
callbackURLScheme:redirectScheme
76+
completionHandler:^(NSURL * _Nullable callbackURL,
77+
NSError * _Nullable error) {
78+
__strong OIDExternalUserAgentIOS *strongSelf = weakSelf;
79+
if (!strongSelf) {
80+
return;
81+
}
82+
strongSelf->_webAuthenticationVC = nil;
83+
if (callbackURL) {
84+
[strongSelf->_session resumeExternalUserAgentFlowWithURL:callbackURL];
85+
} else {
86+
NSError *safariError =
87+
[OIDErrorUtilities errorWithCode:OIDErrorCodeUserCanceledAuthorizationFlow
88+
underlyingError:error
89+
description:nil];
90+
[strongSelf->_session failExternalUserAgentFlowWithError:safariError];
91+
}
92+
}];
93+
_webAuthenticationVC = authenticationVC;
94+
openedUserAgent = [authenticationVC start];
95+
}
96+
}
9397
// iOS 11, use SFAuthenticationSession
94-
} else if (@available(iOS 11.0, *)) {
95-
__weak OIDExternalUserAgentIOS *weakSelf = self;
96-
NSString *redirectScheme = request.redirectScheme;
97-
SFAuthenticationSession *authenticationVC =
98-
[[SFAuthenticationSession alloc] initWithURL:requestURL
99-
callbackURLScheme:redirectScheme
100-
completionHandler:^(NSURL * _Nullable callbackURL,
101-
NSError * _Nullable error) {
102-
__strong OIDExternalUserAgentIOS *strongSelf = weakSelf;
103-
if (!strongSelf) {
104-
return;
105-
}
106-
strongSelf->_authenticationVC = nil;
107-
if (callbackURL) {
108-
[strongSelf->_session resumeExternalUserAgentFlowWithURL:callbackURL];
109-
} else {
110-
NSError *safariError =
111-
[OIDErrorUtilities errorWithCode:OIDErrorCodeUserCanceledAuthorizationFlow
112-
underlyingError:error
113-
description:@"User cancelled."];
114-
[strongSelf->_session failExternalUserAgentFlowWithError:safariError];
115-
}
116-
}];
117-
_authenticationVC = authenticationVC;
118-
openedSafari = [authenticationVC start];
98+
if (@available(iOS 11.0, *)) {
99+
// SFAuthenticationSession doesn't work with guided access (rdar://40809553)
100+
if (!openedUserAgent && !UIAccessibilityIsGuidedAccessEnabled()) {
101+
__weak OIDExternalUserAgentIOS *weakSelf = self;
102+
NSString *redirectScheme = request.redirectScheme;
103+
SFAuthenticationSession *authenticationVC =
104+
[[SFAuthenticationSession alloc] initWithURL:requestURL
105+
callbackURLScheme:redirectScheme
106+
completionHandler:^(NSURL * _Nullable callbackURL,
107+
NSError * _Nullable error) {
108+
__strong OIDExternalUserAgentIOS *strongSelf = weakSelf;
109+
if (!strongSelf) {
110+
return;
111+
}
112+
strongSelf->_authenticationVC = nil;
113+
if (callbackURL) {
114+
[strongSelf->_session resumeExternalUserAgentFlowWithURL:callbackURL];
115+
} else {
116+
NSError *safariError =
117+
[OIDErrorUtilities errorWithCode:OIDErrorCodeUserCanceledAuthorizationFlow
118+
underlyingError:error
119+
description:@"User cancelled."];
120+
[strongSelf->_session failExternalUserAgentFlowWithError:safariError];
121+
}
122+
}];
123+
_authenticationVC = authenticationVC;
124+
openedUserAgent = [authenticationVC start];
125+
}
126+
}
119127
// iOS 9 and 10, use SFSafariViewController
120-
} else if (@available(iOS 9.0, *)) {
121-
SFSafariViewController *safariVC =
122-
[[SFSafariViewController alloc] initWithURL:requestURL];
123-
safariVC.delegate = self;
124-
_safariVC = safariVC;
125-
[_presentingViewController presentViewController:safariVC animated:YES completion:nil];
126-
openedSafari = YES;
128+
if (@available(iOS 9.0, *)) {
129+
if (!openedUserAgent) {
130+
SFSafariViewController *safariVC =
131+
[[SFSafariViewController alloc] initWithURL:requestURL];
132+
safariVC.delegate = self;
133+
_safariVC = safariVC;
134+
[_presentingViewController presentViewController:safariVC animated:YES completion:nil];
135+
openedUserAgent = YES;
136+
}
137+
}
127138
// iOS 8 and earlier, use mobile Safari
128-
} else {
129-
openedSafari = [[UIApplication sharedApplication] openURL:requestURL];
139+
if (!openedUserAgent){
140+
openedUserAgent = [[UIApplication sharedApplication] openURL:requestURL];
130141
}
131142

132-
if (!openedSafari) {
143+
if (!openedUserAgent) {
133144
[self cleanUp];
134145
NSError *safariError = [OIDErrorUtilities errorWithCode:OIDErrorCodeSafariOpenError
135146
underlyingError:nil
136147
description:@"Unable to open Safari."];
137148
[session failExternalUserAgentFlowWithError:safariError];
138149
}
139-
return openedSafari;
150+
return openedUserAgent;
140151
}
141152

142153
- (void)dismissExternalUserAgentAnimated:(BOOL)animated completion:(void (^)(void))completion {
@@ -154,21 +165,17 @@ - (void)dismissExternalUserAgentAnimated:(BOOL)animated completion:(void (^)(voi
154165

155166
[self cleanUp];
156167

157-
if (@available(iOS 12.0, *)) {
168+
if (webAuthenticationVC) {
158169
// dismiss the ASWebAuthenticationSession
159170
[webAuthenticationVC cancel];
160171
if (completion) completion();
161-
} else if (@available(iOS 11.0, *)) {
172+
} else if (authenticationVC) {
162173
// dismiss the SFAuthenticationSession
163174
[authenticationVC cancel];
164175
if (completion) completion();
165-
} else if (@available(iOS 9.0, *)) {
176+
} else if (safariVC) {
166177
// dismiss the SFSafariViewController
167-
if (safariVC) {
168-
[safariVC dismissViewControllerAnimated:YES completion:completion];
169-
} else {
170-
if (completion) completion();
171-
}
178+
[safariVC dismissViewControllerAnimated:YES completion:completion];
172179
} else {
173180
if (completion) completion();
174181
}

0 commit comments

Comments
 (0)