& { #requires -Version 7.0 $ErrorActionPreference = 'Stop' $PSNativeCommandUseErrorActionPreference = $false $manifest = @' { "dependsOn": [ "Cuemon.AspNetCore.Authentication@11.0.0", "Cuemon.Core@11.0.0" ], "framework": "net10.0", "mode": "console", "schemaVersion": 1, "source": "// Program.cs\nusing System;\nusing System.Security.Claims;\nusing Cuemon;\nusing Cuemon.AspNetCore.Authentication;\nusing Microsoft.AspNetCore.Http;\nusing Microsoft.Net.Http.Headers;\n\nvar context = new DefaultHttpContext()\n{\n Request = { IsHttps = true }\n};\ncontext.Request.Headers.Append(HeaderNames.Authorization, \"Basic YWxpY2U6cGFzc3dvcmQ=\");\n\nvar result = Authenticator.Authenticate(context, false,\n (HttpContext _, string authorizationHeader) => authorizationHeader,\n (HttpContext _, string credentials, out ConditionalValue principal) =>\n {\n principal = new SuccessfulValue(\n new ClaimsPrincipal(new ClaimsIdentity(\n new[] { new Claim(ClaimTypes.Name, \"alice\") }, \"Basic\")));\n return true;\n });\n\nConsole.WriteLine(\"Succeeded: \" + result.Succeeded);\nConsole.WriteLine(\"User: \" + result.Result?.Identity?.Name);" } '@ $OutputEncoding = [Text.UTF8Encoding]::new($false) [Console]::OutputEncoding = $OutputEncoding $verifyTests = $false function Assert-CsrunTestOutput([string] $Output) { $lines = @($Output -split '\r?\n') $headers = @($lines | Where-Object { $_ -match '^Test run summary: Passed! - .+\)$' }) if ($headers.Count -ne 1) { throw 'Expected exactly one completed xUnit test summary.' } $headerIndex = [Array]::IndexOf($lines, $headers[0]) $summaryLines = @($lines | Select-Object -Skip ($headerIndex + 1) -First 4) $counts = @{} foreach ($line in $summaryLines) { if ($line -match '^\s*(total|failed|succeeded|skipped):\s*(\d+)\s*$') { $counts[$Matches[1]] = [int]$Matches[2] } } if ($counts.Count -ne 4) { throw 'The xUnit test summary counts are incomplete or ambiguous.' } $failed = $counts.failed $passed = $counts.succeeded $skipped = $counts.skipped $total = $counts.total if ($total -lt 1 -or $passed -lt 1 -or $failed -ne 0 -or $skipped -ne 0 -or $passed -ne $total) { throw 'Tests must execute at least once, all pass, and none fail or skip.' } } # Downloads and executes the latest stable csrun release with your permissions. # Source and release history: https://github.com/codebeltnet/csrun $engineDirectory = Join-Path ([IO.Path]::GetTempPath()) ('csrun-' + [guid]::NewGuid().ToString('N')) $null = New-Item -ItemType Directory -Path $engineDirectory $enginePath = Join-Path $engineDirectory 'csrun.ps1' $process = $null try { $release = Invoke-RestMethod 'https://api.github.com/repos/codebeltnet/csrun/releases/latest' $assets = @($release.assets | Where-Object name -CEQ 'csrun.ps1') if ($assets.Count -ne 1 -or $assets[0].digest -notmatch '^sha256:[0-9a-f]{64}$') { throw "csrun release $($release.tag_name) must publish one csrun.ps1 asset with a SHA-256 digest." } $asset = $assets[0] Invoke-WebRequest $asset.browser_download_url -OutFile $enginePath $actualHash = (Get-FileHash -LiteralPath $enginePath -Algorithm SHA256).Hash if ("sha256:$actualHash" -ne $asset.digest) { throw "csrun asset checksum mismatch: $actualHash" } $startInfo = [Diagnostics.ProcessStartInfo]::new('pwsh') foreach ($argument in @('-NoProfile','-NonInteractive','-File')) { $startInfo.ArgumentList.Add($argument) } $startInfo.ArgumentList.Add($enginePath) $startInfo.ArgumentList.Add('-ManifestJson') $startInfo.ArgumentList.Add($manifest) $startInfo.RedirectStandardOutput = $true $startInfo.RedirectStandardError = $true $startInfo.UseShellExecute = $false $process = [Diagnostics.Process]::new() $process.StartInfo = $startInfo if (-not $process.Start()) { throw 'Could not start csrun.' } $stdoutTask = $process.StandardOutput.ReadToEndAsync() $stderrTask = $process.StandardError.ReadToEndAsync() $process.WaitForExit() $output = $stdoutTask.GetAwaiter().GetResult() $diagnostics = $stderrTask.GetAwaiter().GetResult() Write-Output $output if ($diagnostics) { [Console]::Error.Write($diagnostics) } } catch { [Console]::Error.WriteLine("csrun diagnostics retained in: $engineDirectory") throw } finally { $resolvedTemp = [IO.Path]::TrimEndingDirectorySeparator([IO.Path]::GetFullPath([IO.Path]::GetTempPath())) $resolvedEngineDirectory = [IO.Path]::GetFullPath($engineDirectory) if ([IO.Path]::GetDirectoryName($resolvedEngineDirectory) -ne $resolvedTemp -or [IO.Path]::GetFileName($resolvedEngineDirectory) -cnotmatch '^csrun-[a-f0-9]{32}$') { throw 'Unsafe cleanup path.' } if ($process -and $process.ExitCode -eq 0) { Remove-Item -LiteralPath $resolvedEngineDirectory -Recurse -Force } } if ($process.ExitCode -ne 0) { throw "csrun execution failed with exit code $($process.ExitCode). $diagnostics" } if ($verifyTests) { Assert-CsrunTestOutput $output } }