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

Fix exception on multiple AuthenticateAsync calls #58

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,16 @@ public AndroidWebAuthenticationUi(Context context)
this.Context = context;
}

public Context Context { get; private set; }
public Context Context { get; }

public Task<IDictionary<string, string>> AuthenticateAsync(Uri requestUri, Uri callbackUri)
{
TaskCompletionSource<IDictionary<string, string>> tcs = new TaskCompletionSource<IDictionary<string, string>>();

this.Completed += (s, e) =>
{
tcs.SetResult(e.AuthorizationParameters);
};
var handler = new AuthenticationResultHandler(this, tcs);

this.Failed += (s, e) =>
{
tcs.SetException(e.Error);
};
this.Completed += handler.OnCompleted;
this.Failed += handler.OnFailed;

string stateKey = AndroidAuthenticationState.Default.Add<AndroidWebAuthenticationUi>(this);
Intent intent = new Intent(this.Context, typeof(AndroidWebAuthenticationActivity));
Expand All @@ -59,5 +54,31 @@ internal void OnFailed(AuthFailedEventArgs e)
Failed(this, e);
}
}

private sealed class AuthenticationResultHandler
{
private readonly AndroidWebAuthenticationUi _webAuthenticationUi;
private readonly TaskCompletionSource<IDictionary<string, string>> _tcs;

public AuthenticationResultHandler(AndroidWebAuthenticationUi webAuthenticationUi, TaskCompletionSource<IDictionary<string, string>> tcs)
{
_webAuthenticationUi = webAuthenticationUi;
_tcs = tcs;
}

public void OnCompleted(object sender, AuthCompletedEventArgs e)
{
_webAuthenticationUi.Completed -= OnCompleted; // unsubscribe

_tcs.SetResult(e.AuthorizationParameters);
}

public void OnFailed(object sender, AuthFailedEventArgs e)
{
_webAuthenticationUi.Failed -= OnFailed; // unsubscribe

_tcs.SetException(e.Error);
}
}
}
}