public void Listen <TToken>( Uri callbackUri, TaskCompletionSource <TToken> completionSource) where TToken : TokenCredentials { Platform.Current.ProtocolLaunch += (s, e) => { if (e.Uri.ToString().Contains(callbackUri.ToString())) { try { var result = _handler .ParseAndValidateCallback <TToken>( e.Uri); if (result != null) { completionSource.SetResult(result); } } catch (Exception ex) { completionSource.SetException(ex); } } }; }
public void Listen <TToken>( Uri callbackUri, TaskCompletionSource <TToken> completionSource) where TToken : TokenCredentials { #pragma warning disable 4014 _httpServer.CreateServer( (request, response) => { response.WriteHtmlString(FRAGMENT_HTML); try { var result = _handler .ParseAndValidateCallback <TToken>( request.Uri); if (result != null) { completionSource.SetResult(result); } } catch (Exception e) { completionSource.SetException(e); } }) .Listen(callbackUri); #pragma warning restore 4014 completionSource.Task.ContinueWith(t => _httpServer.Close()); }
public async Task <TToken> Authorize <TToken>( Uri callbackUri, Uri authorizationUri) where TToken : TokenCredentials { var taskCompletionSource = new TaskCompletionSource <TToken>(); var webViewCompletionSource = new TaskCompletionSource <UIWebView>(); Platform.Current.RunOnMainThread(async() => { var context = Platform.Current.Context; var controller = new WebViewController( context.View.Bounds, webViewCompletionSource); context.PresentViewController(controller, false, null); var webView = await webViewCompletionSource.Task.ConfigureAwait(false); if (!Platform.Current.IsOnline) { throw new NoConnectivityException( StringResources.OfflineConnectivityException); } webView.LoadRequest(new NSUrlRequest(new NSUrl(authorizationUri.ToString()))); webView.ShouldStartLoad = (view, request, type) => { if (request.Url.ToString().Contains(callbackUri.ToString())) { webView.LoadHtmlString( StringResources.OAuthCallbackResponse, new NSUrl("/")); var result = _handler .ParseAndValidateCallback <TToken>( new Uri(request.Url.ToString())); taskCompletionSource.SetResult(result); controller.DismissViewController(false, null); return(false); } return(true); }; }); return(await taskCompletionSource.Task.ConfigureAwait(false)); }
//TODO: inject these static platform references public async Task <TToken> Authorize <TToken>( Uri callbackUri, Uri authorizationUri) where TToken : TokenCredentials { var viewCompletionSource = new TaskCompletionSource <WebView>(); var tokenCompletionSource = new TaskCompletionSource <TToken>(); Platform.Current.RunOnMainThread(async() => { Platform.Current.Context.Navigate( typeof(WebViewPage), viewCompletionSource); var webView = await viewCompletionSource .Task .ConfigureAwait(false); webView.NavigationStarting += (sender, args) => { if (args.Uri != null && args.Uri.ToString().Contains( callbackUri.ToString())) { var cancel = args.Cancel; webView.NavigateToString(StringResources.OAuthCallbackResponse); var result = _handler .ParseAndValidateCallback <TToken>( args.Uri); tokenCompletionSource.SetResult(result); Platform.Current.Context.GoBack(); } }; if (!Platform.Current.IsOnline) { throw new NoConnectivityException( StringResources.OfflineConnectivityException); } webView.Navigate(authorizationUri); }); return(await tokenCompletionSource.Task.ConfigureAwait(false)); }