Completely restored authorization_code grant for user side.

Reworked oauth_sessions table.
Added extension to use MariaDB's JSON columns.
Rewritten tests for authorization_code grant for client side.
Deprecate some old shit.
[skip ci]
This commit is contained in:
ErickSkrauch
2019-09-18 02:14:05 +03:00
parent 8a1d7148d0
commit 45101d6453
27 changed files with 418 additions and 404 deletions

View File

@@ -4,6 +4,9 @@ namespace common\components\Redis;
use InvalidArgumentException;
use Yii;
/**
* @deprecated
*/
class Key {
private $key;

View File

@@ -5,6 +5,9 @@ use ArrayIterator;
use IteratorAggregate;
use Yii;
/**
* @deprecated
*/
class Set extends Key implements IteratorAggregate {
public function add($value): self {

View File

@@ -1,8 +1,10 @@
<?php
declare(strict_types=1);
namespace common\db\mysql;
use SamIT\Yii2\MariaDb\QueryBuilder as MysqlQueryBuilder;
use yii\db\ExpressionInterface;
use yii\db\mysql\QueryBuilder as MysqlQueryBuilder;
class QueryBuilder extends MysqlQueryBuilder {

View File

@@ -1,12 +1,20 @@
<?php
declare(strict_types=1);
namespace common\db\mysql;
use yii\db\mysql\Schema as MysqlSchema;
use SamIT\Yii2\MariaDb\JsonExpressionBuilder;
use SamIT\Yii2\MariaDb\Schema as MysqlSchema;
class Schema extends MysqlSchema {
public function createQueryBuilder() {
return new QueryBuilder($this->db);
$result = new QueryBuilder($this->db);
$result->setExpressionBuilders([
'yii\db\JsonExpression' => JsonExpressionBuilder::class,
]);
return $result;
}
}

View File

@@ -93,7 +93,7 @@ class Account extends ActiveRecord {
}
public function getOauthSessions(): ActiveQuery {
return $this->hasMany(OauthSession::class, ['owner_id' => 'id'])->andWhere(['owner_type' => 'user']);
return $this->hasMany(OauthSession::class, ['account_id' => 'id']);
}
public function getOauthClients(): OauthClientQuery {

View File

@@ -1,38 +1,32 @@
<?php
declare(strict_types=1);
namespace common\models;
use common\components\Redis\Set;
use Yii;
use yii\base\NotSupportedException;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveQuery;
use yii\db\ActiveRecord;
/**
* Fields:
* @property integer $id
* @property string $owner_type contains one of the OauthOwnerType constants
* @property string|null $owner_id
* @property string $client_id
* @property string $client_redirect_uri
* @property integer $created_at
* @property int $account_id
* @property string $client_id
* @property int $legacy_id
* @property array $scopes
* @property integer $created_at
*
* Relations:
* @property OauthClient $client
* @property Account $account
* @property Set $scopes
*/
class OauthSession extends ActiveRecord {
public static function tableName(): string {
return '{{%oauth_sessions}}';
return 'oauth_sessions';
}
public static function find(): OauthSessionQuery {
return new OauthSessionQuery(static::class);
}
public function behaviors() {
public function behaviors(): array {
return [
[
'class' => TimestampBehavior::class,
@@ -49,39 +43,28 @@ class OauthSession extends ActiveRecord {
return $this->hasOne(Account::class, ['id' => 'owner_id']);
}
public function getScopes(): Set {
return new Set(static::getDb()->getSchema()->getRawTableName(static::tableName()), $this->id, 'scopes');
}
public function getScopes(): array {
if (empty($this->scopes) && $this->legacy_id !== null) {
return Yii::$app->redis->smembers($this->getLegacyRedisScopesKey());
}
public function getAccessTokens() {
throw new NotSupportedException('This method is possible, but not implemented');
return (array)$this->scopes;
}
public function beforeDelete(): bool {
if (!$result = parent::beforeDelete()) {
return $result;
if (!parent::beforeDelete()) {
return false;
}
$this->clearScopes();
$this->removeRefreshToken();
if ($this->legacy_id !== null) {
Yii::$app->redis->del($this->getLegacyRedisScopesKey());
}
return true;
}
public function removeRefreshToken(): void {
/** @var \api\components\OAuth2\Repositories\RefreshTokenStorage $refreshTokensStorage */
// TODO: rework
$refreshTokensStorage = Yii::$app->oauth->getRefreshTokenStorage();
$refreshTokensSet = $refreshTokensStorage->sessionHash($this->id);
foreach ($refreshTokensSet->members() as $refreshTokenId) {
$refreshTokensStorage->delete($refreshTokensStorage->get($refreshTokenId));
}
$refreshTokensSet->delete();
}
public function clearScopes(): void {
$this->getScopes()->delete();
private function getLegacyRedisScopesKey(): string {
return "oauth:sessions:{$this->legacy_id}:scopes";
}
}

View File

@@ -1,41 +0,0 @@
<?php
declare(strict_types=1);
namespace common\models;
use yii\db\ActiveQuery;
/**
* @see \common\models\OauthSession
*/
class OauthSessionQuery extends ActiveQuery {
/**
* The owner_id field in the oauth_sessions table has a string type.
* If you try to search using an integer value, the MariaDB will not apply the index, which will cause
* a huge rows scan.
*
* After examining the query builder logic in Yii2, we managed to find a solution to bring the value
* that the builder will use to create a link to the string exactly before the construction
* and restore the original value afterwards.
*
* @param $builder
* @return ActiveQuery|\yii\db\Query
*/
public function prepare($builder) {
$idHasBeenCastedToString = false;
if ($this->primaryModel instanceof Account && $this->link === ['owner_id' => 'id']) {
$this->primaryModel->id = (string)$this->primaryModel->id;
$idHasBeenCastedToString = true;
}
$query = parent::prepare($builder);
if ($idHasBeenCastedToString) {
$this->primaryModel->id = (int)$this->primaryModel->id;
}
return $query;
}
}

View File

@@ -1,35 +1,27 @@
<?php
return [
'admin-test1' => [
'id' => 1,
'owner_type' => 'user',
'owner_id' => 1,
'account_id' => 1,
'client_id' => 'test1',
'client_redirect_uri' => 'http://test1.net/oauth',
'scopes' => null,
'created_at' => 1479944472,
],
'banned-account-session' => [
'id' => 2,
'owner_type' => 'user',
'owner_id' => 10,
'account_id' => 10,
'client_id' => 'test1',
'client_redirect_uri' => 'http://test1.net/oauth',
'scopes' => null,
'created_at' => 1481421663,
],
'deleted-client-session' => [
'id' => 3,
'owner_type' => 'user',
'owner_id' => 1,
'account_id' => 1,
'client_id' => 'deleted-oauth-client-with-sessions',
'client_redirect_uri' => 'http://not-exists-site.com/oauth/ely',
'scopes' => null,
'created_at' => 1519510065,
],
'actual-deleted-client-session' => [
'id' => 4,
'owner_type' => 'user',
'owner_id' => 2,
'account_id' => 2,
'client_id' => 'deleted-oauth-client-with-sessions',
'client_redirect_uri' => 'http://not-exists-site.com/oauth/ely',
'scopes' => null,
'created_at' => 1519511568,
],
];