2016-08-04 03:37:21 +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;
|
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;
|
|
|
|
|
2019-08-02 21:02:08 +05:30
|
|
|
class OAuth2Identity implements IdentityInterface {
|
2016-08-04 03:37:21 +05:30
|
|
|
|
|
|
|
/**
|
2016-11-30 14:49:10 +05:30
|
|
|
* @var AccessTokenEntity
|
2016-08-04 03:37:21 +05:30
|
|
|
*/
|
|
|
|
private $_accessToken;
|
|
|
|
|
2018-04-18 02:17:25 +05:30
|
|
|
private function __construct(AccessTokenEntity $accessToken) {
|
|
|
|
$this->_accessToken = $accessToken;
|
|
|
|
}
|
|
|
|
|
2016-08-04 03:37:21 +05:30
|
|
|
/**
|
|
|
|
* @inheritdoc
|
2019-08-01 14:47:12 +05:30
|
|
|
* @throws UnauthorizedHttpException
|
2017-09-19 22:36:16 +05:30
|
|
|
* @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 {
|
2016-12-18 04:50:53 +05:30
|
|
|
/** @var AccessTokenEntity|null $model */
|
2019-08-23 13:58:04 +05:30
|
|
|
// TODO: rework
|
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
|
|
|
}
|
|
|
|
|
2019-08-02 21:02:08 +05:30
|
|
|
// @codeCoverageIgnoreStart
|
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');
|
|
|
|
}
|
|
|
|
|
2019-08-02 21:02:08 +05:30
|
|
|
// @codeCoverageIgnoreEnd
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
private function getSession(): OauthSession {
|
2019-08-02 21:02:08 +05:30
|
|
|
return OauthSession::findOne(['id' => $this->_accessToken->getSessionId()]);
|
2017-09-19 22:36:16 +05:30
|
|
|
}
|
|
|
|
|
2016-08-04 03:37:21 +05:30
|
|
|
}
|