Login and security scripts
What a login or protection script needs so that a slow or broken script never lets an action through.
The login gate
A login script declares itself the server's login gate in on_load with context.declare_login_gate(). From then on a player needs the gate's admission:
- Call
player.admit()after a good login, andplayer.revoke_admission()on a logout. Only the gate may call them, and only while it runs. player.is_admitted()tells any script whether a player is admitted (always true without a gate).- A player whose session Mojang verified needs no admission and no password:
player.verification()returnsVerification::Premium. The other values arePassword(the gate decides),None(no gate: anyone can join under any name) andProxy(a proxy forwarded the identity). - A player who arrives from another server of your group with a move pass counts as admitted (
player.arrived_by_move()). - While the gate script is unloaded or failed, nobody is admitted: the server stays closed instead of failing open.
- There is one gate per server: a second script that declares itself is refused.
On Play Strawberry's offline-mode servers the login gate is Play Strawberry's own login script, auth-lite, and no other script can declare itself the gate. Only that script also lets players with a premium Minecraft account in without a password.
An admission belongs to one connection. player.admit() admits only a player who is in the game: called on a player handle kept after that player left, it admits nobody. A password login counts only for the connection that typed it. When the server reconfigures a player's connection (for example after a resource pack or settings change), an admitted player stays admitted: at player-login, player.is_admitted() tells whether this connection is admitted.
Before a player is admitted
- The server refuses the player's own actions (blocks, items, entities, inventories, signs, books, vehicles) and holds the player in place. Only what the connection needs stays open.
- The player's custom-payload messages and the platform's commands wait.
- The player's chat lines and commands reach the gate while it runs. A command runs only if the gate marked it sensitive. While the gate does not run, they reach nobody.
The gate's sensitive commands (its login, register and change-password commands) are its own: their command events, their chat-command packets and their completions reach only the gate, and no other script may register commands of those names.
Never fail open
A protection, permission or login script decides whether an action happens. Register every such handler as blocking, at HandlerPriority::Lowest (so scripts that skip cancelled events never see the action), with on_failure: FailureVerdict::Deny. If your script faults or hangs, the action is refused, and it stays refused while the script restarts and after it stops for good.
Cover every way in
A player who is not logged in must not be able to change the world, reach other players or run commands. Your handlers run before the server's own check. A login script cancels, for that player, at least:
- chat and commands other than its own login commands (
PlayerChatEvent,PlayerCommandSendEvent); - movement and teleports (
PlayerMoveEvent,PlayerTeleportEvent); - block breaking and placing, interaction, item drops and pickups, inventory clicks;
- damage dealt and taken, entity targeting, vehicles and portals;
- completions and the command list, with the extension events in Tab-complete and command lists;
- if you gate network packets with
PacketReceivedEvent, keep keep-alives, the client's tick end and position packets flowing, or the client is kicked.
A cancelled move puts the player back where they were.
Protection and login scripts are asked every time before a player picks up a mob or fluid with a bucket, brushes, shears a beehive or a sheep, uses riptide, lights fire, throws a projectile, edits or opens a sign, opens a container, swaps hands, leashes a mob or picks up an arrow or trident. If the scripts cannot be asked at that moment, the action does not happen.
Passwords
Hash and check passwords with the server's password hashing, declare your login, register and change-password commands sensitive so the server log never shows their arguments and no other script sees them, and store only PHC strings in your data folder.
Names and sessions
On an offline-mode server, anyone can type any name. Track sessions by the player's connection (for example the entity id at join), not only by name, so that a reconnect or a second login with the same name never inherits a logged-in session. Compare names without regard to case when you look up accounts.
Test it
Test every guarded action while logged out, a wrong password, a reload and a restart of your script, and a script fault in the middle of a login (the action must stay refused). Check that no log line contains a password.
Check a connection before the player joins
A script can hold every connection before the player joins, ask the Java client for a cookie, and admit or refuse the connection (Paper's AsyncPlayerConnectionConfigureEvent with retrieveCookie). A typical use: a lobby stores a signed ticket on the client with send_packet before it transfers the player, and the game server checks the ticket.
- Call
context.declare_connection_check()once and register the extension eventsconnection-checkandconnection-cookie. - On
connection-check, take the connection fromconnection_check::connection(event)and keep it until you decide. - If you need a ticket, call
request_cookie(key). The answer comes once asconnection-cookie(connection_check::cookie(event)), with the bytes, orNonewhen the client has no such cookie. - Call
admit()orrefuse(message).
The player enters the world only when every script that declared the check admitted the connection. You see only your own connections' events and your own cookie answers. The check does not replace the login gate; both can run on one server.
The connection is disconnected with a short text in the player's language when it has waited 10 seconds, when a declaring script that has not admitted it is not running, or when such a script's handler fails.
Limits:
- At most 128 connections wait at once, 4 from one known address.
- At most 4 cookie requests of one script wait for one connection.
- A cookie is at most 5120 bytes. A key is
namespace:pathin lower case, at most 256 bytes, and never in thestrawberrynamespace. - A refusal message is cut at 512 characters.
Errors:
InvalidKey: the key breaks the rules above.Unsupported: a Bedrock client has no cookies. For a move between your sub-servers, read its data withsub_servers::checked_bedrock_handoff.Busy: 4 cookie requests already wait for this connection.Gone: the connection stopped waiting, or the handle belongs to an older instance of your script.
A cookie is kept by the client, and a modified client can send any bytes. Trust it only when it proves itself, for example a ticket your lobby signed over the player's UUID, the destination and an expiry, with a key only your servers know.
Connection checks across a reload
- A second
declare_connection_check()by the same script name in the same server run (a hot reload, an unload and load, a restart after a failure) answersOkand changes nothing. - A new instance may register the connection events without declaring: it is offered the waiting connections as long as this script name declared earlier in this server run. A name that never declared is never shown a connection.
- If a new instance fails to load, nobody is admitted and the waiting connections are disconnected when their time is up.
plugin unloadof a declaring script keeps the check on: nobody joins until the script runs again or the server restarts.- The check runs once per connection, before its first play phase. A player sent back to the configuration phase with
player.reconfigureis not checked again (no event, no cookie request).
Players you transfer
After transfer, transfer_labeled or transfer_checked the server sends the player the transfer packet and waits. It does not close the connection, and it cannot know whether the other server lets the player in.
- The player goes when their client follows the transfer. The leave event comes as for any leave, and
player.transfer_sent()is true in that handler when a transfer was sent (false when the call sent nothing: a refused label or host, a closed connection, or a packet a script's own hook cancelled). - A game that must not leave the player in play keeps them out of play itself until they leave, for example in spectator mode, or by a kick after a delay it chooses.
- A Strawberry server that receives a player by transfer accepts them by default (
accepts-transfers). With that setting off it refuses them with a message.