Skip to content

Add an interactive login flow - #13

Merged
sahilkalgutkar merged 1 commit into
mainfrom
feature/login-flow
Aug 31, 2026
Merged

sahilkalgutkar merged 1 commit into
mainfrom
feature/login-flow

Conversation

@sahilkalgutkar

Copy link
Copy Markdown
Owner

modelforgectl login gets a person a credential. The server could already verify a JWT somebody held; nothing here helped them obtain one.

$ modelforgectl login
to sign in, visit:

  https://idp.example.com/authorize?...

signed in as sahil@example.com (admin)
this session expires at 2026-08-31T20:34:32Z

RFC 8252, and why each piece is there

The shape follows from what a command-line tool can safely do:

  • No client secret. One shipped inside a binary is a string every user can read out of it. The client is public and PKCE replaces the secret. The test provider recomputes the challenge from the verifier and refuses a mismatch, so the CLI cannot ship without PKCE and still pass its tests.
  • 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.
  • nonce bound to this login, so a token captured elsewhere cannot be replayed into it.
  • Redirect to 127.0.0.1, not localhost. localhost resolves through DNS; a hostile resolver points it elsewhere and the authorization code lands on somebody else's machine. The literal address cannot be redirected.
  • The callback page interpolates nothing. Reflecting a provider's error_description would 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, because MkdirAll leaves an existing mode alone. MODELFORGE_TOKEN still wins, so a script can override whoever is signed in on the machine.

logout deletes the file and says plainly that this is not revocation.

A real bug the smoke test found

The first version left Endpoint.AuthStyle unset. x/oauth2 then 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 two TOKEN requests for one code.

Setting AuthStyleInParams is 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: OpenBrowser on macOS launched my actual browser, which raced the curl standing in for it — hence --no-browser, which is a genuine feature for headless machines. And my standalone test provider omitted Content-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: whoami before and after, the login completing, credential at -rw-------, an authenticated write using the stored login, the audit line naming the person, logout clearing it, and exactly one token request at the provider.

Coverage

90.9% across internal/. OpenBrowser was split so the platform dispatch is testable and only the exec is uncovered.

`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%.
@sahilkalgutkar
sahilkalgutkar merged commit b39a923 into main Aug 31, 2026
2 checks passed
@sahilkalgutkar
sahilkalgutkar deleted the feature/login-flow branch August 31, 2026 19:39
@codecov

codecov Bot commented Aug 31, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 75.55556% with 66 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
internal/cli/login.go 75.57% 31 Missing and 22 partials ⚠️
internal/cli/cli.go 50.00% 7 Missing ⚠️
internal/api/api.go 80.64% 4 Missing and 2 partials ⚠️

📢 Thoughts on this report? Let us know!

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