# Minecraft protocol

OAuthMC implements the Handshake, Status, Login, and (for link mode on 1.21.6+) Configuration states of the Minecraft Java Edition TCP protocol. It never enters the Play state, so world, chunk, entity, and registry changes across Minecraft releases do not affect it.

## Tested client matrix

`tests/clients/run-matrix.mjs` verifies every release against real `node-minecraft-protocol` clients in online mode:

| Minecraft version | Protocol version | Code mode | Link mode (dialog) | Notes |
| --- | --- | --- | --- | --- |
| 1.8.9 | 47 | Tested | Fallback (code kick) | Classic login layout |
| 1.12.2 | 340 | Tested | Fallback (code kick) | Classic login layout |
| 1.16.5 | 754 | Tested | Fallback (code kick) | Classic login layout |
| 1.19 | 759 | Tested | Fallback (code kick) | Encryption Response includes `hasToken` boolean |
| 1.19.2 | 760 | Tested | Fallback (code kick) | Encryption Response includes `hasToken` boolean |
| 1.20.1 | 763 | Tested | Fallback (code kick) | Login Start includes optional UUID |
| 1.21.1 | 767 | Tested | Fallback (code kick) | Encryption Request includes `shouldAuthenticate` boolean |
| 1.21.6 | 771 | Fallback/code | Tested | First version with Configuration `Show Dialog` (`0x12`) |
| 1.21.8 | 772 | Fallback/code | Tested | Configuration `Show Dialog` (`0x12`) |
| 1.21.11 | 774 | Fallback/code | Tested | Configuration `Show Dialog` (`0x12`) |
| 26.1 | 775 | Tested | Tested | Latest version tested by `node-minecraft-protocol` (`Show Dialog` `0x12`) |

Protocol `776` (26.2, trailing 16-byte Session ID in Login Success) and protocol `777` (26.3, `Show Dialog` shifted to `0x13`) are implemented from the protocol spec and verified in `tests/integration.rs`, not in `run-matrix.mjs`.

## Why link mode uses a Configuration dialog

Minecraft's `DisconnectedScreen` renders kick reasons in a non-clickable `MultiLineTextWidget`. `open_url` click events inside a Login Disconnect or Configuration Disconnect text component cannot be clicked.

Starting in Minecraft 1.21.6 (protocol `771`), the client supports the Dialog system and the clientbound **Show Dialog (configuration)** packet during the Configuration state, without entering a world. Dialog action buttons natively support `open_url` (with Minecraft's confirmation prompt before launching the browser) and `copy_to_clipboard`.

- **Protocol >= 771 (1.21.6+)**: OAuthMC sends `Login Success` (`0x02`), waits for `Login Acknowledged` (`0x03`), and sends `Show Dialog (configuration)` with an inline `minecraft:multi_action` dialog containing **Open link** (`open_url`) and **Copy code** (`copy_to_clipboard`), `columns: 2`, `can_close_with_escape: false` (so Escape cannot dismiss the dialog with no way back), `pause: false` (required by the vanilla dialog codec when `after_action` is `"none"`), and `after_action: "none"`.
- **Protocol < 771**: OAuthMC falls back to the encrypted Login Disconnect code kick. The ticket still works by code, and the `code` template may include `{link}` (though older clients cannot click it).
- **Held-connection limits or mixed tenants**: if `OAUTHMC_MAX_HELD` or `OAUTHMC_MAX_HELD_PER_IP` is full, or if open tickets for that username span multiple tenants, OAuthMC falls back to the Login Disconnect code kick.
- **Rejoining on a held ticket**: at most one held connection is kept per ticket. When a player rejoins, OAuthMC signals the older held connection to disconnect with `replaced` and waits up to 2 seconds for its held slot to release so rejoins succeed even when `OAUTHMC_MAX_HELD=1` or `OAUTHMC_MAX_HELD_PER_IP=1`.

## Handshake and Status

- **Legacy `0xFE` Server List Ping**: connections starting with byte `0xFE` are closed immediately.
- **Status (intent `1`)**: OAuthMC echoes the client's protocol number so the server list never shows a version mismatch badge, reports `0/0` players, returns `OAUTHMC_MOTD` and the 64x64 PNG icon (`OAUTHMC_FAVICON`, or the built-in OAuthMC icon), and echoes the 8-byte Ping Request payload (`0x01`).
- **Login (intent `2`) and Transfer (intent `3`)**: both enter the Login state.

## Login state quirks across versions

- **Login Start (`0x00`)**: trailing fields after the username changed across versions (optional profile public key in 1.19, optional UUID in 1.19.3, required UUID in 1.20.2). OAuthMC reads only the leading username string (capped at 64 bytes) and ignores trailing payload bytes.
- **Encryption Request (`0x01`)**:
  - Server ID is always the empty string `""`.
  - Public key is DER-encoded X.509 `SubjectPublicKeyInfo` for the runtime RSA-1024 key.
  - Verify token is 4 fresh random bytes per connection.
  - Starting at protocol `766` (1.20.5+), a trailing `shouldAuthenticate` boolean (`0x01`) is appended.
- **Encryption Response (`0x01`)**:
  - For protocols `759` (1.19) and `760` (1.19.1/1.19.2), a boolean `hasToken` precedes the verify token. When `hasToken` is `0`, the client sent a salt and signature instead of the encrypted token; OAuthMC accepts this (see [Security model](https://docs.sculk.network/nightly/oauthmc/security.md#cryptography-and-protocol-hardening)).
  - For all other versions, the encrypted verify token is decrypted and compared in constant time.
  - The decrypted shared secret must be 16 bytes and initializes AES-128-CFB8 in both directions (`IV = key`).
- **Server hash**: Minecraft's signed hex SHA-1 digest of `"" || shared_secret || public_key_der` (two's complement with `-` prefix when MSB is set, leading zeroes stripped).
- **Login Disconnect (`0x00`)**: always encoded as a JSON text component `{"text": "..."}`, which remains valid in the Login state across all supported versions (including 1.20.3+ and 26.x).
- **Login Success (`0x02`)** (link mode, protocol `>= 771`): encrypted 16-byte UUID, username string, and VarInt-prefixed properties array forwarded from Mojang's `hasJoined` response. Starting at protocol `776` (26.2+), a trailing 16-byte random Session ID is appended.
- **Login Acknowledged (`0x03`, serverbound)**: empty payload sent by the client to enter the Configuration state.

## Configuration state (link mode)

Text components and inline dialogs in Configuration packets use the 1.20.2+ nameless (network) NBT format: no root name bytes after the root tag byte (`0x08` + big-endian `u16` length + modified UTF-8 for plain text components; `0x0A` + entries + `0x00` for compounds). Every NBT string uses Java modified UTF-8 (`DataInput.readUTF` / `DataOutput.writeUTF`: `U+0000` as `C0 80`, supplementary characters as a 6-byte UTF-16 surrogate pair) and truncates cleanly before `u16::MAX` bytes without splitting a surrogate pair.

| Packet | Direction | Packet ID | Notes |
| --- | --- | --- | --- |
| Disconnect (configuration) | Clientbound | `0x02` | Nameless NBT `TAG_String` (`verified`, `expired`, `locked`, `replaced`, `shutdown`, or `code` when `OAUTHMC_LINK_HOLD_SECONDS` expires before ticket TTL). |
| Keep Alive (configuration) | Clientbound / Serverbound | `0x04` / `0x04` | Big-endian `i64`; sent every 12 seconds, connection dropped if unanswered after 30 seconds. |
| Show Dialog (configuration) | Clientbound | `0x12` (`771..=776`), `0x13` (`>= 777`) | Nameless NBT `TAG_Compound` inline `minecraft:multi_action` dialog. Shifted by `+1` in 26.3 (`777`) after `Post Effects` was inserted at `0x0A`. |

While held in Configuration, a dedicated reader task accepts serverbound packets up to 1 MiB (keeping `OAUTHMC_MAX_PACKET_LEN` for Handshake and Login) so modded clients (Fabric, NeoForge) sending large channel-registration plugin messages are not dropped. Every serverbound packet except Keep Alive is streamed and discarded in 4 KiB chunks without buffering the full payload. When the ticket transitions (redeemed, locked, deleted, or superseded by a newer join) or the server shuts down, a `tokio::sync::watch` or `Notify` signal wakes the hold loop immediately and sends Configuration Disconnect (`0x02`).
