Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 30 additions & 7 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ npm link # put the built `reply` binary on your PATH

The suite is fully offline — no test contacts the Reply.io API or the identity
server. `fetch` is stubbed, and the OAuth loopback flow is exercised against a
local `127.0.0.1` listener with an injected browser stub. One test runs two
built CLI processes (`dist/`) against a fake identity server on `127.0.0.1`, so
run `npm run build` before `npm test`. CI runs the build and tests on Linux and
Windows.
local `127.0.0.1` listener with an injected browser stub. Some tests run built
CLI processes (`dist/`) against a fake identity server on `127.0.0.1` (two
refreshing one sign-in together, a logout during a refresh, a logout the server
refuses), so run `npm run build` before `npm test`. CI runs the build and tests
on Linux and Windows.

`npm run smoke:hosts` is a separate check that runs `reply skills install`
entirely inside a throwaway `HOME`/`USERPROFILE` sandbox, and proves your real
Expand Down Expand Up @@ -67,9 +68,12 @@ reads, changes and rewrites the whole file while holding `credentials.json.lock`
beside it, and the rewrite is a temp file renamed into place. While another
program has the file open (an antivirus scan, say), Windows can refuse the
write's read of the file and its rename over it; the two are retried together,
for about 5 s in all, before the write fails. The lock file is created
exclusively and holds the holder's pid, host name, pid namespace (on Linux) and
the time it was taken. A lock older than 30 s, or whose holder is gone, is taken
for about 5 s in all, before the write fails. A refresh and a logout also read the
record once under the lock before their request to the identity server, and that
read isn't retried: a refused read fails the command at once, before anything is
sent, which keeps their time under the lock within the sum given below. The lock
file is created exclusively and holds the holder's pid, host name, pid namespace
(on Linux) and the time it was taken. A lock older than 30 s, or whose holder is gone, is taken
over; a live one is waited on for up to 45 s. The pid is trusted only on the host
and in the pid namespace that wrote it: sandboxes and containers that give each
command a pid namespace of its own give them equal pids, so elsewhere a lock is
Expand Down Expand Up @@ -100,6 +104,25 @@ endpoint's HTTP status) and `error` (its OAuth error code) when it answered, and
answer came. The top-level `status` stays unset, since it is Reply's answer to the
command itself, and no token or request body is ever printed.

`auth logout` ends an OAuth sign-in at the identity server before it removes the
record. Holding the lock once, it reads the record, posts its refresh token to the
revocation endpoint (`/connect/revocation`, RFC 7009, with
`token_type_hint=refresh_token` and the CLI's client id) and removes the record,
so a refresh by another process can't store a newer token in between that would
stay valid at the server. The revocation times out after 20 s, as the token
request does, so a logout holds the lock no longer than a refresh: one request of
up to 20 s and one write of about 5 s at most, inside the 30 s after which a lock
is taken over. Only a stall past that (a suspended machine, say) lets another
process take the lock over and store a newer token; the removal's own read sees
that the record changed, and the logout reports the sign-in as not ended. The
record is removed whatever the answer. A `2xx` counts as confirmed (RFC 7009
answers `200` for a token the server doesn't know, too). A redirect isn't
followed, so the token is posted nowhere else. A redirect, another status, a
timeout, a network error or a record that changed leaves one warning line on
stderr and `revoked: false` under `--json`. Nothing is sent for an API-key record
or when nothing is stored, and an API key, stored or from `--api-key` or
`REPLY_API_KEY`, is never sent.

## Credential resolution

Resolved in strict order, first hit wins:
Expand Down
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ Inspect and manage the active credential:
```sh
reply auth status # who you're signed in as, and how — no secrets shown
reply auth whoami # verify the stored credential against the API
reply auth logout # remove the stored credential
reply auth logout # end the sign-in at Reply and remove the stored credential
```

Pass a key for a single command with `--api-key` or the `REPLY_API_KEY`
Expand All @@ -130,6 +130,18 @@ server refuses outright fails with `auth.refresh_failed` (exit 2) and needs a ne
(`status`, and `error`, its OAuth error code) or why no answer came (`reason`:
`timeout`, `network`, `unreadable` or `lock_wait`). Tokens are never printed.

`reply auth logout` ends an OAuth login at Reply too: it revokes the stored refresh
token at the sign-in server, then removes the stored credential, so the login can't
be refreshed again from anywhere. If the sign-in server can't be reached, doesn't
answer within 20 s, refuses or redirects, the credential is still removed and a
warning on stderr says the login may still be valid at Reply. With `--json` the
result is `{"logged_out":true,"revoked":true}`, or `"revoked":false` when the logout
couldn't confirm the login ended at Reply; `revoked` is left out when there was
nothing to revoke (a stored API key,
or nothing stored). A logout always acts on the stored credential, whatever
`--api-key` or `REPLY_API_KEY` says, and an API key is never sent to the sign-in
server.

## Profiles

Profiles keep more than one Reply.io account signed in at once — each stores its
Expand Down Expand Up @@ -787,7 +799,8 @@ sets it for each command it runs. The value must be one lower-case name of up to
dots, `+` or `-`; anything else is left out and the User-Agent stays as it is.
It is a label, not an input: it never refuses a command and never prints
anything, and it is read only from the environment of the process. The token
requests a sign-in and its refreshes make carry the same User-Agent.
requests a sign-in and its refreshes make, and the revocation a logout sends,
carry the same User-Agent.

## Contributing

Expand Down
25 changes: 24 additions & 1 deletion src/__tests__/auth/oauth-flow.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import {describe, it, expect, beforeEach, afterEach, vi} from 'vitest';
import http from 'http';
import type {AddressInfo} from 'net';
import {URL} from 'url';

const real_fetch = globalThis.fetch;
const mock_fetch = vi.fn();
vi.stubGlobal('fetch', mock_fetch);

import {build_authorize_url, run_login, refresh_stored, browser_open_command, CLIENT_ID, SCOPE} from '../../auth/oauth-flow';
import {
build_authorize_url, run_login, refresh_stored, revoke_refresh_token, browser_open_command, CLIENT_ID, SCOPE,
} from '../../auth/oauth-flow';
import {RuntimeError} from '../../utils/errors';
import {cli_version} from '../../config';
import type {Oauth_record} from '../../credentials/types';
Expand Down Expand Up @@ -158,4 +162,23 @@ describe('auth/oauth-flow', ()=>{
});
});
});

describe('revoke_refresh_token', ()=>{
// Breaks if a revocation that got no answer in time is taken for one that couldn't reach the
// server: Node names the abort a TimeoutError, and a check that misses it makes a logout warn
// that the sign-in server could not be reached.
it('comes back unconfirmed, as a timeout, when the server never answers', async()=>{
const server = http.createServer(()=>{ /* takes the request and never answers */ });
await new Promise<void>(resolve=>server.listen(0, '127.0.0.1', resolve));
const {port} = server.address() as AddressInfo;
mock_fetch.mockImplementationOnce(real_fetch);
try {
expect(await revoke_refresh_token({authority: `http://127.0.0.1:${port}`, refresh_token: 'RT', timeout_ms: 100}))
.toEqual({revoked: false, reason: 'timeout'});
} finally {
server.closeAllConnections();
await new Promise<void>(resolve=>server.close(()=>resolve()));
}
});
});
});
Loading
Loading