Skip to content

Make site login prompt on the terminal by default - #24

Merged
tonycoder-hub merged 1 commit into
mainfrom
feat/interactive-login
Oct 16, 2026
Merged

tonycoder-hub merged 1 commit into
mainfrom
feat/interactive-login

Conversation

@tonycoder-hub

Copy link
Copy Markdown
Owner

Summary

ptctl site login SITE is now interactive by default: it prompts for the username, then the password and optional two-step code with echo off, and saves the new session cookie to an owner-only file. The flags scripts and agents need are documented in pt help login / site login --help instead of being required on every run.

$ ptctl site login tjupt
Signing in to TJUPT / 北洋园PT with one login request; failed logins count toward the site's lockout.
Username: alice
Password (hidden):
Two-step code (hidden; press Enter if none):
site login succeeded: site=tjupt username=alice session_expires=2026-10-23T08:00:00Z requests=2
session cookie saved to /home/alice/.config/ptctl/sessions/tjupt.cookie (owner-only)
use it with: ptctl site status --cookie-stdin tjupt < '/home/alice/.config/ptctl/sessions/tjupt.cookie'

Deliberate behavior changes

  • Interactive default. Without --credential-bundle-stdin, site login prompts on the terminal. This is the one command that accepts a TTY for secrets; with the bundle, a terminal on stdin is still refused. A non-terminal stdin without the bundle is a usage error (exit 2) pointing at --credential-bundle-stdin. This replaces the former "bundle required" usage error.
  • Saved cookie by default. The interactive mode saves the cookie to <user config dir>/ptctl/sessions/SITE.cookie (directory 0700, file 0600, atomic replace). No command reads it back automatically. help config and the safety defaults now mention this exception.
  • New flags.
    • --cookie-file PATH saves the cookie to an explicit owner-only file in both modes.
    • --output table|json adds a new JSON kind, site.login, with site_id, username, session_days, session_expires_at, cookie_file and requests_attempted. It never contains the cookie.
    • --output json with the bundle requires --cookie-file, because stdout otherwise carries the cookie.
  • Legacy pipe mode is unchanged. --credential-bundle-stdin without --cookie-file still writes only the cookie line to a pipe or owner-only stdout. Its two refusal messages now end with , or use --cookie-file.
  • Help. site login --help prints the full help and exits 0. pt help login (or pt help site login) is a new topic, and the unknown-topic list includes login.
  • Cookie file safety. The cookie file is created as an owner-only temp file before the login request, so an unwritable destination never costs a login attempt. It is renamed into place only after the verifying read. A failed login leaves an earlier cookie file untouched. Directories, symlinks and other non-regular targets are refused up front.

Echo handling

Echo is turned off before each hidden prompt is printed, the same way sudo does it, so nothing typed after the prompt appears is echoed. An early prototype printed the prompt first and then disabled echo, and a pty test that answered immediately saw the password echoed.

  • Echo control uses golang.org/x/sys directly: termios on Unix (TIOCGETA/TIOCSETA on BSD/darwin, TCGETS/TCSETS elsewhere) and console mode on Windows. No new module is added.
  • Canonical line editing and signals stay on.
  • Ctrl+C restores the terminal and exits 130.
  • Platforms without terminal support fall back to requiring --credential-bundle-stdin.

Verification

  • gofmt, go vet ./... (also with GOOS=windows and GOOS=linux), go test -race ./... and go build -trimpath ./cmd/ptctl pass. The terminal files were also compiled alone for freebsd, openbsd, netbsd, dragonfly, solaris, illumos, aix, android, plan9, js and wasip1.
  • Unit tests use a fake terminal. They cover:
    • prompt order and echo state;
    • default-path saving with 0700/0600 modes;
    • exact JSON output;
    • usage errors that never prompt;
    • failures that leave no cookie or temp file;
    • help text covering every flag.
  • Offline pty run (expect, answers sent with no delay):
    • Ctrl+C at either hidden prompt exits 130 with echo restored.
    • EOF at the username prompt cancels before anything is sent.
    • A malformed code is rejected before any request.
    • Neither hidden answer appears in the pty output, and no cookie file is created.
  • Live TJUPT run from a pty, with the code from a password manager:
    • exit 0, requests=2;
    • neither hidden answer in the output;
    • a 0700 directory and 0600 cookie file with no leftover temp file;
    • site status --cookie-stdin tjupt < file reported authenticated for the same account.

🤖 Generated with Claude Code

`ptctl site login SITE` now asks for the username, then the password and
the optional two-step code with echo off, and saves the new session cookie
to an owner-only file under the user config directory
(<config dir>/ptctl/sessions/SITE.cookie), printing its path and a
follow-up command. Echo is turned off before each hidden prompt is shown,
so nothing typed after the prompt appears is echoed, and Ctrl+C restores
echo before exiting 130. Echo control uses golang.org/x/sys directly
(termios on Unix, console mode on Windows); no new module is added.

Scripts and agents keep --credential-bundle-stdin and gain --cookie-file
and --output json (kind site.login, which never contains the cookie). The
cookie file is created as an owner-only temporary file before the login
request, so an unwritable destination never costs a login attempt, and is
renamed into place only after the verifying read. `pt help login` and
`site login --help` document every flag, the exit statuses, and the stop
reasons.

Verified on TJUPT from a pseudo-terminal: one login with a two-step code
from a password manager, neither hidden answer in the terminal output, a
0700 directory and 0600 cookie file, and `site status` accepting it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@tonycoder-hub
tonycoder-hub merged commit 032067c into main Oct 16, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant