Repository navigation
Add an interactive login flow - #13
Merged
Merged
Conversation
`modelforgectl login` gets a person a credential, which is what was missing: the server could verify a JWT somebody already held, but nothing here helped them obtain one. This is the RFC 8252 native-application flow, and its shape follows from what a command-line tool can safely do. There is no client secret, because one shipped inside a binary is a string every user can read out of it; the client is public and PKCE takes the secret's place. The redirect goes to 127.0.0.1 rather than localhost, since localhost resolves through DNS and a hostile resolver delivers the authorization code to another machine. state is checked before the code is touched, or anybody who can make a browser load a URL could complete a login of their choosing and leave the victim holding their session. A nonce binds the token to this login so one captured elsewhere cannot be replayed into it. The callback page interpolates nothing, because reflecting a provider's error text would be cross-site scripting on a page that has just handled a code. The credential is stored 0600 in a 0700 directory, both set explicitly rather than left to the umask, and an existing directory is tightened rather than trusted since MkdirAll leaves one alone. MODELFORGE_TOKEN still wins, so a script can override whoever is signed in on the machine it runs on. --no-browser prints the URL and waits, for a headless box or anyone who would rather paste it into a browser they trust. Smoke-testing this found a real bug. Leaving Endpoint.AuthStyle unset makes x/oauth2 probe for the server's preferred client-authentication style by trying one and retrying with the other. An authorization code is single-use, so the probe burns it, and the error surfaced is the second attempt's invalid_grant — pointing at the code rather than at whatever actually went wrong first. Setting AuthStyleInParams is both correct, since RFC 6749 has a public client send its id in the body, and removes the failure mode: one token request per login where there had been two. logout deletes the local file and says plainly that this is not revocation. Coverage across internal/ is 90.9%.
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
modelforgectl logingets a person a credential. The server could already verify a JWT somebody held; nothing here helped them obtain one.RFC 8252, and why each piece is there
The shape follows from what a command-line tool can safely do:
state, checked before the code is touched. Without it, anybody who can make your browser load a URL completes a login of their choosing and you end up holding their session.noncebound to this login, so a token captured elsewhere cannot be replayed into it.127.0.0.1, notlocalhost.localhostresolves through DNS; a hostile resolver points it elsewhere and the authorization code lands on somebody else's machine. The literal address cannot be redirected.error_descriptionwould be XSS on a page that has just handled an authorization code — there's a test for it.Credential storage
~/.config/modelforge/credential, mode 0600 in a 0700 directory — set explicitly rather than left to the umask, since 022 leaves a readable credential in a home directory other accounts can list. An existing directory is tightened rather than trusted, becauseMkdirAllleaves an existing mode alone.MODELFORGE_TOKENstill wins, so a script can override whoever is signed in on the machine.logoutdeletes the file and says plainly that this is not revocation.A real bug the smoke test found
The first version left
Endpoint.AuthStyleunset.x/oauth2then probes for the server's preferred client-auth style: try one, retry with the other if rejected.An authorization code is single-use, so the probe burns it. The error you see is the second attempt's
invalid_grant, pointing at the code — when the real problem was whatever made the first request fail. I spent a while chasing a phantom double-exchange before instrumenting the provider and seeing twoTOKENrequests for one code.Setting
AuthStyleInParamsis both correct (RFC 6749: a public client sends its id in the body, having no secret for a Basic header) and removes the failure mode. Verified: one token request per login, where there had been two.Two things I got wrong while debugging this, worth noting since both looked like product bugs:
OpenBrowseron macOS launched my actual browser, which raced thecurlstanding in for it — hence--no-browser, which is a genuine feature for headless machines. And my standalone test provider omittedContent-Type: application/json, which is what made the first exchange fail in the first place.Also added
GET /v1/auth/config— where to sign in. Deliberately unauthenticated: a client cannot present a credential before it knows where to get one, and every value is visible to anybody watching a browser log in.GET /v1/auth/whoami— who the current credential belongs to. Answering "am I logged in, as whom, with what access" by decoding a JWT locally would report what the token claims rather than what the server accepts, which is the thing in doubt when a request is refused.--no-browser, for headless machines and remote shells.Verified live
Against the real binary and a standalone provider:
whoamibefore and after, the login completing, credential at-rw-------, an authenticated write using the stored login, the audit line naming the person,logoutclearing it, and exactly one token request at the provider.Coverage
90.9% across
internal/.OpenBrowserwas split so the platform dispatch is testable and only theexecis uncovered.