A tenant had exactly the users somebody had created with a command on the
server. That is not a missing screen: a shop with an owner and four staff
either shared one password or raised a ticket per person, and a phone app
for the shop floor could not exist while there was one account to sign in
as.
Registration is by invitation, never open signup - the same line already
drawn around creating a company. The code carries the address and the role
and the request carries only a password, so a code that gets forwarded
cannot become somebody else's account, and a staff invitation cannot be
redeemed as an owner. Single use lives in the UPDATE and the account is
created in the same transaction.
Deactivating a member revokes their sessions in that transaction too. An
access token lives twelve hours, so without it "remove their access"
removed it sometime tomorrow. The session list and revoke that go with it
are the benefit of opaque tokens the product had been paying for and never
collecting: nothing could say what was signed in, let alone stop one.
Face images now work on a deployment with no object storage, which was
every local install and every self-hosted site - the arrivals feed said
"not storing customer photos" for every customer forever, on the screen
whose whole job is to show a face. Bounded to one row per visitor, so it
grows with the customer base and not with footfall; the bucket stays
primary wherever one exists.
Image.auth says whether a URL needs the session, because a browser img
cannot load one that does, a mobile image view can, and a webview can do
neither - the desktop client resolves those to a data URI in Go.
Found by running it, not by tests:
* UPDATE ... RETURNING gives the value AFTER the update, so the prune
read back empty keys, deleted nothing, and the table grew with
footfall exactly as if it were not there. The fake agreed with either
version; only the live Postgres test caught it.
* Trusting only the auth flag broke every shop card, because Sites.jsx
rebuilt a partial snapshot object and dropped it. A relative URL is
now sufficient on its own.
* ago() renders a future time as "just now", so a code valid for a week
read "expires just now".
Verified live against real Postgres: invite, preview, escalation refused,
register into a session, replay 404, staff forbidden, device revoked and
401 at once, last owner refused, and a 92,405-byte camera JPEG stored,
served to its owner, 401 with no session, 404 to another tenant, and
rendered in a browser.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HViLj9gYNRtSr7YVZmW5sn
81 lines
3.0 KiB
Go
81 lines
3.0 KiB
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
)
|
|
|
|
// Which devices are signed in, and signing one of them out.
|
|
//
|
|
// This is the point of opaque tokens in a table rather than JWTs, and until now
|
|
// the product had the cost of that choice without the benefit. The argument
|
|
// recorded for it was that this system puts customer data on shop-floor PCs and
|
|
// staff phones that get lost, resold and shared between people, so "log that
|
|
// device out, now" has to actually work - and there was no endpoint that could
|
|
// list what was signed in, let alone stop one.
|
|
//
|
|
// It matters most on mobile, which is why it arrives with it: a phone is the
|
|
// device most likely to leave the building in somebody's pocket.
|
|
|
|
func (s *Server) handleSessions(w http.ResponseWriter, r *http.Request) {
|
|
p := PrincipalFrom(r.Context())
|
|
rows, err := s.Store.UserSessions(r.Context(), p.UserID)
|
|
if err != nil {
|
|
s.serverError(w, "list sessions", err)
|
|
return
|
|
}
|
|
if rows == nil {
|
|
rows = []DeviceSession{}
|
|
}
|
|
// Marked here rather than in SQL: which session is "this one" is a property
|
|
// of the request, and the store has no business knowing about requests.
|
|
for i := range rows {
|
|
rows[i].Current = rows[i].ID == p.SessionID
|
|
}
|
|
writeJSON(w, http.StatusOK, rows)
|
|
}
|
|
|
|
// handleRevokeSession signs one device out.
|
|
//
|
|
// A person may only revoke their OWN sessions - the store scopes the update by
|
|
// user id, so a session id, which is not a secret and travels in the list
|
|
// above, cannot be used to sign somebody else out. Removing a colleague's
|
|
// access is a different question with a different answer: deactivate them
|
|
// through the team endpoint, which revokes every session they have.
|
|
func (s *Server) handleRevokeSession(w http.ResponseWriter, r *http.Request) {
|
|
p := PrincipalFrom(r.Context())
|
|
id := r.PathValue("id")
|
|
if !looksLikeUUID(id) {
|
|
writeErr(w, http.StatusNotFound, "not_found", "No such device.")
|
|
return
|
|
}
|
|
if err := s.Store.RevokeUserSession(r.Context(), p.UserID, id); err != nil {
|
|
writeErr(w, http.StatusNotFound, "not_found", "No such device.")
|
|
return
|
|
}
|
|
s.Store.Audit(r.Context(), AuditEntry{
|
|
ClientID: p.ClientID, ActorID: p.UserID, ActorKind: "user",
|
|
Action: "auth.session.revoke", Entity: "session", EntityID: id,
|
|
})
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
|
|
// handleRevokeOtherSessions is "sign out everywhere else".
|
|
//
|
|
// It deliberately keeps the caller's own session. Somebody who has just lost a
|
|
// phone should not also be signed out of the device in their hand, in the
|
|
// middle of dealing with it.
|
|
func (s *Server) handleRevokeOtherSessions(w http.ResponseWriter, r *http.Request) {
|
|
p := PrincipalFrom(r.Context())
|
|
n, err := s.Store.RevokeOtherSessions(r.Context(), p.UserID, p.SessionID)
|
|
if err != nil {
|
|
s.serverError(w, "revoke sessions", err)
|
|
return
|
|
}
|
|
s.Store.Audit(r.Context(), AuditEntry{
|
|
ClientID: p.ClientID, ActorID: p.UserID, ActorKind: "user",
|
|
Action: "auth.session.revoke_others", Entity: "session",
|
|
Detail: map[string]any{"count": n},
|
|
})
|
|
writeJSON(w, http.StatusOK, map[string]any{"signed_out": n})
|
|
}
|