Skip to content

feat(auth): end the sign-in at the server on logout - #45

Open
vigubikReply wants to merge 1 commit into
mainfrom
dev/REPLY-55245-logout-revokes
Open

vigubikReply wants to merge 1 commit into
mainfrom
dev/REPLY-55245-logout-revokes

Conversation

@vigubikReply

Copy link
Copy Markdown
Contributor

What changed

reply auth logout now ends the sign-in at the sign-in server as well as on this computer. Until now it only
removed the stored record, and the refresh token stayed valid at the server until its lifetime ran out.

  • The request: for a stored OAuth sign-in, the CLI posts the record's refresh token to the server's
    revocation endpoint (RFC 7009), ${authority}/connect/revocation, with token_type_hint=refresh_token and
    the CLI's client_id, under the same User-Agent as the token requests and with the same 20 s timeout. Then it
    removes the record.

  • One hold of the credentials lock covers the record's read, the revocation and the removal, so a refresh by
    another process can't store a newer token in between. A logout holds the lock no longer than a refresh does;
    CONTRIBUTING.md writes the sum out.

  • When the server doesn't confirm, the logout still happens here. The record is removed, the exit code stays
    0, and one line on stderr says the sign-in may still be valid at Reply, with the cause:

    What happened revoked The warning names
    any 2xx (the server answers 200 for a token it revoked and for one it doesn't know) true no warning
    another status, a redirect included false the HTTP status
    no answer within 20 s false the timeout
    the server can't be reached false that it couldn't be reached
    the stored record changed while the revocation was out (only after the lock was taken over from a stalled process) false that the sign-in changed
  • A redirect isn't followed. Following one would post the refresh token wherever it points, and a page
    answering there would read as confirmed.

  • --json gains revoked beside logged_out. It is left out when nothing was sent: an API-key record, an
    OAuth record with no refresh token, or no record.

  • A logout acts on the stored record, whatever --api-key or REPLY_API_KEY supplies. An API key is never
    sent to the revocation endpoint. No token is ever printed.

  • Docs: the README's "Authentication" and CONTRIBUTING.md ("Credentials on disk", "Tests") describe the new
    behaviour. The help text of auth logout now reads "End the stored sign-in at Reply and remove the stored
    credential".

Why

A logout that only deletes a file doesn't sign anyone out at the server. A copy of the credentials file taken
before the logout could go on refreshing the sign-in afterwards, and the person who logged out had no way to
end it.

How it was verified

Tests first. Every new or changed test was run against the code before the change and failed.

  • Process tests (src/__tests__/auth/refresh-processes.test.ts): built CLI processes run against a fake
    sign-in server on 127.0.0.1. The fake now applies a refresh when it arrives and holds only the answer, as a
    real server does, and has a strict revocation endpoint.
    • A logout that starts while another process is refreshing waits for the lock and revokes the refreshed token:
      the fake ends with no live refresh token and the record is gone.
    • A refusal (503), a redirect (307) and an unreachable server each end as a logout with revoked: false, one
      warning line, and no token in stdout or stderr. The redirect's target never receives the token.
    • A record that changes during the revocation is reported as revoked: false.
    • With REPLY_API_KEY set and --api-key passed, a stored OAuth record is still revoked, the server sees one
      request, and its body holds none of the keys. With an API-key record or no record, nothing is sent.
  • The timeout is tested in-process (src/__tests__/auth/oauth-flow.test.ts) against a server that accepts
    and never answers.
  • Each test was also run against a deliberately broken build, and went red: the read moved outside the lock,
    the token_type_hint dropped, the User-Agent dropped, the timeout misclassified.
  • The whole suite: npm run build, then npm test: 67 files, 1113 tests passed, 2 skipped (the existing
    case-insensitive-filesystem tests).

Not yet run against a live sign-in server. That run comes before the merge.

Compatibility

Additive: logged_out keeps its meaning and the exit codes are unchanged. A new field in --json output is a
minor change under "What counts as breaking", so this is a feat.

`reply auth logout` now revokes the stored refresh token at the sign-in server's
revocation endpoint (RFC 7009) before it removes the stored record, so a logged-out
sign-in can't be refreshed again. Until now it only removed the local record.

The read, the revocation and the removal share one hold of the credentials lock. When
the server doesn't confirm (a refusal, a redirect, a timeout, an unreachable server, or
a record that changed during the revocation), the record is still removed and one
warning says the sign-in may still be valid. `--json` gains `revoked`.
Comment thread src/auth/oauth-flow.ts
});
// The answer's body says nothing more (RFC 7009 §2.2); dropping it frees the connection.
await res.body?.cancel().catch(()=>undefined);
return res.ok ? {revoked: true} : {revoked: false, status: res.status};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking, the fix belongs in Identity: a 2xx here means the server deleted the token we sent, not that the sign-in ended. If someone with a copy of the credentials file refreshed first (T1 → T2), the file still holds the spent T1. Logout sends T1, Identity deletes only T1's row (IdentityServer4 revokes just the handle it's given) and answers 200, so we print revoked: true while the copy's T2 keeps refreshing, indefinitely under Sliding with no cap. Reuse detection from reply-team/Reply.Identity#840 doesn't catch it, since a logout never goes through a refresh.

Suggest a follow-up in Identity: revoking a refresh token also deletes its sign-in's live token (RefreshTokenRevoker.RevokeSignInAsync), so revoked: true holds whichever token of the sign-in the file has.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. The server will end the whole sign-in when any of its refresh tokens is revoked, for clients with reuse detection on. That is a server change and comes as a follow-up there; nothing changes in this PR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One limit to add: the server knows a used refresh token only until that token expires (30 days after it was issued) and is cleaned up. Past that, revoking it ends nothing, and a password change is what ends the sign-in.

This branch has not been deployed

No deployments
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.

2 participants