Sessions & Client Management — Internals
This document covers the internal implementation details of Spaceport's session system. It is intended for contributors and advanced users who need to understand how sessions work under the hood.
Cookie Creation Pipeline
The spaceport-uuid cookie is created at the HTTP layer before any route handling occurs.
HTTP Request Flow
When HttpRequestHandler processes an incoming request:
- Parse the
Cookieheader and extract any existingspaceport-uuidvalue. - If no
spaceport-uuidcookie is present:- Generate a new UUID via
UUID.randomUUID().toString() - Build a
Set-Cookieheader with the following attributes:Path=/HttpOnlySameSite=LaxSecure(only if not in debug mode)Max-Agefrom manifest confighttp.spaceport cookie expiration(default: 5,184,000 seconds / 60 days)
- Add the cookie to the response
- Generate a new UUID via
- The cookie value is then used to resolve or create the Client for this request.
The header is constructed by the pure static helper HttpRequestHandler.buildSessionCookieHeader(String uuid, int maxAge, boolean secure) and emitted as a raw Set-Cookie header via response.addHeader(...) rather than through response.addCookie(...). This is deliberate: the bundled Servlet 3.1 API's javax.servlet.http.Cookie has no way to express SameSite (that arrived in Servlet 6 / Jakarta), while Jetty 9.4 — the bundled server — honors the raw header directly. Keeping the construction in a pure helper also makes the security attributes unit-testable without a servlet container.
A production header looks like:
spaceport-uuid=<uuid>; Path=/; Max-Age=5184000; HttpOnly; SameSite=Lax; Secure
Debug mode drops only the Secure attribute (so the cookie works over http://localhost).
WebSocket Upgrade
When a WebSocket connection is established, the upgrade HTTP request contains cookies. The server:
- Reads
spaceport-uuidfrom the upgrade request's cookies - Calls
Client.getClientByCookie(cookie)to find the associated Client - Calls
client.attachSocketHandler(handler)to register the WebSocket connection - The socket handler is stored as a
WeakReference<SocketHandler>in the Client'ssocketsset
This means WebSocket connections are automatically associated with the same Client as the browser's HTTP requests, with no additional application code required.
Client Resolution Pipeline
Every incoming request resolves a Client through this sequence:
Request arrives with spaceport-uuid cookie
│
├─ Cookie present?
│ ├─ Yes → Client.getClientByCookie(cookieValue)
│ │ ├─ Client found → use it
│ │ └─ Client not found → Client.getNewClient(cookieValue)
│ │
│ └─ No → generate UUID, Client.getNewClient(uuid), set cookie
│
└─ Client placed in request context as r.context.client
Dock resolved as r.context.dock via client.getDock(spaceportUUID)
ClientRegistry Lookup
Client.getClientByCookie() iterates through the ClientRegistry's CopyOnWriteArrayList<Client> and checks each client's authenticationCookies list for a matching value. This is a linear scan — O(n) where n is the total number of Clients ever created.
Authentication Internals
CouchDB View Query
Client.getAuthenticatedClient(userId, password) authenticates against CouchDB:
- Queries the
user-views/authenticateview in theusersdatabase withkey: userId - The view returns the user document row if the user ID exists
- If no row is returned, authentication fails immediately
Password Verification
Once the user document is found:
- Instantiate a
ClientDocumentfrom the row - Call
clientDoc.checkPassword(password) checkPassword()usesBCrypt.checkpw(password, storedHash)if the stored value looks like a BCrypt hash- Falls back to plaintext string comparison for legacy accounts (pre-BCrypt migration)
Alert Dispatch and Cancellation
After password verification succeeds:
- A new Client is created (or an existing one is located)
client.authenticatedis set totrueclient.user_idis set to the user document IDAlerts.invoke('on client auth', [userID, client])is fired- If any listener returns
[cancelled: true], authentication is rolled back:client.authenticatedis reset tofalsegetAuthenticatedClient()returnsnull
- If no cancellation occurs, the authenticated Client is returned
On failure:
Alerts.invoke('on client auth failed', [userID, exists])is firedexistsistrueif the user ID was found but the password was wrongexistsisfalseif no user document was found at allgetAuthenticatedClient()returnsnull
This alert-based cancellation allows applications to implement bans, IP restrictions, or multi-factor authentication checks without modifying the core auth code.
Note that the in-place marking in steps 2–3 (setting authenticated and user_id on an existing Client) is also exposed publicly as client.authenticate(userId). Application code that restores an out-of-band-validated session — for example, from a durable session store — calls that method rather than touching the private fields.
Logout Teardown
The logout verbs perform a staged teardown built on top of the removeCookie primitive:
deauthenticate(cookie)callsremoveCookie(cookie), dropping the cookie fromauthenticationCookies- If that was the Client's last cookie:
authenticatedis set tofalsecloseSockets()closes and detaches every live WebSocket session- The Client is removed from
ClientRegistry.activeClients
- If other cookies remain (another device is still logged in), nothing else happens — the Client stays authenticated and registered
deauthenticateAll() skips the per-cookie logic: it clears all cookies and performs the full teardown unconditionally.
Removing a logged-out Client from the registry keeps O(n) registry scans shorter and prevents getClient(userId) from returning it for server-initiated pushes. A Client without cookies cannot be resolved by getClientByCookie(). Removal is concurrency-safe (activeClients is a CopyOnWriteArrayList), and the in-flight request is unaffected because it already holds its Client reference.
closeSockets() iterates a snapshot of the socket set: closing a session triggers onWebSocketClose → removeSocketHandler on the socket's own thread, which would otherwise mutate the set mid-iteration. Each still-connected handler is closed via handler.getSession()?.close() — the same mechanism SocketResult uses — and the set is then cleared.
Dock Storage Model
The Dock is a per-session Cargo container. Its storage backend depends on whether the Client is authenticated.
Authenticated Dock
ClientDocument (CouchDB)
└─ "spaceport-docks"
└─ "${spaceportUUID}" ← Cargo.fromDocument(document)
├─ key1: value1
├─ key2: value2
└─ ...
- Created via
Cargo.fromDocument(document)which binds the Cargo to a path within the CouchDB document - Reads and writes are persisted to the
usersdatabase - Each session UUID gets its own namespace, so a user logged in on two devices has two independent docks
- Data survives server restarts
Unauthenticated Dock
Client (in-memory)
└─ cargo (root Cargo)
└─ "spaceport-docks.${spaceportUUID}" ← child Cargo
├─ key1: value1
└─ ...
- Created as a child path on the Client's in-memory
cargoobject - No database backing — data is lost on server restart
- When a Client authenticates, the dock reference changes from memory-backed to document-backed. Any data stored in the anonymous dock is not migrated to the authenticated dock.
Dock Access Path
In route handlers, r.context.dock is resolved by:
- Getting the current Client from the request context
- Reading the
spaceport-uuidcookie value from the request - Calling
client.getDock(spaceportUUID) - The result is placed in the context and also made available to Launchpad templates as
dock
ClientRegistry Implementation
Data Structure
class ClientRegistry {
static final CopyOnWriteArrayList<Client> clients = new CopyOnWriteArrayList<>()
}
The CopyOnWriteArrayList provides thread-safe iteration without explicit locking, which is important since client lookups happen on every HTTP request. The trade-off is that writes (adding new clients) are more expensive because they copy the entire list.
No Automatic Eviction
The ClientRegistry removes Clients only on explicit logout — deauthenticate(cookie) unregisters a Client when its last cookie is detached, and deauthenticateAll() unregisters unconditionally. Beyond that, there is no automatic cleanup. This is a deliberate simplicity trade-off:
- Clients that never log out accumulate for the lifetime of the JVM process
- There is no expiration, no LRU eviction, no periodic cleanup
- For applications with moderate traffic, this is not a problem — Client objects are lightweight
- For high-traffic applications or long-running servers, memory usage will grow proportionally to the number of unique visitors who never log out
Lookup Methods
All lookups are linear scans over the full list:
| Method | Scans | Matches on |
|---|---|---|
getClient(clientId) |
All clients | client.user_id == clientId — prefers a match whose WebSocket is live (isActive()), else the first match |
getClientByCookie(cookie) |
All clients | cookie in client.authenticationCookies |
getClientByHandler(handler) |
All clients | handler in client.sockets (via WeakReference) |
The getClient socket preference exists because more than one registered Client can share a user_id — for example, a durable session restored onto one cookie's Client while the same user is already logged in on another cookie in the same process. For server-initiated lookups and pushes by user ID, the connected Client is the one that can actually receive the message. Single-match and no-match behavior is unchanged (no match still creates and registers a new Client).
WebSocket Client Association
WebSocket handlers are stored as WeakReference<SocketHandler> in the Client's sockets set. This means:
- If a SocketHandler is garbage collected (e.g., the connection was closed and no other references exist), the WeakReference will return
null - The
socketsset is not automatically cleaned of dead references getClientByHandler()must handlenulldereferences when iterating- A Client can have multiple simultaneous WebSocket connections (e.g., multiple browser tabs)
Socket Lifecycle
- Connection opened:
client.attachSocketHandler(handler)adds aWeakReferenceto the set - Messages flow: The handler is used for transmissions and other WebSocket communication
- Connection closed:
client.removeSocketHandler(handler)removes the reference from the set - Logout:
client.closeSockets()(called bydeauthenticate/deauthenticateAll) closes every still-connected session and clears the set, so a logged-out session cannot keep a live authenticated socket - If not explicitly removed: The WeakReference will eventually return
nullafter GC
Thread Safety Considerations
- ClientRegistry: Thread-safe via
CopyOnWriteArrayList— concurrent reads are lock-free, writes copy the backing array - Client.authenticationCookies: Standard
ArrayList— not inherently thread-safe, but typically only modified during login/logout (single-threaded per request) - Client.sockets:
SetofWeakReference— modifications happen during WebSocket open/close events - ClientDocument: Extends
Document, which handles CouchDB read/write synchronization - Cargo (Dock): Thread-safe via Cargo's internal synchronization mechanisms
Security Notes
Cookie Security
HttpOnlyprevents client-side JavaScript from reading the session cookie, mitigating XSS-based session theftSameSite=Laxkeeps the cookie off cross-site subrequests — the enabling condition for CSRF and one of the vectors for planting a fixed session ID — while still sending it on top-level navigations, so following an external link into the app (or bouncing through a login redirect) arrives with the session intact.Strictwould withhold the cookie on those navigations too, which is whyLaxis the right balance (and the modern browser default)Secureflag is only set in production (non-debug) mode — during development, cookies work over plain HTTP- The cookie value is a random UUID with no embedded information — session data is server-side only
XSS Sanitization
All ClientDocument profile setters (setName, setEmail, setPhone, updateStatus) and note operations (addNote, updateNote) pass values through a clean() method that strips potentially dangerous HTML/script content. This is a defense-in-depth measure — the data should also be escaped at render time.
Password Storage
- New passwords are hashed with BCrypt via
changePassword(), usingBCrypt.gensalt(bcryptCost()) - The work factor comes from the manifest (
auth→bcrypt cost), resolved byClientDocument.resolveBcryptCost(): values in4–31are honored; unset, non-numeric, or out-of-range values fall back to the default of10when the helper is called directly. Manifest validation rejects explicitly invalid costs before startup or migrations. The clamping means a config typo can neither throw (jBCrypt rejects costs outside 4–31) nor silently weaken hashing - Changing the configured cost never invalidates stored hashes — bcrypt embeds the cost in every hash string (
$2a$<cost>$...) andBCrypt.checkpwreads it from the stored hash, not from config. Only newly set passwords use the new cost checkPassword()supports both BCrypt hashes and legacy plaintext for migration scenarios- There is no automatic migration from plaintext to BCrypt on login, and no automatic re-hash when a stored hash's cost is below the configured target — applications must handle either explicitly if needed
Users Database Resolution
ClientDocument.usersDatabase() reads memory cores -> main -> users database, defaulting to users. Client document creation/lookup, password authentication views and registry setup all use this resolver. Application migrations that manipulate user records should use it too. Changing the setting does not migrate existing databases or records.
Make the docs better.Edit this page on GitHub