2017-09-19 22:36:16 +05:30
|
|
|
<?php
|
2019-08-01 14:47:12 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
namespace api\components\User;
|
|
|
|
|
2019-12-09 22:01:54 +05:30
|
|
|
use api\components\Tokens\TokenReader;
|
2019-08-02 05:59:20 +05:30
|
|
|
use Carbon\Carbon;
|
2017-09-19 22:36:16 +05:30
|
|
|
use common\models\Account;
|
|
|
|
use Exception;
|
2019-08-01 14:47:12 +05:30
|
|
|
use Lcobucci\JWT\Token;
|
|
|
|
use Lcobucci\JWT\ValidationData;
|
2017-09-19 22:36:16 +05:30
|
|
|
use Yii;
|
|
|
|
use yii\base\NotSupportedException;
|
|
|
|
use yii\web\UnauthorizedHttpException;
|
|
|
|
|
|
|
|
class JwtIdentity implements IdentityInterface {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Token
|
|
|
|
*/
|
|
|
|
private $token;
|
|
|
|
|
2019-12-09 22:01:54 +05:30
|
|
|
/**
|
|
|
|
* @var TokenReader|null
|
|
|
|
*/
|
|
|
|
private $reader;
|
|
|
|
|
2019-08-01 14:47:12 +05:30
|
|
|
private function __construct(Token $token) {
|
2018-04-18 02:17:25 +05:30
|
|
|
$this->token = $token;
|
|
|
|
}
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
public static function findIdentityByAccessToken($rawToken, $type = null): IdentityInterface {
|
|
|
|
try {
|
2019-08-01 14:47:12 +05:30
|
|
|
$token = Yii::$app->tokens->parse($rawToken);
|
2017-09-19 22:36:16 +05:30
|
|
|
} catch (Exception $e) {
|
|
|
|
Yii::error($e);
|
|
|
|
throw new UnauthorizedHttpException('Incorrect token');
|
|
|
|
}
|
|
|
|
|
2019-08-01 14:47:12 +05:30
|
|
|
if (!Yii::$app->tokens->verify($token)) {
|
|
|
|
throw new UnauthorizedHttpException('Incorrect token');
|
|
|
|
}
|
2017-09-19 22:36:16 +05:30
|
|
|
|
2019-08-02 05:59:20 +05:30
|
|
|
$now = Carbon::now();
|
|
|
|
if ($token->isExpired($now)) {
|
2019-08-01 14:47:12 +05:30
|
|
|
throw new UnauthorizedHttpException('Token expired');
|
2017-09-19 22:36:16 +05:30
|
|
|
}
|
|
|
|
|
2019-08-02 05:59:20 +05:30
|
|
|
if (!$token->validate(new ValidationData($now->getTimestamp()))) {
|
2019-08-01 14:47:12 +05:30
|
|
|
throw new UnauthorizedHttpException('Incorrect token');
|
|
|
|
}
|
|
|
|
|
|
|
|
return new self($token);
|
|
|
|
}
|
|
|
|
|
2019-08-02 05:59:20 +05:30
|
|
|
public function getToken(): Token {
|
|
|
|
return $this->token;
|
|
|
|
}
|
|
|
|
|
2019-08-01 14:47:12 +05:30
|
|
|
public function getAccount(): ?Account {
|
2019-12-09 22:01:54 +05:30
|
|
|
return Account::findOne(['id' => $this->getReader()->getAccountId()]);
|
2017-09-19 22:36:16 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
public function getAssignedPermissions(): array {
|
2019-12-09 22:01:54 +05:30
|
|
|
return $this->getReader()->getScopes() ?? [];
|
2017-09-19 22:36:16 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
public function getId(): string {
|
2019-08-01 14:47:12 +05:30
|
|
|
return (string)$this->token;
|
2017-09-19 22:36:16 +05:30
|
|
|
}
|
|
|
|
|
2019-08-02 05:59:20 +05:30
|
|
|
// @codeCoverageIgnoreStart
|
2017-09-19 22:36:16 +05:30
|
|
|
public function getAuthKey() {
|
|
|
|
throw new NotSupportedException('This method used for cookie auth, except we using Bearer auth');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function validateAuthKey($authKey) {
|
|
|
|
throw new NotSupportedException('This method used for cookie auth, except we using Bearer auth');
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function findIdentity($id) {
|
|
|
|
throw new NotSupportedException('This method used for cookie auth, except we using Bearer auth');
|
|
|
|
}
|
|
|
|
|
2019-08-02 05:59:20 +05:30
|
|
|
// @codeCoverageIgnoreEnd
|
|
|
|
|
2019-12-09 22:01:54 +05:30
|
|
|
private function getReader(): TokenReader {
|
|
|
|
if ($this->reader === null) {
|
|
|
|
$this->reader = new TokenReader($this->token);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->reader;
|
|
|
|
}
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
}
|