2019-09-22 21:12:21 +05:30
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace api\components\User;
|
|
|
|
|
|
|
|
use common\models\Account;
|
|
|
|
use common\models\OauthSession;
|
|
|
|
use Exception;
|
|
|
|
use Yii;
|
|
|
|
use yii\base\NotSupportedException;
|
|
|
|
use yii\web\UnauthorizedHttpException;
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
readonly class LegacyOAuth2Identity implements IdentityInterface {
|
2019-09-22 21:12:21 +05:30
|
|
|
|
|
|
|
/**
|
2024-12-02 15:40:55 +05:30
|
|
|
* @param string[] $scopes
|
2019-09-22 21:12:21 +05:30
|
|
|
*/
|
2024-12-02 15:40:55 +05:30
|
|
|
private function __construct(
|
|
|
|
private string $accessToken,
|
|
|
|
private int $sessionId,
|
|
|
|
private array $scopes,
|
|
|
|
) {
|
2019-09-22 21:12:21 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
* @throws UnauthorizedHttpException
|
|
|
|
*/
|
|
|
|
public static function findIdentityByAccessToken($token, $type = null): IdentityInterface {
|
|
|
|
$tokenParams = self::findRecordOnLegacyStorage($token);
|
|
|
|
if ($tokenParams === null) {
|
|
|
|
throw new UnauthorizedHttpException('Incorrect token');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($tokenParams['expire_time'] < time()) {
|
|
|
|
throw new UnauthorizedHttpException('Token expired');
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
return new self($token, $tokenParams['session_id'], $tokenParams['scopes']);
|
2019-09-22 21:12:21 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
public function getAccount(): ?Account {
|
2024-12-02 15:40:55 +05:30
|
|
|
return $this->getSession()?->account;
|
2019-09-22 21:12:21 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getAssignedPermissions(): array {
|
|
|
|
return $this->scopes;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getId(): string {
|
|
|
|
return $this->accessToken;
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
/** @codeCoverageIgnoreStart */
|
2019-09-22 21:12: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');
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
/** @codeCoverageIgnoreEnd */
|
2019-09-22 21:12:21 +05:30
|
|
|
private static function findRecordOnLegacyStorage(string $accessToken): ?array {
|
|
|
|
$record = Yii::$app->redis->get("oauth:access:tokens:{$accessToken}");
|
|
|
|
if ($record === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2024-12-02 15:40:55 +05:30
|
|
|
$data = json_decode((string)$record, true, 512, JSON_THROW_ON_ERROR);
|
|
|
|
} catch (Exception) {
|
2019-09-22 21:12:21 +05:30
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$data['scopes'] = (array)Yii::$app->redis->smembers("oauth:access:tokens:{$accessToken}:scopes");
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getSession(): ?OauthSession {
|
2019-12-15 20:31:36 +05:30
|
|
|
return OauthSession::findOne(['legacy_id' => $this->sessionId]);
|
2019-09-22 21:12:21 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|