Return user field when requestUser param received on authentication/refresh endpoint [deploy]

This commit is contained in:
ErickSkrauch
2021-03-06 10:37:58 +01:00
parent 077db4f328
commit 1aed8f59cb
6 changed files with 149 additions and 61 deletions

View File

@ -4,39 +4,33 @@ declare(strict_types=1);
namespace api\modules\authserver\models;
use common\models\Account;
use Lcobucci\JWT\Token;
class AuthenticateData {
final class AuthenticateData {
/**
* @var Account
*/
private $account;
private Account $account;
/**
* @var Token
*/
private $accessToken;
private string $accessToken;
/**
* @var string
*/
private $clientToken;
private string $clientToken;
public function __construct(Account $account, string $accessToken, string $clientToken) {
private bool $requestUser;
public function __construct(Account $account, string $accessToken, string $clientToken, bool $requestUser) {
$this->account = $account;
$this->accessToken = $accessToken;
$this->clientToken = $clientToken;
$this->requestUser = $requestUser;
}
public function getResponseData(bool $includeAvailableProfiles = false): array {
$uuid = str_replace('-', '', $this->account->uuid);
$result = [
'accessToken' => $this->accessToken,
'clientToken' => $this->clientToken,
'selectedProfile' => [
'id' => str_replace('-', '', $this->account->uuid),
// Might contain a lot more fields, but even Mojang returns only those:
'id' => $uuid,
'name' => $this->account->username,
'legacy' => false,
],
];
@ -46,6 +40,20 @@ class AuthenticateData {
$result['availableProfiles'] = $availableProfiles;
}
if ($this->requestUser) {
// There are a lot of fields, but even Mojang returns only those:
$result['user'] = [
'id' => $uuid,
'username' => $this->account->username,
'properties' => [
[
'name' => 'preferredLanguage',
'value' => $this->account->lang,
],
],
];
}
return $result;
}

View File

@ -34,10 +34,16 @@ class AuthenticationForm extends ApiForm {
*/
public $clientToken;
/**
* @var string|bool
*/
public $requestUser;
public function rules(): array {
return [
[['username', 'password', 'clientToken'], RequiredValidator::class],
[['clientToken'], ClientTokenValidator::class],
[['requestUser'], 'boolean'],
];
}
@ -85,7 +91,7 @@ class AuthenticationForm extends ApiForm {
/** @var Account $account */
$account = $loginForm->getAccount();
$token = Yii::$app->tokensFactory->createForMinecraftAccount($account, $this->clientToken);
$dataModel = new AuthenticateData($account, (string)$token, $this->clientToken);
$dataModel = new AuthenticateData($account, (string)$token, $this->clientToken, (bool)$this->requestUser);
/** @var OauthSession|null $minecraftOauthSession */
$minecraftOauthSession = $account->getOauthSessions()
->andWhere(['client_id' => OauthClient::UNAUTHORIZED_MINECRAFT_GAME_LAUNCHER])

View File

@ -28,10 +28,16 @@ class RefreshTokenForm extends ApiForm {
*/
public $clientToken;
/**
* @var string|bool
*/
public $requestUser;
public function rules(): array {
return [
[['accessToken', 'clientToken'], RequiredValidator::class],
[['accessToken'], AccessTokenValidator::class, 'verifyExpiration' => false],
[['requestUser'], 'boolean'],
];
}
@ -83,7 +89,7 @@ class RefreshTokenForm extends ApiForm {
$minecraftOauthSession->last_used_at = time();
Assert::true($minecraftOauthSession->save());
return new AuthenticateData($account, (string)$token, $this->clientToken);
return new AuthenticateData($account, (string)$token, $this->clientToken, (bool)$this->requestUser);
}
}