Accounts and sessions

com.server.login.LoginHandler

A login block (after a dataserver block) gives the application user accounts. JBend creates three tables (jbend_users, jbend_sessions, jbend_bans) and the web server gains four endpoints.

dataserver:
source env:DB_DSN

login:
sessions 7d

The endpoints

Requests are ordinary form posts; answers are JSON.

RequestFieldsAnswer
POST /api/registerusername, password, optional email{"ok":true,"code":"ok","message":"Account created.","user":"kane"} and a session cookie
POST /api/loginusername, passwordThe same shape, with a session cookie on success
POST /api/logoutnone{"ok":true}; the session is ended
GET /api/menone{"user":"kane"} or {"user":null}
fetch('/api/login', {
    method: 'POST',
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    body: 'username=' + encodeURIComponent(u) + '&password=' + encodeURIComponent(p)
}).then(r => r.json()).then(r => { if (!r.ok) alert(r.message); });

When ok is false, code says why and message is a sentence you can show:

codeMeaningHTTP status
invalidWrong username or password. Deliberately does not say which.400
takenThe username is in use.400
bad-usernameUsernames are 3 to 20 letters, digits or underscores.400
bad-passwordPasswords are 6 to 72 characters.400
lockedToo many failed logins, or too many registrations from one address.429
bannedThe account is banned.400
closedRegistration is turned off.400
originThe request came from a page on another site.403
errorThe database is unavailable.500

Knowing who is asking

All three return null for a visitor who is not logged in.

Using the login handler directly

For logins that do not go through the endpoints, such as a username and password sent over a game socket:

LoginHandler login = Main.launcher.getLoginHandler();
login.verify(username, password, c.getClientIP(), new LoginHandler.Callback() {
    public void result(LoginHandler.Result r) {
        if (r.ok)
            enterGame(c, r.username);
        else
            c.sendMessage("loginfail:" + r.message);
    }
});

verify checks the password and applies the lockout but opens no session. login and register take the same arguments (plus an email for register) and put a new session token in r.token. Callbacks run on the database thread.

How it is stored

ThingHow
PasswordsPBKDF2-HMAC-SHA256, 120,000 iterations, a random 16-byte salt per account, stored as pbkdf2$120000$salt$hash. They cannot be read back, by you or by anyone who copies the table.
SessionsA random 256-bit token. The browser holds it in an HttpOnly, SameSite=Lax cookie named jbend_session (also Secure when the request arrived over HTTPS through a proxy). The database holds only the token's SHA-256, so a leaked sessions table cannot be used to log in.
RestartsLive sessions are loaded at startup; users stay logged in across a restart.

Protections

ban user spammer        ban ip 203.0.113.9
unban user spammer      bans

Bans typed into the console take effect at once, including on connections that are already open, and are kept in the database.

Privileges

Each account has an integer privileges column, 0 by default, returned in Result.privileges on login. JBend attaches no meaning to it; use it for roles of your own, and set it with SQL.

Coming from JBend 1.x? Version 1 kept passwords encrypted with a key that was in the source code, and identified users by a cookie containing their name. Neither survives in 2.0. Accounts now live in new tables (jbend_users, not users) and old accounts are not carried over: 1.x passwords cannot be converted to hashes without knowing them, so users register again.