Command-line tools
A command-line tool such as photos-cli is a public clientAn OAuth client, such as code running in a browser or an app on a phone, that cannot keep a credential secret because the people using it can inspect it. It relies on protections such as PKCE instead of a client secret. that may run beside a browser, on a remote server with none, or in a job with nobody present. Each case calls for a different way to get Ava's approval, and every case needs care with where the tool keeps her tokens.
Choosing how to sign in
The photo service publishes photos-cli, a command-line tool for people who would rather type than click. From a terminal Ava can list her albums, download an album to a folder, or upload a directory of photos. Developers also use it in scripts, such as a nightly job that backs up their albums.
Before it can do any of that, photos-cli needs a token, and a terminal is an awkward place to get one. The tool might be running on Ava's laptop next to a browser, on a remote server she is working on through a terminal sessionThe ongoing connection between later requests and an earlier sign-in, so you are not asked for your password on every click. It is often tracked with a cookie holding an unpredictable session identifier. with no browser at all, or inside an automated job where nobody is present. Each situation points to a different answer.
| Option | Fits when | Watch for |
|---|---|---|
| System browser with a loopback redirect | The machine running the tool has a browser you can use | Listening only on the loopback address, and only during sign-in |
| Device authorization grant | The tool runs where no browser is available, such as a remote server | A user code someone else sends you |
| Personal access token or API key | A person wants a simple credential to paste into scripts | Long lifetimes, broad access, and copies everywhere; not OAuth |
| Client credentials grant | Automation acts as itself, not as a person | Needs its own confidential client registration |
In the first two, Ava signs in at the photo service and approves photos-cli, so the tool receives tokens limited to what she approved. photos-cli is installed by everyone who downloads it, so it is a public client with no secret, and both flows rely on the protections public clients use, such as PKCEProof Key for Code Exchange, pronounced pixie: the client creates a secret for each authorization attempt, sends only a hash of it at the start, and proves it holds the secret when redeeming the code, so a stolen authorization code is useless to anyone else. in the authorization code flowThe main OAuth flow: the browser visits the authorization server, where the person signs in and approves access, then carries an authorization code back to the client, which exchanges it for tokens directly. When the client runs on a server, the tokens never pass through the browser..
Signing in with a browser
On Ava's laptop, signing in runs the authorization codeA short-lived, single-use value that the authorization server sends back to a client through the browser. The client swaps it for tokens in a direct request to the authorization server; APIs never accept it. flow with PKCE, as any desktop application would. All domains, tokens and keys in these examples are fictional.
$ photos-cli login
Opening your browser to sign in at auth.photos.example.
If it does not open, visit this address:
https://auth.photos.example/authorize?response_type=code&client_id=photos-cli&...
Waiting for you to finish signing in...
Signed in. Tokens saved in the system credential store.The address is shortened here for display. Before opening the browser, the tool asked the operating system for a free port and started listening on it at 127.0.0.1, the loopback address that only programs on the same machine can reach. Its authorizationDeciding whether a requested action is permitted, such as whether a signed-in account may delete a particular photo. Authentication says which account is asking; authorization decides what it can do. request used http://127.0.0.1:53682/oauth/callback as the redirect URI. As the Redirect URIs and client metadata lesson explained, the photo service matches loopback addresses exactly except for the port. When Ava approves, the browser delivers the code to the tool's temporary listener, which shows a short "You can close this window" page, stops listening, and exchanges the code with its verifier.
The listener's details matter. It binds to the loopback interface only, so no other machine on the network can send it a response. It uses the literal address 127.0.0.1 rather than the name localhost, which a misconfigured machine could resolve somewhere unexpected. And it listens only for the length of the sign-in. Another program on the same machine could still try to grab the code, which is one more reason the exchange depends on the verifier.
On a remote server there is no browser to open. There the tool uses the device authorization grant, exactly as the Device authorization lesson followed it with a photo frame, from the user code to the polling. What a command-line tool adds is the choice of when to use it. photos-cli falls back to the device grant only when it cannot open a browser on this machine, such as in a terminal session with no display, or when you ask for it:
$ photos-cli login --device
On a device with a browser, open https://photos.example/device
and enter the code PXGH-KQRT.
Waiting for approval...
Signed in.The browser flow is the default wherever it works because its code can only arrive at a listener on this machine. A user code can be approved from anywhere, and the attack in A code someone else started relies on exactly that, so only enter a code that you have just seen printed by a tool you started yourself.
Credentials without a sign-in flow
Many services also let you create a personal access token or APIAn application programming interface: a defined way for software to interact with a service. A web API offers endpoints where other programs send HTTP requests. key in your account settings and paste it into a tool. This is not OAuthA framework that lets a person give an application limited access to their resources at another service, using tokens instead of sharing their password. OAuth is about access; OpenID Connect adds sign-in.. There is no authorization request and no consent screen for a particular client, just a credential you made for yourself. These tokens are popular because they are simple: they work in any script, on any machine, with no browser.
The simplicity has costs. Personal access tokens often last until someone remembers to delete them, carry whatever access was selected when they were created, and end up in scripts, configuration files, and chat messages. The service cannot tell which tool is using one, and nothing refreshes or rotates it. Services that offer them reduce the risk with expiry dates, narrow scopes chosen at creation, a page where you can review and revokeCancelling a credential, token, certificate, or authenticator before it would expire, so that it is no longer accepted. Whether every copy stops working at once depends on how services check it. your tokens, and a recognizable prefix in each token value, which lets code-hosting services spot one that was committed by mistake and report it.
Automation deserves separate thought. Running a scheduled job with your own sign-in gives the job your authority for as long as its refresh tokenA longer-lived credential that a client sends to the authorization server, never to an API, to get new access tokens without asking the person again. lasts. When the work belongs to an organization rather than to one person, such as archiving Lantern Studio's shared albums every night, the job should have its own client registration and use the client credentials grant, keeping its credential in the scheduler's secrets store. Its access is then defined for the job and can be reviewed or removed without touching anyone's personal account. That grant is only for software acting as itself. A tool acting for you should get its access from your approval.
Storing tokens on the machine
After Ava signs in, photos-cli holds a refresh token that keeps it working for weeks. Where the tool keeps that token decides who else can use it.
The best place is the operating system's credential store: the Keychain on macOS, Credential Manager on Windows, or a Secret Service keyring on Linux desktops. These stores keep secrets encrypted, release them to programs running as your account, and on some systems ask before doing so. Servers often have no such store, so the fallback is a file readable only by your account, such as ~/.config/photos-cli/credentials.json with permissions that exclude every other user. That stops other people on the machine, but not other programs running as you, and the file can travel into backups or a copied home directory.
Environment variables are convenient for automation, and they leak in their own ways. Every program the tool starts inherits them, and debugging output, crash reports, and build logs sometimes print the whole environment. Build systems usually mask known secret values in their logs, but a token printed in a slightly different form, encoded or split across lines, can slip past the mask.
Tokens also leak through the way commands are typed. A token given as an argument, as in photos-cli upload --token demo-cli-token-1, is saved in your shell history and, while the command runs, appears in the process list that other users of the same machine can often see. photos-cli instead reads a token from a prompt, from standard input, or from a file, which keeps it out of both.
Where photos-cli stores tokens
- Credential store
- Encrypted, released to her account
- File on a server
- Readable only by her account
The best place, then the fallback
Where a token escapes
- Environment variable
- Inherited, and printed in logs
- Command-line argument
- Shell history and the process list
Other programs and users can reach it
photos-cli logout revokes the refresh token, so any copy that escaped stops working too.
Signing out finishes the job. photos-cli logout revokes the refresh token at the photo service's revocation endpoint, then deletes the stored tokens, so any copy of the refresh token that escaped earlier, into a backup or a log, stops working too.
Check your understanding
Try these questions before moving on. If an answer isn't right, use the feedback and try again.
Try it in the Lab
- Run a command-line login with a loopback listener and a device codeRun a small CLI that signs in through the browser on a loopback port or with a device code, keeps its refresh token for your account only, runs automation as its own client and revokes on logout.