2016-08-04 03:37:21 +05:30
|
|
|
<?php
|
2017-09-19 22:36:16 +05:30
|
|
|
namespace api\components\User;
|
2016-08-04 03:37:21 +05:30
|
|
|
|
2016-11-30 14:49:10 +05:30
|
|
|
use api\components\OAuth2\Entities\AccessTokenEntity;
|
2016-08-04 03:37:21 +05:30
|
|
|
use common\models\Account;
|
|
|
|
use common\models\OauthSession;
|
2016-11-30 14:49:10 +05:30
|
|
|
use Yii;
|
2016-08-04 03:37:21 +05:30
|
|
|
use yii\base\NotSupportedException;
|
|
|
|
use yii\web\UnauthorizedHttpException;
|
|
|
|
|
|
|
|
/**
|
2017-09-19 22:36:16 +05:30
|
|
|
* @property Account $account
|
2016-08-04 03:37:21 +05:30
|
|
|
*/
|
|
|
|
class Identity implements IdentityInterface {
|
|
|
|
|
|
|
|
/**
|
2016-11-30 14:49:10 +05:30
|
|
|
* @var AccessTokenEntity
|
2016-08-04 03:37:21 +05:30
|
|
|
*/
|
|
|
|
private $_accessToken;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
2017-09-19 22:36:16 +05:30
|
|
|
* @throws \yii\web\UnauthorizedHttpException
|
|
|
|
* @return IdentityInterface
|
2016-08-04 03:37:21 +05:30
|
|
|
*/
|
2017-09-19 22:36:16 +05:30
|
|
|
public static function findIdentityByAccessToken($token, $type = null): IdentityInterface {
|
|
|
|
if ($token === null) {
|
|
|
|
throw new UnauthorizedHttpException('Incorrect token');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Speed-improved analogue of the `count(explode('.', $token)) === 3`
|
|
|
|
if (substr_count($token, '.') === 2) {
|
|
|
|
return JwtIdentity::findIdentityByAccessToken($token, $type);
|
|
|
|
}
|
|
|
|
|
2016-12-18 04:50:53 +05:30
|
|
|
/** @var AccessTokenEntity|null $model */
|
2017-09-19 22:36:16 +05:30
|
|
|
$model = Yii::$app->oauth->getAccessTokenStorage()->get($token);
|
2016-08-04 03:37:21 +05:30
|
|
|
if ($model === null) {
|
|
|
|
throw new UnauthorizedHttpException('Incorrect token');
|
2017-09-19 22:36:16 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if ($model->isExpired()) {
|
2016-08-04 03:37:21 +05:30
|
|
|
throw new UnauthorizedHttpException('Token expired');
|
|
|
|
}
|
|
|
|
|
|
|
|
return new static($model);
|
|
|
|
}
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
public function getAccount(): ?Account {
|
2016-08-04 03:37:21 +05:30
|
|
|
return $this->getSession()->account;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-09-19 22:36:16 +05:30
|
|
|
* @return string[]
|
2016-08-04 03:37:21 +05:30
|
|
|
*/
|
2017-09-19 22:36:16 +05:30
|
|
|
public function getAssignedPermissions(): array {
|
|
|
|
return array_keys($this->_accessToken->getScopes());
|
|
|
|
}
|
|
|
|
|
2016-12-18 04:50:53 +05:30
|
|
|
public function getId(): string {
|
2016-11-30 14:49:10 +05:30
|
|
|
return $this->_accessToken->getId();
|
2016-08-04 03:37:21 +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');
|
|
|
|
}
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
private function __construct(AccessTokenEntity $accessToken) {
|
|
|
|
$this->_accessToken = $accessToken;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getSession(): OauthSession {
|
|
|
|
return OauthSession::findOne($this->_accessToken->getSessionId());
|
|
|
|
}
|
|
|
|
|
2016-08-04 03:37:21 +05:30
|
|
|
}
|