windows: user-token.ps1 validates keys via verae-print.exe (PS5.1 IWR timed out); mask short strings fully

This commit is contained in:
Grok Bot (for George) 2026-10-03 19:20:33 -04:00
commit 1adb28bf37
2 changed files with 23 additions and 4 deletions

View file

@ -34,7 +34,7 @@ function Get-VeraeTargetUser {
function Get-MaskedToken([string]$t) { function Get-MaskedToken([string]$t) {
if (-not $t) { return "" } if (-not $t) { return "" }
$t = $t.Trim() $t = $t.Trim()
if ($t.Length -le 12) { return ($t.Substring(0, [Math]::Min(4, $t.Length)) + "...") } if ($t.Length -le 12) { return "***" }
return ($t.Substring(0, 7) + "..." + $t.Substring($t.Length - 4)) return ($t.Substring(0, 7) + "..." + $t.Substring($t.Length - 4))
} }

View file

@ -13,8 +13,27 @@ $ErrorActionPreference = "Continue"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$ValidateUrl = "https://zap.verae.ai/v1/status" # documented as free; 401 = bad key $ValidateUrl = "https://zap.verae.ai/v1/status" # documented as free; 401 = bad key
function Test-VeraeKey([string]$tok) { # Prefer the Go validator (verae-print.exe validate-key): PowerShell 5.1's
# Invoke-WebRequest was seen timing out on nimo (proxy auto-detect) while Go and
# curl answered in <1 s. The key is read from a file and never on a command line.
function Find-VeraeExe {
foreach ($c in @((Join-Path $PSScriptRoot "verae-print.exe"), (Join-Path (Split-Path -Parent $PSScriptRoot) "bin\verae-print.exe"))) {
if (Test-Path $c) { return $c }
}
return $null
}
function Test-VeraeKey([string]$tok, [string]$file) {
if ($tok -notmatch '^(xVerae-|key-)\S{8,}$') { return @{ result = "badformat"; detail = "must start with xVerae- (or legacy key-)" } } if ($tok -notmatch '^(xVerae-|key-)\S{8,}$') { return @{ result = "badformat"; detail = "must start with xVerae- (or legacy key-)" } }
$exe = Find-VeraeExe
if ($exe -and $file -and (Test-Path $file)) {
$ini = Join-Path $env:TEMP ("verae-val-" + [guid]::NewGuid().ToString("N") + ".ini")
try {
& $exe validate-key --token-file $file --out $ini 2>&1 | Out-Null
$kv = @{}
if (Test-Path $ini) { Get-Content $ini | ForEach-Object { if ($_ -match '^([^=\[]+)=(.*)$') { $kv[$Matches[1]] = $Matches[2] } } }
if ($kv.result) { return @{ result = $kv.result; detail = $kv.detail } }
} finally { Remove-Item $ini -ErrorAction SilentlyContinue }
}
try { try {
$r = Invoke-WebRequest -UseBasicParsing -TimeoutSec 10 -Uri $ValidateUrl -Headers @{ "x-api-key" = $tok } $r = Invoke-WebRequest -UseBasicParsing -TimeoutSec 10 -Uri $ValidateUrl -Headers @{ "x-api-key" = $tok }
return @{ result = "valid"; detail = "HTTP $($r.StatusCode)" } return @{ result = "valid"; detail = "HTTP $($r.StatusCode)" }
@ -37,7 +56,7 @@ switch ($Mode) {
if (Test-Path $tokPath) { $existing = (Get-Content $tokPath -Raw -ErrorAction SilentlyContinue) } if (Test-Path $tokPath) { $existing = (Get-Content $tokPath -Raw -ErrorAction SilentlyContinue) }
if ($existing) { $existing = $existing.Trim() } if ($existing) { $existing = $existing.Trim() }
$v = @{ result = "none"; detail = "" } $v = @{ result = "none"; detail = "" }
if ($existing) { $v = Test-VeraeKey $existing } if ($existing) { $v = Test-VeraeKey $existing $tokPath }
Write-Host "existing key: $([bool]$existing) $(Get-MaskedToken $existing) check=$($v.result) $($v.detail)" Write-Host "existing key: $([bool]$existing) $(Get-MaskedToken $existing) check=$($v.result) $($v.detail)"
Write-VeraeIni $IniOut "token" ([ordered]@{ Write-VeraeIni $IniOut "token" ([ordered]@{
account = $u.Account; sid = $u.Sid; profile = $u.Profile; source = $u.Source; path = $tokPath account = $u.Account; sid = $u.Sid; profile = $u.Profile; source = $u.Source; path = $tokPath
@ -46,7 +65,7 @@ switch ($Mode) {
} }
"validate" { "validate" {
$tok = (Get-Content $TokenFile -Raw).Trim() $tok = (Get-Content $TokenFile -Raw).Trim()
$v = Test-VeraeKey $tok $v = Test-VeraeKey $tok $TokenFile
Write-Host "validate $(Get-MaskedToken $tok): $($v.result) $($v.detail)" Write-Host "validate $(Get-MaskedToken $tok): $($v.result) $($v.detail)"
Write-VeraeIni $IniOut "validate" ([ordered]@{ result = $v.result; detail = $v.detail; masked = (Get-MaskedToken $tok) }) Write-VeraeIni $IniOut "validate" ([ordered]@{ result = $v.result; detail = $v.detail; masked = (Get-MaskedToken $tok) })
} }