Skip to content

feat(cli): use the credential site login saved for every site command - #28

Merged
tonycoder-hub merged 1 commit into
mainfrom
feat/saved-site-credentials
Oct 16, 2026
Merged

tonycoder-hub merged 1 commit into
mainfrom
feat/saved-site-credentials

Conversation

@tonycoder-hub

Copy link
Copy Markdown
Owner

Summary

After ptctl site login SITE, every site command for that site now works without a credential flag. Signing in is meant to be done once, not repeated as --cookie-stdin / --api-key-stdin on every call.

  • site login tjupt already saved the session cookie it issues; commands now read it back implicitly.
  • site login mteam now asks for the API key with echo off and saves the API key once one status read accepts it. Before this change, M-Team had no way to save a credential.
  • site login --cookie-stdin SITE / site login --api-key-stdin SITE save a piped credential the same way, for agents. Nothing is saved, and an earlier file is kept, if the check fails.
  • The saved credential is used by status, account, search, detail, bonus catalog and review, site metafile fetch, and site bonus exchange submit.
  • An explicit stdin flag always takes precedence.
  • New ptctl site logout SITE deletes the saved files. It sends nothing to the site, so the session or key stays valid there until it expires or is revoked.

Security model change (deliberate)

This change deliberately adds credential persistence and implicit read-back, which the project previously avoided:

  • Storage. The file is ptctl/sessions/SITE.cookie or SITE.api-key under os.UserConfigDir(). It is plaintext, mode 0600 in a 0700 directory, and written through an owner-only temp file plus rename.
    • Anyone who can read it as the user can act as the user on that site.
    • An M-Team API key does not expire on its own.
    • An OS keyring or credential helper stays listed as future work.
  • Read checks. Reads refuse, without disclosing the content:
    • a symlink or non-regular file;
    • group/other permission bits (outside Windows);
    • a file that changes between open and stat;
    • content that is not exactly one valid credential line, after stripping one final \n or \r\n.
  • Check order. The file is located after every other usage check and read at the point stdin used to be read, so the preflight order is unchanged.
    • A missing file is a usage error (exit 2) and contacts no site.
    • A refused file is exit 1.
  • Acknowledgements. A saved credential never stands in for an acknowledgement flag.
  • Passwords. Account and downloader passwords are never saved.
    • With the saved cookie, bonus exchange submit on a password-confirmed site (TJUPT) asks for the account password with echo off, before the 45 s timeout starts, so typing it does not use up the review/submit time.
    • Without a terminal, it exits 2 and names --credential-bundle-stdin.
  • Tests. TestMain isolates HOME, XDG_CONFIG_HOME and AppData, so tests never read real saved credentials. Fixtures are synthetic canaries.

Public text changes (deliberate)

  • Usage error. --cookie-stdin is required; credentials are never accepted in argv (and the API-key variant) is replaced by no saved session cookie|API key for site "SITE"; run 'ptctl site login SITE' first, or pipe one with --cookie-stdin|--api-key-stdin.
  • Usage lines. Site command usage lines show the credential flag as optional ([--cookie-stdin | --api-key-stdin]).
  • Login help. It is rewritten for the saved credential and the new save mode, and site logout has its own help.
  • Login success hint. For the default destination it now says site commands use the saved file when no stdin flag is given.
  • API-key save output. The text output is new. JSON is site.login with exactly site_id, username, api_key_file, requests_attempted.
  • TJUPT password-login JSON. It is byte-identical (covered by the existing TestSiteLoginReportsJSONWithoutTheCookie).
  • Exchange submit errors. A missing ack is now reported on its own: site bonus exchange submit requires --acknowledge-bonus-exchange.
  • Redactor-safe rewording. Messages that named a cookie or API-key path followed by : were partly swallowed by security.Redact (for example refusing to replace … with a session cookie: [REDACTED]). They now use … because it is not a regular file, including the pre-existing --cookie-file message.

Unchanged:

  • JSON fields;
  • stop reasons and their order;
  • exit codes;
  • site/route IDs and origins.

Out of scope

  • reconcile (--site-cookie-stdin, --site-ref) still takes its cookie from stdin only.
  • site login --credential-bundle-stdin without --cookie-file still writes the cookie to stdout and does not save it, for compatibility with existing agent pipelines.

Testing

  • gofmt, go vet ./..., go test -race ./..., go build -trimpath ./cmd/ptctl.
  • New internal/cli/saved_credentials_test.go:
    • every site command reads the saved credential without stdin;
    • the explicit flag wins;
    • unsafe or invalid files are refused without disclosure;
    • login saves only after an accepted check;
    • save-mode usage errors never read stdin;
    • logout semantics;
    • bonus exchange submit with the saved cookie and a terminal password.
  • Live check against M-Team in a throwaway HOME, connectivity only, with no download and no genDlToken:
    • site login --api-key-stdin mteam saved the key with one request (file 0600, directory 0700);
    • a later plain site status mteam authenticated.
    • The temp dir was deleted.

🤖 Generated with Claude Code

After `ptctl site login SITE`, every site command for that site works
without a credential flag. `site login` keeps the TJUPT session cookie
it issues, and now also saves an M-Team API key (typed hidden, or piped
with --api-key-stdin) or a browser cookie (--cookie-stdin) once one
status read accepts it. Status, account, search, detail, bonus catalog
and review, metafile fetch, and bonus exchange submit read the saved
file when no stdin flag is given; an explicit flag still wins.

The saved file lives at ptctl/sessions/SITE.cookie or SITE.api-key
under the user config directory, 0600 in a 0700 directory. It is
refused without disclosure when it is a symlink or not a regular file,
grants group or other access (outside Windows), changes while opened,
or is not exactly one valid credential line. It is located after every
other usage check and read where stdin would have been read, so the
preflight order is unchanged. A saved credential never replaces an
acknowledgement flag, and account and downloader passwords are never
saved. `ptctl site logout SITE` deletes the saved files.

Bonus exchange submit on a password-confirmed site (TJUPT) with the
saved cookie asks for the account password with echo off before its
timeout starts. Error messages that named a cookie or API-key path are
reworded so the redactor no longer swallows their tail.

Tests isolate HOME, XDG_CONFIG_HOME, and AppData so they never read
real saved credentials; fixtures are synthetic canaries.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@tonycoder-hub
tonycoder-hub merged commit e1d5e29 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