mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
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:
@@ -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 {
|
||||
|
@@ -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";
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -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;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user