Skip to content

Commit ed218e9

Browse files
shanselmanCopilot
andcommitted
feat: detect expired setup codes
Co-authored-by: Copilot <[email protected]>
1 parent 9e319da commit ed218e9

3 files changed

Lines changed: 82 additions & 1 deletion

File tree

‎docs/gateway-node-integration.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ After changing either `gateway.nodes.allowCommands` or `gateway.nodes.denyComman
379379
- [x] Handle `hello-ok.auth.deviceToken` — save it for future connections
380380
- [x] Accept QR images and clipboard setup content as alternate ways to enter the same bootstrap payload
381381
- [x] Show "auto-paired!" vs "waiting for approval" based on auth method
382-
- [ ] Handle bootstrap token expiry gracefully (re-generate if expired)
382+
- [x] Handle bootstrap token expiry gracefully when setup code payloads include expiry metadata (`expiresAt`, `expires_at`, `expires`, `expiry`, or `exp`)
383383

384384
### 5.4 Upstream Contributions / Issues to File
385385

‎src/OpenClaw.Tray.WinUI/Windows/SetupWizardWindow.cs‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
using OpenClawTray.Services;
99
using System;
1010
using System.Collections.Generic;
11+
using System.Globalization;
1112
using System.IO;
1213
using System.Runtime.InteropServices;
1314
using System.Runtime.InteropServices.WindowsRuntime;
15+
using System.Text.Json;
1416
using System.Threading.Tasks;
1517
using Windows.ApplicationModel.DataTransfer;
1618
using Windows.Storage.Pickers;
@@ -455,6 +457,16 @@ private bool TryApplySetupCode(string code, string successMessage)
455457
UpdatePairingStatusText();
456458
}
457459

460+
if (TryGetSetupCodeExpiry(doc.RootElement, out var expiresAt) &&
461+
expiresAt <= DateTimeOffset.UtcNow)
462+
{
463+
_draftBootstrapToken = "";
464+
_connectionTested = false;
465+
_testStatusLabel.Text = "❌ Setup code expired. Generate a fresh QR/setup code from the gateway and try again.";
466+
Logger.Warn($"[Setup] Setup code expired at {expiresAt:O}");
467+
return false;
468+
}
469+
458470
if (string.IsNullOrWhiteSpace(_draftGatewayUrl) ||
459471
string.IsNullOrWhiteSpace(_draftBootstrapToken))
460472
{
@@ -478,6 +490,56 @@ private bool TryApplySetupCode(string code, string successMessage)
478490
}
479491
}
480492

493+
internal static bool TryGetSetupCodeExpiry(JsonElement payload, out DateTimeOffset expiresAt)
494+
{
495+
foreach (var propertyName in new[] { "expiresAt", "expires_at", "expires", "expiry", "exp" })
496+
{
497+
if (payload.TryGetProperty(propertyName, out var value) &&
498+
TryParseSetupCodeExpiryValue(value, out expiresAt))
499+
{
500+
return true;
501+
}
502+
}
503+
504+
expiresAt = default;
505+
return false;
506+
}
507+
508+
private static bool TryParseSetupCodeExpiryValue(JsonElement value, out DateTimeOffset expiresAt)
509+
{
510+
if (value.ValueKind == JsonValueKind.String)
511+
{
512+
var text = value.GetString();
513+
if (DateTimeOffset.TryParse(
514+
text,
515+
CultureInfo.InvariantCulture,
516+
DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal,
517+
out expiresAt))
518+
{
519+
return true;
520+
}
521+
522+
if (long.TryParse(text, NumberStyles.Integer, CultureInfo.InvariantCulture, out var unixFromString))
523+
{
524+
expiresAt = UnixTimeToDateTimeOffset(unixFromString);
525+
return true;
526+
}
527+
}
528+
else if (value.ValueKind == JsonValueKind.Number && value.TryGetInt64(out var unix))
529+
{
530+
expiresAt = UnixTimeToDateTimeOffset(unix);
531+
return true;
532+
}
533+
534+
expiresAt = default;
535+
return false;
536+
}
537+
538+
private static DateTimeOffset UnixTimeToDateTimeOffset(long value) =>
539+
value > 10_000_000_000
540+
? DateTimeOffset.FromUnixTimeMilliseconds(value)
541+
: DateTimeOffset.FromUnixTimeSeconds(value);
542+
481543
private async void OnPasteSetupFromClipboard(object sender, RoutedEventArgs e)
482544
{
483545
try

‎tests/OpenClaw.Tray.Tests/TrayMenuWindowMarkupTests.cs‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,25 @@ public void SetupWizard_HasPairingExpectationGuidance()
198198
Assert.Contains("Already paired", source);
199199
}
200200

201+
[Fact]
202+
public void SetupWizard_DetectsExpiredSetupCodes()
203+
{
204+
var sourcePath = Path.Combine(
205+
GetRepositoryRoot(),
206+
"src",
207+
"OpenClaw.Tray.WinUI",
208+
"Windows",
209+
"SetupWizardWindow.cs");
210+
211+
var source = File.ReadAllText(sourcePath);
212+
213+
Assert.Contains("TryGetSetupCodeExpiry", source);
214+
Assert.Contains("Setup code expired", source);
215+
Assert.Contains("expiresAt", source);
216+
Assert.Contains("expires_at", source);
217+
Assert.Contains("exp", source);
218+
}
219+
201220
private static string GetRepositoryRoot()
202221
{
203222
var directory = new DirectoryInfo(AppContext.BaseDirectory);

0 commit comments

Comments
 (0)