2016-05-30 05:14:17 +05:30
|
|
|
<?php
|
|
|
|
namespace api\components\User;
|
|
|
|
|
|
|
|
use common\models\AccountSession;
|
2016-11-04 14:53:04 +05:30
|
|
|
use DateInterval;
|
|
|
|
use DateTime;
|
2016-05-30 05:14:17 +05:30
|
|
|
use Yii;
|
|
|
|
use yii\web\IdentityInterface;
|
|
|
|
|
|
|
|
class LoginResult {
|
|
|
|
/**
|
|
|
|
* @var IdentityInterface
|
|
|
|
*/
|
|
|
|
private $identity;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $jwt;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var AccountSession|null
|
|
|
|
*/
|
|
|
|
private $session;
|
|
|
|
|
|
|
|
public function __construct(IdentityInterface $identity, string $jwt, AccountSession $session = null) {
|
|
|
|
$this->identity = $identity;
|
|
|
|
$this->jwt = $jwt;
|
|
|
|
$this->session = $session;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getIdentity() : IdentityInterface {
|
|
|
|
return $this->identity;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getJwt() : string {
|
|
|
|
return $this->jwt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return AccountSession|null
|
|
|
|
*/
|
|
|
|
public function getSession() {
|
|
|
|
return $this->session;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAsResponse() {
|
|
|
|
/** @var Component $component */
|
|
|
|
$component = Yii::$app->user;
|
2016-11-04 14:53:04 +05:30
|
|
|
|
|
|
|
$now = new DateTime();
|
|
|
|
$expiresIn = (clone $now)->add(new DateInterval($component->expirationTimeout));
|
|
|
|
|
2016-05-30 05:14:17 +05:30
|
|
|
$response = [
|
|
|
|
'access_token' => $this->getJwt(),
|
2016-11-04 14:53:04 +05:30
|
|
|
'expires_in' => $expiresIn->getTimestamp() - $now->getTimestamp(),
|
2016-05-30 05:14:17 +05:30
|
|
|
];
|
|
|
|
|
|
|
|
$session = $this->getSession();
|
|
|
|
if ($session !== null) {
|
|
|
|
$response['refresh_token'] = $session->refresh_token;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|