10 cards
Recommended loopback redirect URI
`http://127.0.0.1:{port}/{path}` (IPv4) or `http://[::1]:{port}/{path}` (IPv6).
Why avoid `localhost`
RFC 8252 marks using localhost as NOT RECOMMENDED because it may resolve incorrectly or accidentally listen on another network interface.
Random port
The CLI opens a free port assigned by the OS; the authorization server must accept any port for loopback redirects.
PKCE S256
`code_challenge = BASE64URL(SHA256(code_verifier))`, prevents another app on the machine from stealing the authorization code.
state
A random value the CLI generates and checks at the callback to prevent CSRF.
After receiving the code
The CLI shuts down the loopback server immediately, returns a "You can close this tab" page, then exchanges the code for a token.
Opening the browser automatically
Print the URL first, then call `open` (macOS), `xdg-open` (Linux), `rundll32 url.dll,FileProtocolHandler` (Windows; do not use `cmd /c start` because cmd treats `&` as a command separator); catch errors via `child.on('error')`.
When to switch to device flow
No browser, running over SSH, running in a container, or the loopback port is blocked.
Loopback vs device flow
Loopback is fast and needs no code typing but requires a browser on the same machine; device flow works anywhere but is easier to abuse for phishing.
AWS CLI v2 example
`aws sso login` uses Authorization Code + loopback by default; add `--use-device-code` to use device flow.