PocketMine-MP 5.43.2 git-a137a986d01d9af23452b2e741699a770c7ae112
Loading...
Searching...
No Matches
ProcessSelfSignedLoginTask.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\network\mcpe\auth;
25
29use function base64_decode;
30
36 private const TLS_KEY_ON_COMPLETION = "completion";
37
38 public const MOJANG_AUDIENCE = "api://auth-minecraft-services/multiplayer";
39
47 private NonThreadSafeValue|string|null $error = "Unknown";
48 private ?string $clientPublicKeyDer = null;
49
53 public function __construct(
54 private string $jwt,
55 private string $selfSignedKeyDer,
56 private string $clientDataJwt,
57 private bool $authRequired,
58 \Closure $onCompletion
59 ){
60 $this->storeLocal(self::TLS_KEY_ON_COMPLETION, $onCompletion);
61 }
62
63 public function onRun() : void{
64 try{
65 $this->clientPublicKeyDer = $this->validateChain();
66 $this->error = null;
67 }catch(VerifyLoginException $e){
68 $disconnectMessage = $e->getDisconnectMessage();
69 $this->error = $disconnectMessage instanceof Translatable ? new NonThreadSafeValue($disconnectMessage) : $disconnectMessage;
70 }
71 }
72
73 private function validateChain() : string{
74 $claims = AuthJwtHelper::validateSelfSignedAuthToken($this->jwt, $this->selfSignedKeyDer, audience: self::MOJANG_AUDIENCE);
75 //validateToken will throw if the JWT is not valid
76
77 $clientDerKey = base64_decode($claims->cpk, strict: true);
78 if($clientDerKey === false){
79 throw new VerifyLoginException("Invalid client public key: base64 error decoding");
80 }
81 //no further validation needed - OpenSSL will bail if the key is invalid
82 AuthJwtHelper::validateSelfSignedToken($this->clientDataJwt, $clientDerKey);
83
84 return $clientDerKey;
85 }
86
87 public function onCompletion() : void{
92 $callback = $this->fetchLocal(self::TLS_KEY_ON_COMPLETION);
93 $callback(false, $this->authRequired, $this->error instanceof NonThreadSafeValue ? $this->error->deserialize() : $this->error, $this->clientPublicKeyDer);
94 }
95}
__construct(private string $jwt, private string $selfSignedKeyDer, private string $clientDataJwt, private bool $authRequired, \Closure $onCompletion)
storeLocal(string $key, mixed $complexData)