2016-05-31 03:33:30 +05:30
|
|
|
<?php
|
|
|
|
namespace api\models\authentication;
|
|
|
|
|
|
|
|
use api\models\base\ApiForm;
|
2016-06-17 02:02:23 +05:30
|
|
|
use common\helpers\Error as E;
|
2016-05-31 03:33:30 +05:30
|
|
|
use common\models\AccountSession;
|
|
|
|
use Yii;
|
|
|
|
|
|
|
|
class RefreshTokenForm extends ApiForm {
|
|
|
|
|
|
|
|
public $refresh_token;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var AccountSession|null
|
|
|
|
*/
|
|
|
|
private $session;
|
|
|
|
|
|
|
|
public function rules() {
|
|
|
|
return [
|
2016-06-17 02:02:23 +05:30
|
|
|
['refresh_token', 'required', 'message' => E::REFRESH_TOKEN_REQUIRED],
|
2016-05-31 03:33:30 +05:30
|
|
|
['refresh_token', 'validateRefreshToken'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function validateRefreshToken() {
|
|
|
|
if (!$this->hasErrors()) {
|
|
|
|
/** @var AccountSession|null $token */
|
|
|
|
if ($this->getSession() === null) {
|
2016-06-17 02:02:23 +05:30
|
|
|
$this->addError('refresh_token', E::REFRESH_TOKEN_NOT_EXISTS);
|
2016-05-31 03:33:30 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-09-19 22:36:16 +05:30
|
|
|
* @return \api\components\User\AuthenticationResult|bool
|
2016-05-31 03:33:30 +05:30
|
|
|
*/
|
|
|
|
public function renew() {
|
|
|
|
if (!$this->validate()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var \api\components\User\Component $component */
|
|
|
|
$component = Yii::$app->user;
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
return $component->renewJwtAuthenticationToken($this->getSession());
|
2016-05-31 03:33:30 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return AccountSession|null
|
|
|
|
*/
|
|
|
|
public function getSession() {
|
|
|
|
if ($this->session === null) {
|
|
|
|
$this->session = AccountSession::findOne(['refresh_token' => $this->refresh_token]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->session;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|