Skip to content

Commit 42ebd6d

Browse files
Fix: suppress TOTP wait message in JSON output mode
The generateTOTPCode function unconditionally printed a human-readable message via pterm.Info.Printf when waiting for a fresh TOTP window. This could corrupt JSONL output when --output json was specified in AgentAuthRunCmd.Run. Added a 'quiet' parameter to generateTOTPCode to suppress console output when in JSON mode, preventing non-JSON messages from corrupting the output stream for programmatic consumers. Co-authored-by: mason <mason@onkernel.com>
1 parent 8bbe44b commit 42ebd6d

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

cmd/agents.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -517,15 +517,18 @@ const (
517517

518518
// generateTOTPCode generates a TOTP code from a base32 secret.
519519
// Waits for a fresh window if needed to ensure enough time to submit the code.
520-
func generateTOTPCode(secret string) (string, error) {
520+
// If quiet is true, suppresses human-readable console output (for JSON mode).
521+
func generateTOTPCode(secret string, quiet bool) (string, error) {
521522
// Check if we have enough time in the current window
522523
now := time.Now().Unix()
523524
secondsIntoWindow := now % totpPeriod
524525
remaining := totpPeriod - secondsIntoWindow
525526

526527
if remaining < minSecondsRemaining {
527528
waitTime := remaining + 1 // Wait until just after the new window starts
528-
pterm.Info.Printf("TOTP window has only %ds remaining, waiting %ds for fresh window...\n", remaining, waitTime)
529+
if !quiet {
530+
pterm.Info.Printf("TOTP window has only %ds remaining, waiting %ds for fresh window...\n", remaining, waitTime)
531+
}
529532
time.Sleep(time.Duration(waitTime) * time.Second)
530533
}
531534

@@ -813,7 +816,7 @@ func (c AgentAuthRunCmd) Run(ctx context.Context, in AgentAuthRunInput) error {
813816
totpPatterns := []string{"totp", "code", "verification", "otp", "2fa", "mfa", "authenticator", "token"}
814817
for _, pattern := range totpPatterns {
815818
if strings.Contains(fieldLower, pattern) {
816-
code, err := generateTOTPCode(in.TotpSecret)
819+
code, err := generateTOTPCode(in.TotpSecret, jsonOutput)
817820
if err == nil {
818821
submitValues[fieldName] = code
819822
matched = true
@@ -889,7 +892,7 @@ func (c AgentAuthRunCmd) Run(ctx context.Context, in AgentAuthRunInput) error {
889892

890893
if hasTOTP && in.TotpSecret != "" {
891894
// Generate and submit TOTP code
892-
code, err := generateTOTPCode(in.TotpSecret)
895+
code, err := generateTOTPCode(in.TotpSecret, jsonOutput)
893896
if err != nil {
894897
return err
895898
}

0 commit comments

Comments
 (0)