mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Попытка реализовать отдельный компонент для oAuth авторизации в свой же API. Не тестировал, не проверял работу, просто пушнул, чтобы потом продолжить в дргуом месте.
This commit is contained in:
22
api/components/ApiUser/AuthChecker.php
Normal file
22
api/components/ApiUser/AuthChecker.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
namespace api\components\ApiUser;
|
||||
|
||||
use common\models\OauthAccessToken;
|
||||
use yii\rbac\CheckAccessInterface;
|
||||
|
||||
class AuthChecker implements CheckAccessInterface {
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function checkAccess($token, $permissionName, $params = []) : bool {
|
||||
/** @var OauthAccessToken|null $accessToken */
|
||||
$accessToken = OauthAccessToken::findOne($token);
|
||||
if ($accessToken === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $accessToken->getScopes()->exists($permissionName);
|
||||
}
|
||||
|
||||
}
|
19
api/components/ApiUser/Component.php
Normal file
19
api/components/ApiUser/Component.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
namespace api\components\ApiUser;
|
||||
|
||||
use yii\web\User as YiiUserComponent;
|
||||
|
||||
/**
|
||||
* @property Identity|null $identity
|
||||
*
|
||||
* @method Identity|null getIdentity()
|
||||
*/
|
||||
class Component extends YiiUserComponent {
|
||||
|
||||
public $identity = Identity::class;
|
||||
|
||||
public $enableSession = false;
|
||||
|
||||
public $loginUrl = null;
|
||||
|
||||
}
|
81
api/components/ApiUser/Identity.php
Normal file
81
api/components/ApiUser/Identity.php
Normal file
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
namespace api\components\ApiUser;
|
||||
|
||||
use common\models\Account;
|
||||
use common\models\OauthAccessToken;
|
||||
use common\models\OauthClient;
|
||||
use common\models\OauthSession;
|
||||
use yii\base\NotSupportedException;
|
||||
use yii\web\IdentityInterface;
|
||||
use yii\web\UnauthorizedHttpException;
|
||||
|
||||
/**
|
||||
* @property Account $account
|
||||
* @property OauthClient $client
|
||||
* @property OauthSession $session
|
||||
* @property OauthAccessToken $accessToken
|
||||
*/
|
||||
class Identity implements IdentityInterface {
|
||||
|
||||
/**
|
||||
* @var OauthAccessToken
|
||||
*/
|
||||
private $_accessToken;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public static function findIdentityByAccessToken($token, $type = null) {
|
||||
/** @var OauthAccessToken|null $model */
|
||||
$model = OauthAccessToken::findOne($token);
|
||||
if ($model === null) {
|
||||
throw new UnauthorizedHttpException('Incorrect token');
|
||||
} elseif ($model->isExpired()) {
|
||||
throw new UnauthorizedHttpException('Token expired');
|
||||
}
|
||||
|
||||
return new static($model);
|
||||
}
|
||||
|
||||
private function __construct(OauthAccessToken $accessToken) {
|
||||
$this->_accessToken = $accessToken;
|
||||
}
|
||||
|
||||
public function getAccount() : Account {
|
||||
return $this->getSession()->account;
|
||||
}
|
||||
|
||||
public function getClient() : OauthClient {
|
||||
return $this->getSession()->client;
|
||||
}
|
||||
|
||||
public function getSession() : OauthSession {
|
||||
return $this->_accessToken->session;
|
||||
}
|
||||
|
||||
public function getAccessToken() : OauthAccessToken {
|
||||
return $this->_accessToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* Этот метод используется для получения пользователя, к которому привязаны права.
|
||||
* У нас права привязываются к токенам, так что возвращаем именно его id.
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getId() {
|
||||
return $this->_accessToken->access_token;
|
||||
}
|
||||
|
||||
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');
|
||||
}
|
||||
|
||||
}
|
@@ -26,6 +26,12 @@ use yii\web\User as YiiUserComponent;
|
||||
*/
|
||||
class Component extends YiiUserComponent {
|
||||
|
||||
public $enableSession = false;
|
||||
|
||||
public $loginUrl = null;
|
||||
|
||||
public $identityClass = AccountIdentity::class;
|
||||
|
||||
public $secret;
|
||||
|
||||
public $expirationTimeout = 3600; // 1h
|
||||
|
Reference in New Issue
Block a user