Skip to content
spaceport / docs
The Spaceport handbook

Sessions & Client Management — API Reference

Client

Package: spaceport.personnel

The Client class represents a browser session. Clients are identified by the spaceport-uuid cookie and tracked in the global ClientRegistry.

Static Methods

Client.getNewClient()

Creates a new Client with a generated UUID.

def client = Client.getNewClient()

Creates a new Client and immediately attaches the given cookie value.

def client = Client.getNewClient(cookies.'spaceport-uuid')

Client.getClient(String clientId)

Retrieves an existing Client by its internal ID (the user document _id for authenticated clients). If no registered Client matches, a new one is created and registered with the given ID.

def client = Client.getClient(userId)

Client.getAuthenticatedClient(String userId, String password)

Authenticates a user and returns the associated Client. Queries the CouchDB user-views/authenticate view, verifies the password with BCrypt, and fires authentication alerts.

def client = Client.getAuthenticatedClient('jeremy', 'hunter2')
if (client) {
    client.attachCookie(cookies.'spaceport-uuid')
}

Finds the Client currently associated with a given cookie value.

def client = Client.getClientByCookie(cookies.'spaceport-uuid')

Client.getClientByHandler(SocketHandler socketHandler)

Finds the Client associated with a given WebSocket handler.


Instance Properties

Property Type Access Description
created long read Timestamp (millis) when the Client was created
user_id String private The user document ID; access via getUserID(), set via authenticate(userId)
cargo Cargo read (final) The Client's root Cargo container
authenticated boolean private Authentication state; access via isAuthenticated(), set via authenticate(userId) / cleared via deauthenticate(cookie) — never by direct field access
authenticationCookies List<String> read Cookie values linked to this Client
sockets Set<WeakReference<SocketHandler>> read Active WebSocket connections

Instance Methods

isAuthenticated()

Returns whether this Client has been authenticated against a ClientDocument.

if (client.isAuthenticated()) {
    // user is logged in
}

getUserID()

Returns the user document ID for an authenticated Client.


getDock(String spaceportUUID)

Returns the Dock (session-scoped Cargo) for a given session UUID.

def dock = client.getDock(cookies.'spaceport-uuid')
dock.set('theme', 'dark')

getDocument()

Returns the ClientDocument for an authenticated Client.

def doc = client.getDocument()
def name = doc?.getName()

Associates a spaceport-uuid cookie value with this Client. Used after authentication to link the browser session.

client.attachCookie(cookies.'spaceport-uuid')

Disassociates a cookie value from this Client. This is a low-level primitive — it only drops the cookie from the Client's cookie list, leaving the authenticated flag, WebSockets, and registry entry untouched. It is also used internally by attachCookie() to reassign a cookie between Clients, which must not log anyone out.

For logging a user out, use deauthenticate(cookie) instead, which performs the full session teardown.

client.removeCookie(cookies.'spaceport-uuid')

authenticate(String userId)

Marks this already-bound Client as authenticated for the given user, in place and without a password. This is the supported way to restore a session that was validated out-of-band — for example, from a durable/persisted session store after a restart, or from an SSO assertion.

Because it mutates the Client the framework already bound to the request's cookie, the cookie↔Client binding stays intact and later requests keep resolving to the same object via getClientByCookie(). It deliberately does not mint a new Client (as getClient(userId) would) and does not verify credentials (as getAuthenticatedClient(userId, password) does).

// Restoring a session validated against a persisted session store
if (!client.isAuthenticated()) {
    client.authenticate(userId)
    r.context.dock = client.getDock(uuid)
}

Logs this Client out of a single session. Calls removeCookie(cookie), and if that was the Client's last cookie, performs the full teardown: sets authenticated = false, closes all WebSockets via closeSockets(), and removes the Client from the ClientRegistry.

If other cookies remain (the user is still logged in on another device), the Client stays authenticated and registered — only the one session is detached.

@Alert('on /logout hit')
static _logout(HttpResult r) {
    r.client.deauthenticate(r.context.cookies.'spaceport-uuid' as String)
    r.setRedirectUrl('/')
}

deauthenticateAll()

Logs this Client out of every session ("log out everywhere"). Unconditionally clears all cookies, sets authenticated = false, closes all WebSockets, and removes the Client from the ClientRegistry.

// "Sign out of all devices" action
client.deauthenticateAll()

closeSockets()

Closes and detaches all of this Client's WebSocket connections. Called automatically during logout (deauthenticate / deauthenticateAll) so a de-authenticated session does not keep a live authenticated socket open. Rarely needs to be called directly.


attachSocketHandler(SocketHandler handler)

Registers a WebSocket handler with this Client. Called internally when a WebSocket connection is established.


removeSocketHandler(SocketHandler handler)

Removes a WebSocket handler from this Client. Called internally when a WebSocket connection closes.


ClientDocument

Package: spaceport.personnel Extends: Document Database: ClientDocument.usersDatabase() (default users, configurable via memory cores.main.users database)

A ClientDocument represents a registered user stored in CouchDB. It contains profile information, credentials (BCrypt-hashed), permissions, and notes.

Static Methods

ClientDocument.createNewClientDocument(String userId, String password)

Creates a new user document in the configured users database with a BCrypt-hashed password.

def user = ClientDocument.createNewClientDocument('jeremy', 's3cureP@ss')
user.setName('Jeremy')
user.setEmail('jeremy@example.com')
user.addPermission('administrator')

ClientDocument.getClientDocument(String userId)

Retrieves an existing user document by ID.

def user = ClientDocument.getClientDocument('jeremy')

Profile Methods

All profile setters apply XSS sanitization via clean() and automatically save the document to CouchDB.

getName() / setName(String name)

user.setName('Jeremy Smith')
def name = user.getName()

getEmail() / setEmail(String email)

user.setEmail('jeremy@example.com')

getPhone() / setPhone(String phone)


getStatus() / updateStatus(String status)

user.updateStatus('Away')

Authentication Methods

checkPassword(String password)

Verifies a plaintext password against the stored hash. Supports both BCrypt hashes and legacy plaintext comparison.


changePassword(String newPassword, boolean hash = true)

Changes the user's password. By default, hashes the new password with BCrypt before storing.

The bcrypt work factor is configurable via the manifest (auth → bcrypt cost), defaulting to 10. Values in 4–31 are honored; anything else — unset, non-numeric, or out of range — falls back to 10. Changing the configured cost never invalidates existing hashes — each stored hash records the cost it was created with, so existing passwords keep verifying and only newly set passwords use the new cost. See the Manifest API Reference for details.

user.changePassword('newS3cureP@ss')
# In the config manifest — raise the work factor for new passwords
auth:
  bcrypt cost: 12

Permission Methods

Permissions are stored as a List<String> on the document. All permission methods automatically save the document.

hasPermission(String permission)

Checks whether the user has a specific permission.

if (user.hasPermission('administrator')) {
    // grant access
}

addPermission(String permission)

Adds a permission to the user.

user.addPermission('editor')

removePermission(String permission)

Removes a permission from the user.

user.removePermission('editor')

Notes Methods

Notes are stored as a timestamped map on the document. All note content is sanitized via clean().

addNote(String note)

Adds a note with an automatic timestamp key. The note content is sanitized via clean().

user.addNote('Verified email address')

fetchNote(String key)

Retrieves a note by its timestamp key.


updateNote(def note)

Updates an existing note (matched by its key property). The note content is sanitized via clean() and an updated timestamp is set automatically.


ClientRegistry

Package: spaceport.personnel

Tracks all active Client instances in a CopyOnWriteArrayList. Clients are added when created and removed only by an explicit logout — deauthenticate(cookie) (when the last cookie is detached) or deauthenticateAll(). There is no automatic expiration or eviction, so sessions that never log out remain registered for the lifetime of the application.

The registry is used internally by Client.getClient(), Client.getClientByCookie(), and Client.getClientByHandler() to look up clients.


Authentication Alerts

The authentication system fires alerts that your application can listen to:

'on client auth'

Fired when authentication succeeds, before the Client is returned.

import spaceport.computer.alerts.Alert
import spaceport.computer.alerts.results.Result

class AuthGuard {

    @Alert('on client auth')
    static _checkBanned(Result r) {
        if (isBannedUser(r.context.userID)) {
            r.cancelled = true
        }
    }
}

'on client auth failed'

Fired when authentication fails (wrong password or user not found).

@Alert('on client auth failed')
static _logFailedAuth(Result r) {
    if (r.context.exists) {
        println "Failed login attempt for user: ${r.context.userID}"
    }
}

The spaceport-uuid cookie behavior is configured in your manifest:

http:
  spaceport cookie expiration: 5184000  # seconds (default: 60 days)
Attribute Value
Name spaceport-uuid
Value Random UUID
Path /
HttpOnly true
SameSite Lax
Secure true in production, false in debug mode
Max-Age Configurable, default 60 days (5,184,000 seconds)

Route Context Access

In route handlers, sessions are available through the request context:

Expression Returns Description
r.context.client Client The current session's Client
r.context.dock Cargo The current session's Dock
client Client Shorthand (if delegated)
client.document ClientDocument The user's document (authenticated only)
client.isAuthenticated() boolean Whether the session is logged in

In Launchpad templates, the following variables are available:

Variable Type Description
client Client The current Client
dock Cargo The session Dock

Make the docs better.Edit this page on GitHub