Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot separate user cancel from Logout done in oidcclient logout async #437

Open
Gaburiere-Remira opened this issue Jun 26, 2024 · 0 comments

Comments

@Gaburiere-Remira
Copy link

Gaburiere-Remira commented Jun 26, 2024

i've implemented a net maui 8 mobile application for iOS and Android with IdentityModel.OidcClient v5.0.2. i've an issue logging out in iOS due to the native display alert prompt asking the user if really want to log out

logout_prompt

if user tap on "Annulla" button (orange circle), the IBrowser implementation used to open browser in app throws TaskCanceledException.

Here i can intercept the exception and return as result error with detail "UserCancel" as you can see from the code below:

internal class WebAuthenticatorBrowser : IBrowser
    {
        private readonly IWebAuthenticator _webAuthenticator;

        public WebAuthenticatorBrowser(IWebAuthenticator webAuthenticator)
        {
            _webAuthenticator = webAuthenticator;
        }
        
        public async Task<BrowserResult> InvokeAsync(BrowserOptions options, CancellationToken cancellationToken = default)
        {
            try
            {
                var authResult =
                    await _webAuthenticator.AuthenticateAsync(new Uri(options.StartUrl), new Uri(options.EndUrl));
                var authorizeResponse = ToRawIdentityUrl(options.EndUrl, authResult);

                return new BrowserResult
                {
                    Response = authorizeResponse
                };
            }
            catch (TaskCanceledException e)
            {
                return new BrowserResult()
                {
                    ResultType = BrowserResultType.UserCancel,
                    Error = e.ToString()
                };
            }
            catch (Exception ex)
            {
                return new BrowserResult()
                {
                    ResultType = BrowserResultType.UnknownError,
                    Error = ex.ToString()
                };
            }
        }

        public string ToRawIdentityUrl(string redirectUrl, CustomWebAuthenticatorResult result)
        {
            IEnumerable<string> parameters = result.Properties.Select(pair => $"{pair.Key}={pair.Value}");
            var values = string.Join("&", parameters);

            return $"{redirectUrl}#{values}";
        }
    }

then below the service using oidcclient:

public async Task<bool> LogoutAsync()
{
    var oidcClient = _oidcService.BuildOidcClient();
    var logoutResult = await oidcClient.LogoutAsync();
    if (logoutResult.Error.ToLower() == BrowserResultType.UserCancel.ToString().ToLower() && string.IsNullOrEmpty(logoutResult.Response))
        return false;
    DeleteTokens();
    return true;
}

The issue is when user tap on "Continua" in the prompt: the WebAuthenticationBrowser opens the logout page on our identity server 4 identity provider as shown below:

logout_page

the page performs logout and remains there, so the user must tap on "annulla" button inside the browser. here is the trouble: tapping on cancel in web browser and on cancel on initial prompt performs the same result namely TaskCanceledException.

So in this case i am not able to discriminate between actual user cancellation or login done and user closing web page.

Have you any idea on how to solve this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant