Skip to content

Commit

Permalink
[yggdrasil-api] implement Mojang publickeys API (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
YuxuanZuo authored Jan 19, 2024
1 parent 54ac0f3 commit a489657
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 19 deletions.
5 changes: 5 additions & 0 deletions plugins/yggdrasil-api/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
Route::post('invalidate', 'AuthController@invalidate');
});

Route::prefix('minecraftservices')
->group(function () {
Route::get('publickeys', 'ServicesController@getPublicKeys');
});

Route::prefix('sessionserver/session/minecraft')->group(function () {
Route::post('join', 'SessionController@joinServer');
Route::get('hasJoined', 'SessionController@hasJoinedServer');
Expand Down
16 changes: 3 additions & 13 deletions plugins/yggdrasil-api/src/Controllers/ConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Option;
use Yggdrasil\Exceptions\IllegalArgumentException;
use Yggdrasil\Utils\Key;

class ConfigController extends Controller
{
Expand Down Expand Up @@ -77,17 +77,7 @@ public function hello(Request $request, PluginManager $pluginManager)
$request->getHost(),
]))));

$privateKey = openssl_pkey_get_private(option('ygg_private_key'));

if (!$privateKey) {
throw new IllegalArgumentException(trans('Yggdrasil::config.rsa.invalid'));
}

$keyData = openssl_pkey_get_details($privateKey);

if ($keyData['bits'] < 4096) {
throw new IllegalArgumentException(trans('Yggdrasil::config.rsa.length'));
}
$privateKey = Key::getPrivateKey(config('ygg_private_key'));

$result = [
'meta' => [
Expand All @@ -100,7 +90,7 @@ public function hello(Request $request, PluginManager $pluginManager)
'feature.non_email_login' => true,
],
'skinDomains' => $skinDomains,
'signaturePublickey' => $keyData['key'],
'signaturePublickey' => Key::getPublicKey($privateKey),
];

if (!optional($pluginManager->get('disable-registration'))->isEnabled()) {
Expand Down
29 changes: 29 additions & 0 deletions plugins/yggdrasil-api/src/Controllers/ServicesController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Yggdrasil\Controllers;

use Illuminate\Routing\Controller;
use Yggdrasil\Utils\Key;

class ServicesController extends Controller
{
public function getPublicKeys()
{
$privateKey = Key::getPrivateKey(config('ygg_private_key'));

$result = [
'profilePropertyKeys' => [
[
'publicKey' => Key::getPublicKey($privateKey)
]
],
'playerCertificateKeys' => [
[
'pulicKey' => Key::getPublicKey($privateKey)
]
]
];

return json($result);
}
}
8 changes: 2 additions & 6 deletions plugins/yggdrasil-api/src/Models/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Log;
use Ramsey\Uuid\Uuid;
use Schema;
use Yggdrasil\Exceptions\IllegalArgumentException;
use Yggdrasil\Utils\Key;

class Profile
{
Expand Down Expand Up @@ -46,11 +46,7 @@ public function serialize($unsigned = null)

// 检查 RSA 私钥
if ($unsigned === false) {
$key = openssl_pkey_get_private(option('ygg_private_key'));

if (!$key) {
throw new IllegalArgumentException(trans('Yggdrasil::config.rsa.invalid'));
}
$key = Key::getPrivateKey(config('ygg_private_key'));

$textures['signatureRequired'] = true;
}
Expand Down
30 changes: 30 additions & 0 deletions plugins/yggdrasil-api/src/Utils/Key.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Yggdrasil\Utils;

use Yggdrasil\Exceptions\IllegalArgumentException;

class Key
{
public static function getPrivateKey($key)
{
$privateKey = openssl_pkey_get_private(option('ygg_private_key'));

if (!$privateKey) {
throw new IllegalArgumentException(trans('Yggdrasil::config.rsa.invalid'));
}

return $privateKey;
}

public static function getPublicKey($key)
{
$keyData = openssl_pkey_get_details($key);

if ($keyData['bits'] < 4096) {
throw new IllegalArgumentException(trans('Yggdrasil::config.rsa.length'));
}

return $keyData['key'];
}
}

0 comments on commit a489657

Please sign in to comment.