AppAuth-iOS
AppAuth-iOS copied to clipboard
How to open up the URL after logged in?
I've already logged in and I'm just trying to open up the URL of the site protected by SSO, how it could be done?
In AppAuth-iOS context, which, I assume, we ought to be in, the authentication cookie is to be a persistent
one in iOS 11+. At least by default. A session cookie may be shared if a custom OIDExternalUserAgentIOS
used to open Safari for visiting the authorization endpoint. Similar to: https://gist.github.com/WilliamDenniss/18f3779b4a310361bb955cf4e534f29c. If I understand the question correctly.
Sorry, maybe I should have elaborated more.
We use SSO https://sso.abc.com
, so I logged in successfully via AppAuth. At some point, I need to open a URL of my website. Basically, I do it like below. But it
if let url = URL(string: "https://www.abc.com/popular") {
let vc = SFSafariViewController(url: url)
present(vc, animated: true)
}
But when I did that I see myself NOT logged in on the website. So the question is how can I open a browser as logged-in.
It depends on iOS version. In iOS 11+ SFSafariViewController
does not share cookies with anything; hence, cannot be used for SSO. In iOS 9-10 it does–with its own instances. AppAuth-iOS uses this class in 9-10. As I mentioned, with the authentication classes employed in iOS 11-12, SFAuthenticationSession
and ASWebAuthenticationSession
, the cookie needs to be a persistent one.
we need to customize the appAuth lib. let's start.
- (void)openURLWithSafariViewController:(NSURL *)url;
Add this signature to OIDExternalUserAgentIOS.h
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
- (void)openURLWithSafariViewController:(NSURL *)url {
if (@available(iOS 13.0, *)) {
if (!UIAccessibilityIsGuidedAccessEnabled()) {
NSString *redirectScheme = @"com.terravirtua.example";
ASWebAuthenticationSession *authenticationVC =
[[ASWebAuthenticationSession alloc] initWithURL: url
callbackURLScheme:redirectScheme
completionHandler:^(NSURL * _Nullable callbackURL,
NSError * _Nullable error) {}];
authenticationVC.presentationContextProvider = self;
//authenticationVC.prefersEphemeralWebBrowserSession = true;
[authenticationVC start];
}
}
}
#endif
Add this method to OIDExternalUserAgentIOS.m
Usage in swift
guard let agent = OIDExternalUserAgentIOS(presenting: self,prefersEphemeralSession: false) else {
return
}
agent.openURL(withSafariViewController: URL(string: "https://www.example.com/")!)
with the authentication classes employed in iOS 11-12,
SFAuthenticationSession
andASWebAuthenticationSession
Adding to this, for iOS 11+ what worked for me was instead of opening a new SFSafariViewController
instance, I opened an ASWebAuthenticationSession
. On open, I remained authenticated in my website (after logging in previously).
But if you're logging in on https://sso.abc.com
, you can only be expected to be logged in for https://sso.abc.com/popular
, only in the same domain.
with the authentication classes employed in iOS 11-12,
SFAuthenticationSession
andASWebAuthenticationSession
Adding to this, for iOS 11+ what worked for me was instead of opening a new
SFSafariViewController
instance, I opened anASWebAuthenticationSession
. On open, I remained authenticated in my website (after logging in previously).But if you're logging in on
https://sso.abc.com
, you can only be expected to be logged in forhttps://sso.abc.com/popular
, only in the same domain.
Yes.. absolutely right.