Restore full functionality of OAuth2 server [skip ci]

This commit is contained in:
ErickSkrauch
2019-09-22 00:17:21 +03:00
parent 45101d6453
commit 5536c34b9c
39 changed files with 506 additions and 1157 deletions

View File

@@ -41,6 +41,7 @@ use const common\LATEST_RULES_VERSION;
* @property UsernameHistory[] $usernameHistory
* @property AccountSession[] $sessions
* @property MinecraftAccessKey[] $minecraftAccessKeys
* @property-read OauthRefreshToken[] $oauthRefreshTokens
*
* Behaviors:
* @mixin TimestampBehavior
@@ -101,6 +102,10 @@ class Account extends ActiveRecord {
return $this->hasMany(OauthClient::class, ['account_id' => 'id']);
}
public function getOauthRefreshTokens(): ActiveQuery {
return $this->hasMany(OauthRefreshToken::class, ['account_id' => 'id']);
}
public function getUsernameHistory(): ActiveQuery {
return $this->hasMany(UsernameHistory::class, ['account_id' => 'id']);
}

View File

@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);
namespace common\models;
use Yii;
@@ -22,8 +24,9 @@ use yii\db\ActiveRecord;
* @property int $created_at
*
* Behaviors:
* @property Account|null $account
* @property Account|null $account
* @property OauthSession[] $sessions
* @property-read OauthRefreshToken[] $refreshTokens
*/
class OauthClient extends ActiveRecord {
@@ -31,7 +34,7 @@ class OauthClient extends ActiveRecord {
public const TYPE_MINECRAFT_SERVER = 'minecraft-server';
public static function tableName(): string {
return '{{%oauth_clients}}';
return 'oauth_clients';
}
public function behaviors(): array {
@@ -55,6 +58,10 @@ class OauthClient extends ActiveRecord {
return $this->hasMany(OauthSession::class, ['client_id' => 'id']);
}
public function getRefreshTokens(): ActiveQuery {
return $this->hasMany(OauthRefreshToken::class, ['client_id' => 'id']);
}
public static function find(): OauthClientQuery {
return Yii::createObject(OauthClientQuery::class, [static::class]);
}

View File

@@ -1,23 +0,0 @@
<?php
namespace common\models;
final class OauthOwnerType {
/**
* Used for sessions belonging directly to account.ely.by users
* who have performed password authentication and are using the web interface
*/
public const ACCOUNT = 'accounts';
/**
* Used when a user uses OAuth2 authorization_code protocol to allow an application
* to access and perform actions on its own behalf
*/
public const USER = 'user';
/**
* Used for clients authorized via OAuth2 client_credentials protocol
*/
public const CLIENT = 'client';
}

View File

@@ -0,0 +1,50 @@
<?php
declare(strict_types=1);
namespace common\models;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveQuery;
use yii\db\ActiveRecord;
/**
* Fields:
* @property string $id
* @property int $account_id
* @property int $client_id
* @property int $issued_at
*
* Relations:
* @property-read OauthSession $session
* @property-read Account $account
* @property-read OauthClient $client
*/
class OauthRefreshToken extends ActiveRecord {
public static function tableName(): string {
return 'oauth_refresh_tokens';
}
public function behaviors(): array {
return [
[
'class' => TimestampBehavior::class,
'createdAtAttribute' => 'issued_at',
'updatedAtAttribute' => false,
],
];
}
public function getSession(): ActiveQuery {
return $this->hasOne(OauthSession::class, ['account_id' => 'account_id', 'client_id' => 'client_id']);
}
public function getAccount(): ActiveQuery {
return $this->hasOne(Account::class, ['id' => 'account_id']);
}
public function getClient(): ActiveQuery {
return $this->hasOne(OauthClient::class, ['id' => 'client_id']);
}
}

View File

@@ -17,8 +17,9 @@ use yii\db\ActiveRecord;
* @property integer $created_at
*
* Relations:
* @property OauthClient $client
* @property Account $account
* @property-read OauthClient $client
* @property-read Account $account
* @property-read OauthRefreshToken[] $refreshTokens
*/
class OauthSession extends ActiveRecord {
@@ -43,6 +44,10 @@ class OauthSession extends ActiveRecord {
return $this->hasOne(Account::class, ['id' => 'owner_id']);
}
public function getRefreshTokens(): ActiveQuery {
return $this->hasMany(OauthRefreshToken::class, ['account_id' => 'account_id', 'client_id' => 'client_id']);
}
public function getScopes(): array {
if (empty($this->scopes) && $this->legacy_id !== null) {
return Yii::$app->redis->smembers($this->getLegacyRedisScopesKey());