29 public const NETWORK_ID = ProtocolInfo::LOGIN_PACKET;
33 public string $clientDataJwt;
38 public static function create(
int $protocol,
JwtChain $chainDataJwt,
string $clientDataJwt) : self{
40 $result->protocol = $protocol;
41 $result->chainDataJwt = $chainDataJwt;
42 $result->clientDataJwt = $clientDataJwt;
46 public function canBeSentBeforeLogin() : bool{
51 $this->protocol = $in->getInt();
52 $this->decodeConnectionRequest($in->
getString());
55 protected function decodeConnectionRequest(
string $binary) : void{
58 $chainDataJsonLength = $connRequestReader->
getLInt();
59 if($chainDataJsonLength <= 0){
65 $chainDataJson = json_decode($connRequestReader->get($chainDataJsonLength), associative:
true, flags: JSON_THROW_ON_ERROR);
66 }
catch(\JsonException $e){
67 throw new PacketDecodeException(
"Failed decoding chain data JSON: " . $e->getMessage());
69 if(!is_array($chainDataJson) || count($chainDataJson) !== 1 || !isset($chainDataJson[
"chain"])){
70 throw new PacketDecodeException(
"Chain data must be a JSON object containing only the 'chain' element");
72 if(!is_array($chainDataJson[
"chain"])){
73 throw new PacketDecodeException(
"Chain data 'chain' element must be a list of strings");
76 foreach($chainDataJson[
"chain"] as $jwt){
78 throw new PacketDecodeException(
"Chain data 'chain' must contain only strings");
83 $wrapper =
new JwtChain;
84 $wrapper->chain = $jwts;
85 $this->chainDataJwt = $wrapper;
87 $clientDataJwtLength = $connRequestReader->getLInt();
88 if($clientDataJwtLength <= 0){
91 throw new PacketDecodeException(
"Length of clientData JWT must be positive");
93 $this->clientDataJwt = $connRequestReader->get($clientDataJwtLength);
97 $out->putInt($this->protocol);
98 $out->putString($this->encodeConnectionRequest());
101 protected function encodeConnectionRequest() : string{
104 $chainDataJson = json_encode($this->chainDataJwt, JSON_THROW_ON_ERROR);
105 $connRequestWriter->putLInt(strlen($chainDataJson));
106 $connRequestWriter->put($chainDataJson);
108 $connRequestWriter->putLInt(strlen($this->clientDataJwt));
109 $connRequestWriter->put($this->clientDataJwt);
111 return $connRequestWriter->getBuffer();
115 return $handler->handleLogin($this);