diff --git a/src/private/AuthWebServer.ps1 b/src/private/AuthWebServer.ps1 index 280d31b..d6ac5ee 100644 --- a/src/private/AuthWebServer.ps1 +++ b/src/private/AuthWebServer.ps1 @@ -13,28 +13,52 @@ Function Wait-GoogleAuthApiToken $HttpListener.Start() $authCodeReceived = $False - + while ($HttpListener.IsListening -and -not $authCodeReceived) { - $HttpContext = $HttpListener.GetContext() - $HttpRequest = $HttpContext.Request - $Query = $HttpRequest.QueryString + # Use async method with timeout to allow for cancellation + $contextTask = $HttpListener.GetContextAsync() + + # Wait for either a request or cancellation (check every 500ms) + while (-not $contextTask.IsCompleted) { + Start-Sleep -Milliseconds 500 + + # Check if user pressed Ctrl+C by testing if we can write to console + try { + [Console]::TreatControlCAsInput = $false + if ([Console]::KeyAvailable) { + $key = [Console]::ReadKey($true) + if ($key.Key -eq 'C' -and $key.Modifiers -eq 'Control') { + Write-Information "`nCancellation requested. Stopping auth server..." + return $null + } + } + } + catch { + # Ignore console access errors + } + } + + if ($contextTask.IsCompleted) { + $HttpContext = $contextTask.Result + $HttpRequest = $HttpContext.Request + $Query = $HttpRequest.QueryString - if ($null -ne $Query["code"]) { - $authCode = $Query["code"] - Write-Output $authCode - Write-Verbose "Received auth-code: $authCode" - $authCodeReceived = $true + if ($null -ne $Query["code"]) { + $authCode = $Query["code"] + Write-Output $authCode + Write-Verbose "Received auth-code: $authCode" + $authCodeReceived = $true - # Send "Thanks!" - $buffer = [System.Text.Encoding]::UTF8.GetBytes("
Good Job! Successfully authorized PSBlogger. You can close this browser window now.") - $response = $HttpContext.Response - $response.ContentLength64 = $buffer.Length - $output = $response.OutputStream; - $output.Write($buffer,0,$buffer.Length) - $output.Close() | Write-Verbose - } + # Send "Thanks!" + $buffer = [System.Text.Encoding]::UTF8.GetBytes("Good Job! Successfully authorized PSBlogger. You can close this browser window now.") + $response = $HttpContext.Response + $response.ContentLength64 = $buffer.Length + $output = $response.OutputStream; + $output.Write($buffer,0,$buffer.Length) + $output.Close() | Write-Verbose + } + } } - Write-Verbose "Stopping HttpListener." $HttpListener.Stop() Write-Verbose "Stopped HttpListener." @@ -42,7 +66,7 @@ Function Wait-GoogleAuthApiToken catch { # TODO: Catch Permission denied error and warn about running from an elevated prompt # or add Requires -Administrator - Write-Error $_.ToString() + Write-Error $_.ToString() -ErrorAction Stop } finally { if ($null -ne $HttpListener) { diff --git a/src/public/Initialize-Blogger.ps1 b/src/public/Initialize-Blogger.ps1 index 0983315..25607ba 100644 --- a/src/public/Initialize-Blogger.ps1 +++ b/src/public/Initialize-Blogger.ps1 @@ -6,14 +6,14 @@ This prepares your system to use Pandoc + Blogger together by obtaining an authtoken that is authorized to communicate with blogger. -.PARAMETER clientId +.PARAMETER ClientId Google API Client ID. This currently defaults to the one I use, but you will need to create your own until the Google Application is published and verified -.PARAMETER clientSecret +.PARAMETER ClientSecret Google API Client Secret. A default value is provided, but you can provide your own if you don't trust me. -.PARAMETER redirectUri +.PARAMETER RedirectUri The oAuth redirect URL specifed in the Google API Consent Form. .EXAMPLE @@ -23,18 +23,33 @@ Initiate a login flow with Google #> Function Initialize-Blogger { + [CmdletBinding()] Param( [Parameter(HelpMessage = "Google API ClientId")] - [string]$clientId = "<