88using OpenClawTray . Services ;
99using System ;
1010using System . Collections . Generic ;
11+ using System . Globalization ;
1112using System . IO ;
1213using System . Runtime . InteropServices ;
1314using System . Runtime . InteropServices . WindowsRuntime ;
15+ using System . Text . Json ;
1416using System . Threading . Tasks ;
1517using Windows . ApplicationModel . DataTransfer ;
1618using 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
0 commit comments