mirror of
https://github.com/elyby/accounts.git
synced 2024-11-10 07:22:00 +05:30
45c2ed601d
Refactor all JWT-related components Replace RS256 with ES256 as a preferred JWT algorithm
75 lines
2.0 KiB
PHP
75 lines
2.0 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace api\components\User;
|
|
|
|
use api\components\OAuth2\Entities\AccessTokenEntity;
|
|
use common\models\Account;
|
|
use common\models\OauthSession;
|
|
use Yii;
|
|
use yii\base\NotSupportedException;
|
|
use yii\web\UnauthorizedHttpException;
|
|
|
|
class Oauth2Identity implements IdentityInterface {
|
|
|
|
/**
|
|
* @var AccessTokenEntity
|
|
*/
|
|
private $_accessToken;
|
|
|
|
private function __construct(AccessTokenEntity $accessToken) {
|
|
$this->_accessToken = $accessToken;
|
|
}
|
|
|
|
/**
|
|
* @inheritdoc
|
|
* @throws UnauthorizedHttpException
|
|
* @return IdentityInterface
|
|
*/
|
|
public static function findIdentityByAccessToken($token, $type = null): IdentityInterface {
|
|
/** @var AccessTokenEntity|null $model */
|
|
$model = Yii::$app->oauth->getAccessTokenStorage()->get($token);
|
|
if ($model === null) {
|
|
throw new UnauthorizedHttpException('Incorrect token');
|
|
}
|
|
|
|
if ($model->isExpired()) {
|
|
throw new UnauthorizedHttpException('Token expired');
|
|
}
|
|
|
|
return new static($model);
|
|
}
|
|
|
|
public function getAccount(): ?Account {
|
|
return $this->getSession()->account;
|
|
}
|
|
|
|
/**
|
|
* @return string[]
|
|
*/
|
|
public function getAssignedPermissions(): array {
|
|
return array_keys($this->_accessToken->getScopes());
|
|
}
|
|
|
|
public function getId(): string {
|
|
return $this->_accessToken->getId();
|
|
}
|
|
|
|
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');
|
|
}
|
|
|
|
private function getSession(): OauthSession {
|
|
return OauthSession::findOne($this->_accessToken->getSessionId());
|
|
}
|
|
|
|
}
|