Skip to content

Commit

Permalink
[SDK-2197] Support for PrefersEphemeralWebBrowserSession (#173)
Browse files Browse the repository at this point in the history
Closes #157
  • Loading branch information
frederikprijck authored Jan 4, 2021
1 parent 19cc8fe commit abe9a63
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 5 deletions.
41 changes: 38 additions & 3 deletions src/Auth0.OidcClient.iOS/ASWebAuthenticationSessionBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,44 @@ namespace Auth0.OidcClient
/// </summary>
public class ASWebAuthenticationSessionBrowser : IOSBrowserBase
{
/// <summary>
/// Configuration for the ASWebAuthenticationSession.
/// </summary>
public ASWebAuthenticationSessionOptions SessionOptions { get; }

/// <summary>
/// Creates a new instance of the ASWebAuthenticationSession Browser.
/// </summary>
/// <param name="sessionOptions">The <see cref="ASWebAuthenticationSessionOptions"/> specifying the configuration for the ASWebAuthenticationSession.</param>
/// <example>
/// If any custom browser configuration is needed (e.g. using <see cref="ASWebAuthenticationSessionOptions.PrefersEphemeralWebBrowserSession"/>),
/// a new browser instance should be instantiated and passed to <see cref="Auth0ClientOptions.Browser"/>.
/// <code>
/// var client = new Auth0Client(new Auth0ClientOptions
/// {
/// Domain = "YOUR_AUTH0_DOMAIN",
/// ClientId = "YOUR_AUTH0_CLIENT_ID",
/// Browser = new ASWebAuthenticationSessionBrowser(
/// new ASWebAuthenticationSessionOptions
/// {
/// PrefersEphemeralWebBrowserSession = true
/// }
/// )
/// });
/// </code>
/// </example>
public ASWebAuthenticationSessionBrowser(ASWebAuthenticationSessionOptions sessionOptions = null)
{
SessionOptions = sessionOptions;
}

/// <inheritdoc/>
protected override Task<BrowserResult> Launch(BrowserOptions options, CancellationToken cancellationToken = default)
{
return Start(options);
return Start(options, SessionOptions);
}

internal static Task<BrowserResult> Start(BrowserOptions options)
internal static Task<BrowserResult> Start(BrowserOptions options, ASWebAuthenticationSessionOptions sessionOptions = null)
{
var tcs = new TaskCompletionSource<BrowserResult>();

Expand All @@ -32,9 +63,13 @@ internal static Task<BrowserResult> Start(BrowserOptions options)
asWebAuthenticationSession.Dispose();
});

// iOS 13 requires the PresentationContextProvider set
if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
{
// iOS 13 requires the PresentationContextProvider set
asWebAuthenticationSession.PresentationContextProvider = new PresentationContextProviderToSharedKeyWindow();
// PrefersEphemeralWebBrowserSession is only available on iOS 13 and up.
asWebAuthenticationSession.PrefersEphemeralWebBrowserSession = sessionOptions != null ? sessionOptions.PrefersEphemeralWebBrowserSession : false;
}

asWebAuthenticationSession.Start();

Expand Down
17 changes: 17 additions & 0 deletions src/Auth0.OidcClient.iOS/ASWebAuthenticationSessionOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Auth0.OidcClient
{
/// <summary>
/// Specifies options that can be passed to <see cref="ASWebAuthenticationSessionBrowser"/> implementations.
/// </summary>
public class ASWebAuthenticationSessionOptions
{
/// <summary>
/// Specify whether or not EphemeralWebBrowserSessions should be preferred. Defaults to false.
/// </summary>
/// <remarks>
/// Setting <see cref="PrefersEphemeralWebBrowserSession"/> to true will disable <see href="https://auth0.com/docs/sso">Single Sign On (SSO)</see> on iOS 13+.
/// As a consequence of that, it will also prevent from showing the popup that's being used to ask consent for using Auth0 to sign in.
/// </remarks>
public bool PrefersEphemeralWebBrowserSession { get; set; }
}
}
1 change: 1 addition & 0 deletions src/Auth0.OidcClient.iOS/Auth0.OidcClient.iOS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<Compile Include="IOSBrowserBase.cs" />
<Compile Include="Auth0Client.cs" />
<Compile Include="ASWebAuthenticationSessionBrowser.cs" />
<Compile Include="ASWebAuthenticationSessionOptions.cs" />
<Compile Include="SFAuthenticationSessionBrowser.cs" />
<Compile Include="SFSafariViewControllerBrowser.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
9 changes: 7 additions & 2 deletions test/iOS/MyViewController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ public override void ViewDidLoad()
_auth0Client = new Auth0Client(new Auth0ClientOptions
{
Domain = "auth0-dotnet-integration-tests.auth0.com",
ClientId = "qmss9A66stPWTOXjR6X1OeA0DLadoNP2"
});
ClientId = "qmss9A66stPWTOXjR6X1OeA0DLadoNP2",
// Optional.
// The SDK will determine which browser to use when omitted.
// In case you need to specify custom browser configuration,
// you should create a new Browser instance and provide it with the corresponding options.
Browser = new ASWebAuthenticationSessionBrowser(new ASWebAuthenticationSessionOptions { PrefersEphemeralWebBrowserSession = false })
}) ;

LoginButton.Clicked += Login;
UserInfoButton.Clicked += UserInfo;
Expand Down

0 comments on commit abe9a63

Please sign in to comment.