mirror of
				https://github.com/elyby/accounts.git
				synced 2025-05-31 14:11:46 +05:30 
			
		
		
		
	Return user field when requestUser param received on authentication/refresh endpoint [deploy]
This commit is contained in:
		@@ -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;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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])
 | 
			
		||||
 
 | 
			
		||||
@@ -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);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user