2016-08-21 04:51:39 +05:30
|
|
|
<?php
|
|
|
|
namespace api\modules\authserver\models;
|
|
|
|
|
|
|
|
use api\modules\authserver\exceptions\ForbiddenOperationException;
|
|
|
|
use api\modules\authserver\validators\RequiredValidator;
|
2016-09-01 13:01:43 +05:30
|
|
|
use common\models\Account;
|
2016-08-21 04:51:39 +05:30
|
|
|
use common\models\MinecraftAccessKey;
|
|
|
|
|
|
|
|
class RefreshTokenForm extends Form {
|
|
|
|
|
|
|
|
public $accessToken;
|
|
|
|
public $clientToken;
|
|
|
|
|
|
|
|
public function rules() {
|
|
|
|
return [
|
2016-09-01 13:01:43 +05:30
|
|
|
[['accessToken', 'clientToken'], RequiredValidator::class],
|
2016-08-21 04:51:39 +05:30
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return AuthenticateData
|
|
|
|
* @throws \api\modules\authserver\exceptions\AuthserverException
|
|
|
|
*/
|
|
|
|
public function refresh() {
|
|
|
|
$this->validate();
|
|
|
|
|
|
|
|
/** @var MinecraftAccessKey|null $accessToken */
|
|
|
|
$accessToken = MinecraftAccessKey::findOne([
|
|
|
|
'access_token' => $this->accessToken,
|
|
|
|
'client_token' => $this->clientToken,
|
|
|
|
]);
|
|
|
|
if ($accessToken === null) {
|
|
|
|
throw new ForbiddenOperationException('Invalid token.');
|
|
|
|
}
|
|
|
|
|
2016-09-01 13:01:43 +05:30
|
|
|
if ($accessToken->account->status === Account::STATUS_BANNED) {
|
|
|
|
throw new ForbiddenOperationException('This account has been suspended.');
|
|
|
|
}
|
|
|
|
|
2016-08-21 04:51:39 +05:30
|
|
|
$accessToken->refreshPrimaryKeyValue();
|
|
|
|
$accessToken->update();
|
|
|
|
|
|
|
|
$dataModel = new AuthenticateData($accessToken);
|
|
|
|
|
|
|
|
return $dataModel;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|