mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Добавлен роут и логика для обновления access_token по refresh_token'у
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
<?php
|
||||
namespace api\components\User;
|
||||
|
||||
use api\models\AccountIdentity;
|
||||
use common\models\AccountSession;
|
||||
use Emarref\Jwt\Algorithm\Hs256;
|
||||
use Emarref\Jwt\Claim;
|
||||
@@ -64,6 +65,30 @@ class Component extends YiiUserComponent {
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function renew(AccountSession $session) {
|
||||
$account = $session->account;
|
||||
$transaction = Yii::$app->db->beginTransaction();
|
||||
try {
|
||||
$identity = new AccountIdentity($account->attributes);
|
||||
$jwt = $this->getJWT($identity);
|
||||
|
||||
$result = new RenewResult($identity, $jwt);
|
||||
|
||||
$session->setIp(Yii::$app->request->userIP);
|
||||
$session->last_refreshed_at = time();
|
||||
if (!$session->save()) {
|
||||
throw new ErrorException('Cannot update session info');
|
||||
}
|
||||
|
||||
$transaction->commit();
|
||||
} catch (ErrorException $e) {
|
||||
$transaction->rollBack();
|
||||
throw $e;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getJWT(IdentityInterface $identity) {
|
||||
$jwt = new Jwt();
|
||||
$token = new Token();
|
||||
|
42
api/components/User/RenewResult.php
Normal file
42
api/components/User/RenewResult.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
namespace api\components\User;
|
||||
|
||||
use Yii;
|
||||
use yii\web\IdentityInterface;
|
||||
|
||||
class RenewResult {
|
||||
|
||||
/**
|
||||
* @var IdentityInterface
|
||||
*/
|
||||
private $identity;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $jwt;
|
||||
|
||||
public function __construct(IdentityInterface $identity, string $jwt) {
|
||||
$this->identity = $identity;
|
||||
$this->jwt = $jwt;
|
||||
}
|
||||
|
||||
public function getIdentity() : IdentityInterface {
|
||||
return $this->identity;
|
||||
}
|
||||
|
||||
public function getJwt() : string {
|
||||
return $this->jwt;
|
||||
}
|
||||
|
||||
public function getAsResponse() {
|
||||
/** @var Component $component */
|
||||
$component = Yii::$app->user;
|
||||
|
||||
return [
|
||||
'access_token' => $this->getJwt(),
|
||||
'expires_in' => $component->expirationTimeout,
|
||||
];
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user