Beta

Create a tenant

A new tenant starts with its own users, OAuth settings, audit history and logs. You are its first Tenant Admin.

Command-line tools

A command-line tool such as photos-cli is a public client 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 session with no browser at all, or inside an automated job where nobody is present. Each situation points to a different answer.

OptionFits whenWatch for
System browser with a loopback redirectThe machine running the tool has a browser you can useListening only on the loopback address, and only during sign-in
Device authorization grantThe tool runs where no browser is available, such as a remote serverA user code someone else sends you
Personal access token or API keyA person wants a simple credential to paste into scriptsLong lifetimes, broad access, and copies everywhere; not OAuth
Client credentials grantAutomation acts as itself, not as a personNeeds 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 PKCE in the authorization code flow.

Signing in with a browser

On Ava's laptop, signing in runs the authorization code 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 authorization 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 API key in your account settings and paste it into a tool. This is not OAuth. 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 revoke 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 token 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.

Four places photos-cli could keep or receive Ava's token, and who else can reach it in each.

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.

Ava runs photos-cli in a terminal session on a remote server that has no browser. Which way of signing in fits? 1 of 3

Choose one answer.

Why should photos-cli not accept a token as a command-line argument? 2 of 3

Choose one answer.

photos-cli logout revokes Ava's refresh token at the photo service before deleting the stored copy. Why not just delete the stored copy? 3 of 3

Choose one answer.

Try it in the Lab

We value your privacy

We use cookies and similar technologies to enhance your browsing experience, and analytics to understand our traffic. By clicking "Allow all", you consent to optional analytics. Cookie Policy

Learn identity