feat(tools): forge.py login --token-stdin, a personal access token for a second machine #473
No reviewers
Labels
No labels
area/accessibility
area/audio
area/input
area/install
area/menu
area/renderer
area/saves
area/tooling
area/upscaling
beanstalk: approved
beanstalk: failed
beanstalk: needs approval
beanstalk: running
beanstalk: tested
ci: approved
good first issue
gpu/amd
gpu/igpu
gpu/intel
gpu/nvidia
help wanted
kind
bug
kind
chore
kind
docs
kind
feature
kind
performance
kind
question
needs measurements
platform/linux
platform/macos
platform/steam-deck
platform/windows
preview
priority
critical
priority
high
priority
low
regression
risk
high
risk
low
risk
medium
status
blocked
status
confirmed
status
duplicate
status
invalid
status
needs-info
status
wontfix
ci: approved
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!473
Loading…
Reference in a new issue
No description provided.
Delete branch "mooki/Longshot:feat/forge-token-login"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Type: feat · Risk: low · Tested on: macOS 26.7.1, Apple M4 Max (game-free checks against a stand-in Forgejo, no game run) · Needs: Windows and Linux, no GPU needed (a real token on git.magicbean.dev)
Why
Signing in with
python -I scripts/forge.py loginon a second machine signs you out on the first within about anhour, and signing in again there does the same to the second. Someone who works on two machines can't stay signed in
on both. Fixes #435.
Forgejo keeps one OAuth grant per user per application, and every machine signs in through the same app
(
GIT_CLIENT_IDinscripts/magicbean_login.py). In Forgejo'srouters/web/auth/oauth.go(v17.0.1, the versiongit.magicbean.dev reports):
app.GetGrantByUserID, line 617);InvalidateRefreshTokensis on(lines 161-162);
unauthorized_client, "token was already used" (lines827-831).
git.magicbean.dev has
InvalidateRefreshTokenson. So when machine A signs in or renews, machine B's refresh token goesstale; B works until its access token runs out (about an hour), then its renewal fails:
The
_SessionLockfixed this race between two processes on one machine; it can't help across machines.What changed
forge.py login --token-stdinsigns in with a Forgejo personal access token instead of Discord. The token has norefresh token and is never renewed, so it leaves the one OAuth grant alone and no machine signs another out; when
Forgejo refuses it, forge.py says to make a new one. Plain
login, the Discord sign-in and its renewal are unchanged.token_sign_in,forge_sessionandforge_expireinmagicbean_login.py, thencmd_loginandapiinforge.py.python -I scripts/forge.py login --token-stdin(cmd_loginandread_tokeninscripts/forge.py,token_sign_ininscripts/magicbean_login.py).getpass, so a typed or pasted token isn't shown.An empty line, or one with spaces or control characters in it, is refused.
GET /api/v1/user. A 401 says "token refused" and a 403 names the missing scope. Thetoken is never printed.
write_private, under the session lock, asforge.json:{"kind": "token", "login": ..., "access_token": ..., "expires_at": 4102444800}. There is no refresh token, soit never touches the OAuth grant. The far
expires_at(2100-01-01) is for an older forge.py, which knows no kindand reads it before renewing: such a copy uses the token as an access token not yet due (checked against main's
forge_session). This forge.py never renews a token, whatever the field says.forgeremote with the same code the Discord sign-in runs.write:repository,write:issueandread:user(below).forge_sessionreturns a saved token as it is, never renewing it (magicbean_login.py).forge_expiremarks a token"refused": trueinstead of zeroing its expiry. It runs on git'scredential erase,and now also when one of forge.py's API calls gets a 401 with a token (
apiinforge.py).login --token-stdin. It never says "could not be renewed".LoginErrorcarries the HTTP status (status), which those two checks read.whoamiadds "(personal access token)" for a token.logoutworks for both kinds; for a token it adds thatthe token stays valid on the site until deleted. Plain
loginkeeps a saved token and says so.--againstillsigns in with Discord, replacing it.
matrix_from_forge, when the sign-in service refuses a token, says to useteam_chat.py login --access-token-stdin, and that plainteam_chat.py login(the Discord sign-in) would replacethe token. forge.py's and team_chat.py's help say the same.
docs/forge.mdhas a new "Two machines" section (the one-grant behaviour, the token route, the scopes, theteam chat) and three new lines in "If something goes wrong". CONTRIBUTING.md's "Sign in" step points to it.
tests/test_forge_token_login.py(new). It runs forge.py against a stand-in Forgejo on 127.0.0.1 and runsgit in a sandbox (a temporary HOME, XDG_CONFIG_HOME and GIT_CONFIG_GLOBAL, no system config). It checks:
git credential fill;and git untouched;
/_magicbean/matrix-logintake a personal access token? Its code isn't here.GET /api/v1/userwith the token, a token withread:userworks./login/oauth/userinfoor/login/oauth/introspect, Forgejo v17.0.1 takes only its own OAuth tokensthere, so it would refuse one. userinfo runs only the OAuth2 method (
routers/web/web.go:431-439), which parsesthe token as a Forgejo-signed JWT (
services/auth/method/oauth2.go:84); introspect parses it the same way(
routers/web/auth/oauth.go:401).team_chat.py login --access-token-stdineither way.team_chat.py loginagainst replacing a saved token. The help and the docs say notto run it on a token machine; refusing would change the chat's sign-in for everyone.
[oauth2] INVALIDATE_REFRESH_TOKENS(it weakensrefresh-token reuse detection for everyone), or register a second OAuth application.
The scopes, from each API call forge.py and
test_requests.pymake (Forgejo v17.0.1routers/api/v1/api.go;writeincludesread):read:userGET /user: the check at sign-in, andfork,pr create,pr list --mine(the/usergroup, lines 689-798)write:repositorygit pushto your fork (routers/web/repo/githttp.go:156checks the repository scope at the push's level);fork(POST /repos/.../forks);pr create,ready,wip,edit,update(POST/PATCH .../pulls);pr review(POST .../pulls/N/reviews); reads of pulls,.diff, branches,git/commits, your fork andcollaborators/.../permission(test_requests.py) (the/repos/{owner}/{repo}group, lines 817-1131)write:issueissue create,issue comment,pr comment,pr update(POST .../issues[/N/comments]);--attach(PATCH .../issues/comments/N,POST .../assets);pr wip --needs-measurements,pr ready(POST/DELETE .../issues/N/labels); reads of issues, comments and labels (the issues and labels group, lines 1143-1256)Measurements
Verdict: works, game-free: the new test fails on main (8 of 9:
--token-stdinunknown) and passes here (9 of 9),2 runs each before the rebase and 1 after; the suite adds no failure. A tooling change: its test output is the
measurement (CONTRIBUTING's "build, tools" row).
Commits: main
6c9edffe5d(2026-11-14); this PR feat/forge-token-login @f7b6a93d4e.Hardware: Apple M4 Max, macOS 26.7.1, git 2.54.0 (Apple Git-157), Python 3.14.10. Game-free: no GPU, no game
Scene: none (a tooling change: CONTRIBUTING's "build, tools" row takes test output)
Method:
tests/test_forge_token_login.pyagainst main'sscripts/and against this PR. Before the rebase (main5e072fc8, this PRa65a4f1e) interleaved: main, this PR, main, this PR; after it once each, on 6c9edffe'sscripts/andat
f7b6a93d. The full game-free suite on this PR, compared with main's baseline run on the same MacHow to reproduce:
python -I -B tests/test_forge_token_login.py -v(for main's side: copy it into a checkout of main'stests/and run it there)6c9edffef7b6a93dtests/test_forge_token_login.py(2 runs each before the rebase, 1 after)--token-stdinunknown); the Discord-path test passestests/run_all.py --jobs 6)Screenshots: none; nothing on screen.
Flags and settings
One new command-line option,
forge.py login --token-stdin; no switch, menu row or ini key.forge.py login --token-stdinloginis unchanged; used only when givenTesting
Tested:
test's stand-in on 127.0.0.1, and the tokens were made up.
tests/test_forge_token_login.py: 8 of its 9 tests fail on main, and all 9 pass here. The Discord-path test passeson both.
tests/test_forge_pr.py,tests/test_forge_session_lock.py,tests/test_team_chat.pyandtests/test_check_pr_description.pypass,test_forge_prandtest_check_pr_descriptionas #571 rewrote them.f7b6a93d, compared with main 6c9edffe's by test name.test_forge_token_login, absenton main, passes here.
requests, some are checks main added or changed since
5e072fc8):test_appimage_package_retry,test_appimage_player_build,test_collect_wide,test_control,test_cpm_discovery,test_crash_dialog,test_crash_record,test_env_on,test_game_layout,test_lift_cleanup_error,test_linux_preflight,test_longshot_ctl,test_low_memory_notice,test_make_game_cache,test_mem_gate,test_mem_gate_children,test_mirror_trim,test_oot_state_tap,test_pcache_header_read,test_pipeline_cache_file,test_pipeline_cache_save,test_pipeline_play_cache,test_pipeline_tour,test_prof_symbols,test_program_cache_cleanup,test_program_cache_one_variant,test_readback_late,test_report_run,test_split_giants,test_vk_pgo,test_vram_budget_row,test_walk_key.run: fault-arm64-2026-11-14-180704.ips, recorder_sync_test.exe-2026-11-14-180744.ips).
read_tokenprompts "Personal access token forgit.magicbean.dev (not shown):", reads the line, and the typed text does not come back in the output.
forge.py login --helpshows--token-stdinwith the token page and the scopes.aa95d35f, at8e04fc23(f7b6a93dbefore the rebases ontoe090cc48andaa95d35f):Sweep3's checks of this head (the C/C++ it changes compiled, and its own tests run, on arm64 and x86_64 where they
apply) gave the same results as before;
python -I -B tests/run_all.pyon wave S1's merge11a5b4e7, which carriesthis head: 568/22/17 (passed/failed/skipped) against main aa95d35f's 543/33/17 on this Mac, no new failure
(
reports/2026-11-15-rebase-sweep-3.md).Not tested:
sign-in on the Mac (this PR's second machine), with
whoami, a fetch and a push to his fork; its result goes in acomment.
/_magicbean/matrix-login) takes a personal access token: its code isn't inthis repository (the open question under What changed). Either way the chat is reached: the error names
--access-token-stdin.getpassreads the console on Windows and theterminal on Linux, and the sign-in is saved in each OS's config folder.
Must be tested:
8e04fc23), runpython -I -B tests/test_forge_token_login.py -v; make a personal access token atgit.magicbean.dev/user/settings/applications with write:repository, write:issue and read:user, run
python -I scripts/forge.py login --token-stdinand type or paste it, thenwhoami,git fetch forgeand a pushto your fork; on Linux also pipe it in (
... --token-stdin < file); expecting the test to pass 9 of 9 (main'sforge.py has no
--token-stdin), the token not shown as it is typed or pasted,whoaminaming you with"(personal access token)", and the fetch and the push signing in through it with no prompt;
forge.py login --againsigns back in with Discord afterwards (by hand: test request T-KP6C)
Risks
What could break: low. Plain
loginand--again, the OAuth renewal,forge_expirefor an OAuth sign-in, and thegit setup are unchanged; the test's Discord-path part passes on main and here.
forge.json, which only you can read, as the OAuth sign-in already is. It staysvalid until it is deleted on the site or expires;
logoutsays so.loginprints the ones it needs, and a missing one shows asForgejo's 403 on the command that needs it.
6c9edffe(2026-11-14) without a conflict. Main's #571 (pull request descriptions) hadchanged
scripts/forge.py,docs/forge.mdandCONTRIBUTING.mdin other places, and #543CONTRIBUTING.mdtoo;this PR's hunks applied as they were. No conflict with the author's other open PRs as rebased on
6c9edffe(
git merge-treeof the heads, 2026-11-14).Turning it off: don't pass
--token-stdin(plainloginis unchanged), or revert the commits. A machine signed inwith a token then needs
forge.py loginagain.Compatibility: the builder's tools:
forge.jsoncan now hold a token sign-in ("kind": "token"), with a farexpires_atso that an older forge.py uses it as it is. Nothing changes for an OAuth sign-in, saves, the options file,the caches or the vk ABI.
Issues
Fixes #435
Checklist
Everyone
type(scope): description) of the status line's type.main: onaa95d35f(#761, 2026-11-15), head8e04fc23, without a conflict; this PR's owncommits, old → new:
294aed28→eba8f97c,a71f15ae→93cc757a,f7b6a93d→8e04fc23; the commit nameselsewhere in this description are from before the rebase.
python -I -B tests/run_all.pypasses (any failure that also fails onmainis named). On macOS: 532 passed, 17skipped, and 32 failures that also fail on main
6c9edffeon this Mac, named under Testing. After the rebase onto mainaa95d35f(head8e04fc23): wave S1's merge11a5b4e7, 568/22/17 against main aa95d35f's 543/33/17, no new failure.third_party/untouched (a new folder with aPROVENANCE.mdinstead).When it applies
Assisted-by:/Co-Authored-By:trailers.mainand passing here.Forgejo keeps one OAuth grant per person and application, and every token it issues, at a sign-in or a renewal, moves the grant's counter on; a refresh token from an older count is refused as "token was already used" (routers/web/auth/oauth.go 161-162, 617, 827-831 in Forgejo v17.0.1). Every machine signs in through the same app (GIT_CLIENT_ID), so two machines on one account sign each other out within about an hour. login --token-stdin reads a Forgejo personal access token from standard input (not echoed on a terminal), checks it with GET /api/v1/user ("token refused" on a 401, the missing scope on a 403, never printing it) and saves it with write_private as forge.json {"kind": "token", "login", "access_token"}: no refresh token, no expiry. forge_session returns it as it is; forge_expire (git's erase, or a 401 from forge.py's API calls) marks it refused, and the next use says to make a new one and sign in again instead of "could not be renewed". The git credential helper and the forge remote are set up as the Discord sign-in sets them up. whoami names the kind, logout says the token itself stays valid on the site, plain login keeps a saved token and --again goes back to Discord. The scopes forge.py's calls need are printed: write:repository, write:issue, read:user. The team chat's sign-in service is not in this repository, so whether it takes a personal access token can't be read from the code; when it refuses one, the error, forge.py's help, team_chat.py's help and docs/forge.md point to team_chat.py login --access-token-stdin, and say that plain team_chat.py login (the Discord sign-in) would replace the token. docs/forge.md has a "Two machines" section; CONTRIBUTING.md's sign-in step points to it. tests/test_forge_token_login.py (new) runs forge.py against a stand-in Forgejo on 127.0.0.1, with git sandboxed in a temporary HOME and GIT_CONFIG_GLOBAL. It fails on main; its Discord-path test passes on both. Fixes #435 Assisted-by: Claude Opus 5.5 Co-Authored-By: Claude Opus 5.5 <[email protected]>cf6fa5e943f7b6a93d4eWIP: feat(tools): forge.py login --token-stdin, a personal access token for a second machineto feat(tools): forge.py login --token-stdin, a personal access token for a second machinef7b6a93d4e4b1dc3c4534b1dc3c4538e04fc239bThanks for this, and for tracking the cause down in #435. We took the server-side route you suggested there: git.magicbean.dev has
[oauth2] INVALIDATE_REFRESH_TOKENS = falsesince 2026-11-15, so each machine keeps its own Discord sign-in and a renewal on one no longer signs the others out (checked: two copies of one sign-in both renewed from the same refresh token). That makes a personal-access-token sign-in unneeded for a second machine, so I am closing this. If a sign-in without Discord turns out to be needed (a headless box, a CI machine), reopen it.Pull request closed