/// <summary> /// The core logic for retrieving the authorization code. It MUST be called from the UI thread. /// </summary> /// <param name="url">The authorization code request URL.</param> /// <param name="taskCancellationToken">Cancellation token.</param> /// <returns>The authorization code response.</returns> private async Task<AuthorizationCodeResponseUrl> ReceivedCodeCoreAsync(AuthorizationCodeRequestUrl url, CancellationToken taskCancellationToken) { // Get the current window. PhoneApplicationFrame rootFrame = Application.Current.RootVisual as PhoneApplicationFrame; PhoneApplicationPage rootPage = rootFrame.Content as PhoneApplicationPage; UIElement currentLayout = rootPage.Content; // Create the web authentication user control and add it to the current window. var webAuthControl = new WebAuthenticationBrokerUserControl(); var oauthLayout = new Grid { HorizontalAlignment = HorizontalAlignment.Stretch, VerticalAlignment = VerticalAlignment.Stretch }; rootPage.Content = oauthLayout; oauthLayout.Children.Add(webAuthControl); try { return await webAuthControl.Launch(url.Build()); } finally { // Change back to the old layout. webAuthControl.Visibility = Visibility.Collapsed; oauthLayout.Children.Remove(webAuthControl); rootPage.Content = currentLayout; } }
/// <summary>Asynchronously receives the authorization code.</summary> /// <param name="url">The authorization code request URL.</param> /// <param name="tcs">Task completion source whose result will be set to the authorization code.</param> private async Task ReceiveCodeAsync(AuthorizationCodeRequestUrl url, TaskCompletionSource<AuthorizationCodeResponseUrl> tcs) { var result = await PasswordVaultDataStore.Default.GetAsync<SerializableWebAuthResult>( SerializableWebAuthResult.Name); if (result == null) { // We should run WebAuthenticationBroker.AuthenticateAndContinue from the UI thread ONLY. await InvokeFromUIThread(() => WebAuthenticationBroker.AuthenticateAndContinue(url.Build(), new Uri(GoogleAuthConsts.LocalhostRedirectUri), null, WebAuthenticationOptions.None)); // No need to return anything, cause the application is going to be suspended now. return; } const string Code = "code="; const string Error = "error="; // Get the index of the error or the code. var index = result.ResponseData.IndexOf(Code); index = index != -1 ? index : result.ResponseData.IndexOf(Error); if (index != -1) { tcs.SetResult(new AuthorizationCodeResponseUrl(result.ResponseData.Substring(index))); return; } tcs.SetException(new TokenResponseException( new TokenErrorResponse { Error = result.ResponseStatus.ToString(), ErrorDescription = "The WebAuthenticationBroker didn't return a code or an error. Details: " + result.ResponseErrorDetail, })); }
public void TestBuild_DefaultValues() { var request = new AuthorizationCodeRequestUrl(AuthorizationCodeUrl); var uri = request.Build(); Assert.That(uri, Is.EqualTo(new Uri(AuthorizationCodeUrl.ToString() + "?response_type=code"))); }
public async Task<AuthorizationCodeResponseUrl> ReceiveCodeAsync(AuthorizationCodeRequestUrl url, CancellationToken taskCancellationToken) { var authorizationUrl = url.Build().ToString(); using (var listener = new HttpListener()) { listener.Prefixes.Add(RedirectUri); try { listener.Start(); Logger.Debug("Open a browser with \"{0}\" URL", authorizationUrl); Process.Start(authorizationUrl); // Wait to get the authorization code response. var context = await listener.GetContextAsync().ConfigureAwait(false); NameValueCollection coll = context.Request.QueryString; // Write a "close" response. using (var writer = new StreamWriter(context.Response.OutputStream)) { writer.WriteLine(ClosePageResponse); writer.Flush(); } context.Response.OutputStream.Close(); // Create a new response URL with a dictionary that contains all the response query parameters. return new AuthorizationCodeResponseUrl(coll.AllKeys.ToDictionary(k => k, k => coll[k])); } finally { listener.Close(); } } }
public Task<AuthorizationCodeResponseUrl> ReceiveCodeAsync(AuthorizationCodeRequestUrl url, CancellationToken taskCancellationToken) { var tcs = new TaskCompletionSource<AuthorizationCodeResponseUrl>(); var authorizationUrl = url.Build().ToString(); Logger.Debug("Open a browser with \"{0}\" URL", authorizationUrl); Process.Start(authorizationUrl); string code = string.Empty; while (string.IsNullOrEmpty(code)) { Console.WriteLine("Please enter code: "); code = Console.ReadLine(); } Logger.Debug("Code is: \"{0}\"", code); tcs.SetResult(new AuthorizationCodeResponseUrl { Code = code }); return tcs.Task; }
public Task<AuthorizationCodeResponseUrl> ReceiveCodeAsync(AuthorizationCodeRequestUrl url, CancellationToken taskCancellationToken) { var authorizationUrl = url.Build().ToString(); #if NETSTANDARD Logger.Debug("Requested user open a browser with \"{0}\" URL", authorizationUrl); Console.WriteLine("Please visit the following URL in a web browser, then enter the code shown after authorization:"); Console.WriteLine(authorizationUrl); Console.WriteLine(); #else Logger.Debug("Open a browser with \"{0}\" URL", authorizationUrl); System.Diagnostics.Process.Start(authorizationUrl); #endif string code = string.Empty; while (string.IsNullOrEmpty(code)) { Console.WriteLine("Please enter code: "); code = Console.ReadLine(); } Logger.Debug("Code is: \"{0}\"", code); return Task.FromResult(new AuthorizationCodeResponseUrl { Code = code }); }
public void TestBuild_EscapeValues() { var request = new AuthorizationCodeRequestUrl(AuthorizationCodeUrl) { ClientId = "pa$$word", RedirectUri = "???", Scope = "SC@PE", State = "!", }; var uri = request.Build(); Assert.That(uri, Is.EqualTo(new Uri(AuthorizationCodeUrl.ToString() + "?response_type=code&client_id=pa%24%24word&redirect_uri=%3F%3F%3F&scope=SC%40PE&state=%21"))); }
public void TestBuild() { var request = new AuthorizationCodeRequestUrl(AuthorizationCodeUrl) { ClientId = "100", RedirectUri = "200", Scope = "SCOPE", State = "state", }; var uri = request.Build(); Assert.That(uri, Is.EqualTo(new Uri(AuthorizationCodeUrl.ToString() + "?response_type=code&client_id=100&redirect_uri=200&scope=SCOPE&state=state"))); }
/// <summary> /// </summary> /// <param name="url"></param> /// <param name="taskCancellationToken"></param> /// <returns> /// </returns> public async Task<AuthorizationCodeResponseUrl> ReceiveCodeAsync(AuthorizationCodeRequestUrl url, CancellationToken taskCancellationToken) { var completionSource = new TaskCompletionSource<AuthorizationCodeResponseUrl>(); var fileName = url.Build().ToString(); Process.Start(fileName); var googleAuthCode = await GetCodeDeledateFunc(); completionSource.SetResult(new AuthorizationCodeResponseUrl { Code = googleAuthCode }); return completionSource.Task.Result; }
/// <summary>Asynchronously receives the authorization code.</summary> /// <param name="url">The authorization code request URL.</param> /// <param name="tcs">Task completion source whose result will be set to the authorization code.</param> private async Task ReceiveCodeAsync(AuthorizationCodeRequestUrl url, TaskCompletionSource<AuthorizationCodeResponseUrl> tcs) { const string Code = "code="; const string Error = "error="; try { WebAuthenticationResult result = await WebAuthenticationBroker.AuthenticateAsync (WebAuthenticationOptions.UseTitle, url.Build(), new Uri(GoogleAuthConsts.ApprovalUrl)); if (!string.IsNullOrEmpty(result.ResponseData)) { // Get the index of the error or the code. var index = result.ResponseData.IndexOf(Code); index = index != -1 ? index : result.ResponseData.IndexOf(Error); if (index != -1) { tcs.SetResult(new AuthorizationCodeResponseUrl(result.ResponseData.Substring(index))); return; } } tcs.SetException(new TokenResponseException( new TokenErrorResponse { Error = result.ResponseStatus.ToString(), ErrorDescription = "The WebAuthenticationBroker didn't return a code or an error. Details:" + result.ResponseErrorDetail, })); } catch (Exception ex) { tcs.SetException(ex); } }
/// <summary> /// Receives the authorization code. /// </summary> /// <param name="url">The authorization code request URL</param> /// <param name="taskCancellationToken">Cancellation token</param> /// <returns>The authorization code response</returns> public Task<AuthorizationCodeResponseUrl> ReceiveCodeAsync(AuthorizationCodeRequestUrl url, CancellationToken taskCancellationToken) { var authorizationUrl = url.Build().ToString(); var tcsk = new TaskCompletionSource<AuthorizationCodeResponseUrl> (); new NSObject ().BeginInvokeOnMainThread (async () => { var window = new MacAuthWindow(authorizationUrl, RedirectUri); var result = await window.ShowDialogAsync(NSApplication.SharedApplication.KeyWindow); tcsk.SetResult(result); }); return tcsk.Task; }