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.
| Request | Fields | Answer |
|---|---|---|
POST /api/register | username, password, optional email | {"ok":true,"code":"ok","message":"Account created.","user":"kane"} and a session cookie |
POST /api/login | username, password | The same shape, with a session cookie on success |
POST /api/logout | none | {"ok":true}; the session is ended |
GET /api/me | none | {"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:
| code | Meaning | HTTP status |
|---|---|---|
invalid | Wrong username or password. Deliberately does not say which. | 400 |
taken | The username is in use. | 400 |
bad-username | Usernames are 3 to 20 letters, digits or underscores. | 400 |
bad-password | Passwords are 6 to 72 characters. | 400 |
locked | Too many failed logins, or too many registrations from one address. | 429 |
banned | The account is banned. | 400 |
closed | Registration is turned off. | 400 |
origin | The request came from a page on another site. | 403 |
error | The database is unavailable. | 500 |
Knowing who is asking
- In an HTTP handler:
req.user(). - On a WebSocket:
c.getUser(), set from the cookie sent with the handshake. - Anywhere, from a token:
Main.launcher.getLoginHandler().sessionUser(token).
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
| Thing | How |
|---|---|
| Passwords | PBKDF2-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. |
| Sessions | A 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. |
| Restarts | Live sessions are loaded at startup; users stay logged in across a restart. |
Protections
- Lockout. Five wrong passwords for one account from one address lock that pair for five minutes.
- Registration limit. Five new accounts per address per hour.
- Cross-site requests. A login or registration posted from a page on another site is refused, and the cookie is not sent on cross-site posts.
- Bans. A banned account cannot log in and its sessions stop working. A banned address cannot connect to any JBend server.
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.
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.