AppAuth-iOS icon indicating copy to clipboard operation
AppAuth-iOS copied to clipboard

How to open up the URL after logged in?

Open volkanbicer opened this issue 5 years ago • 6 comments

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?

volkanbicer avatar Jul 05 '19 06:07 volkanbicer

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.

lapinek avatar Jul 08 '19 17:07 lapinek

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.

volkanbicer avatar Jul 09 '19 10:07 volkanbicer

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.

lapinek avatar Jul 10 '19 16:07 lapinek

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/")!)

Jamisyed avatar Sep 19 '23 19:09 Jamisyed

with the authentication classes employed in iOS 11-12, SFAuthenticationSession and ASWebAuthenticationSession

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.

pvtan avatar Nov 09 '23 07:11 pvtan

with the authentication classes employed in iOS 11-12, SFAuthenticationSession and ASWebAuthenticationSession

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.

Yes.. absolutely right.

Jamisyed avatar Nov 26 '23 19:11 Jamisyed