我正在尝试在yii2中实现filsh oauth2。但是我得到了
{
"name": "Exception",
"message": "Argument 1 passed to OAuth2\\GrantType\\ClientCredentials::__construct() must implement interface OAuth2\\Storage\\ClientCredentialsInterface, instance of common\\auth\\Identity given",
"code": 0,
"type": "TypeError",
"file": "C:\\xampp\\htdocs\\warehouse\\vendor\\bshaffer\\oauth2-server-php\\src\\OAuth2\\GrantType\\ClientCredentials.php",
"line": 25,
"stack-trace": [
"#0 [internal function]: OAuth2\\GrantType\\ClientCredentials->__construct(Object(common\\auth\\Identity), Array)",
"#1 C:\\xampp\\htdocs\\warehouse\\vendor\\filsh\\yii2-oauth2-server\\Module.php(95): ReflectionClass->newInstanceArgs(Array)",
"#2 C:\\xampp\\htdocs\\warehouse\\vendor\\filsh\\yii2-oauth2-server\\controllers\\RestController.php(25): filsh\\yii2\\oauth2server\\Module->getServer()",
"#3 [internal function]: filsh\\yii2\\oauth2server\\controllers\\RestController->actionToken()",
"#4 C:\\xampp\\htdocs\\warehouse\\vendor\\yiisoft\\yii2\\base\\InlineAction.php(57): call_user_func_array(Array, Array)",
"#5 C:\\xampp\\htdocs\\warehouse\\vendor\\yiisoft\\yii2\\base\\Controller.php(157): yii\\base\\InlineAction->runWithParams(Array)",
"#6 C:\\xampp\\htdocs\\warehouse\\vendor\\yiisoft\\yii2\\base\\Module.php(528): yii\\base\\Controller->runAction('token', Array)",
"#7 C:\\xampp\\htdocs\\warehouse\\vendor\\yiisoft\\yii2\\web\\Application.php(103): yii\\base\\Module->runAction('oauth2/rest/tok...', Array)",
"#8 C:\\xampp\\htdocs\\warehouse\\vendor\\yiisoft\\yii2\\base\\Application.php(386): yii\\web\\Application->handleRequest(Object(yii\\web\\Request))",
"#9 C:\\xampp\\htdocs\\warehouse\\api\\web\\index.php(17): yii\\base\\Application->run()",
"#10 {main}"
]
}
当我尝试发布我的数据以获取访问令牌时:
{"grant_type" => "password","username" => "indigo","password" => "qwerty","client_id" => "testclient", "client_secret" => "testpass"}
这是我的api / config / main.php
<?php
$params = array_merge(
require(__DIR__ . '/../../common/config/params.php'),
require(__DIR__ . '/../../common/config/params-local.php'),
require(__DIR__ . '/params.php'),
require(__DIR__ . '/params-local.php')
);
return [
'id' => 'app-api',
'basePath' => dirname(__DIR__),
'controllerNamespace' => 'api\controllers',
'bootstrap' => [
'log',
[
'class' => 'yii\filters\ContentNegotiator',
'formats' => [
'application/json' => 'json',
'application/xml' => 'xml',
],
],
],
'modules' => [
'oauth2' => [
'class' => 'filsh\yii2\oauth2server\Module',
'tokenParamName' => 'accessToken',
'tokenAccessLifetime' => 3600 * 24,
'storageMap' => [
'user_credentials' => 'common\auth\Identity',
],
'grantTypes' => [
'user_credentials' => [
'class' => 'OAuth2\GrantType\UserCredentials',
],
'refresh_token' => [
'class' => 'OAuth2\GrantType\RefreshToken',
'always_issue_new_refresh_token' => true
]
]
]
],
'components' => [
'request' => [
'parsers' => [
'application/json' => 'yii\web\JsonParser',
],
],
'response' => [
'formatters' => [
'json' => [
'class' => 'yii\web\JsonResponseFormatter',
'prettyPrint' => YII_DEBUG,
'encodeOptions' => JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE,
],
],
],
'user' => [
// 'identityClass' => 'common\models\User',
'identityClass' => 'common\auth\Identity',
'enableAutoLogin' => false,
'enableSession' => false,
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
'' => 'site/index',
'POST oauth2/<action:\w+>' => 'oauth2/rest/<action>',
'GET product' => 'product/index',
'GET client' => 'client/index',
// 'GET product/<id:\d+>' => 'product/view',
'GET user' => 'user/index',
'PUT,PATCH client' => 'client/update',
'PUT,PATCH product' => 'product/update',
'PUT,PATCH user' => 'user/update',
[
'class' => 'yii\rest\UrlRule',
'controller' => [
'product',
'client'
],
'pluralize' => false,
],
],
],
],
'as authenticator' => [
'class' => 'filsh\yii2\oauth2server\filters\auth\CompositeAuth',
'except' => ['site/index', 'oauth2/rest/token'],
'authMethods' => [
['class' => 'yii\filters\auth\HttpBearerAuth'],
['class' => 'yii\filters\auth\QueryParamAuth', 'tokenParam' => 'accessToken'],
]
],
'as access' => [
'class' => 'yii\filters\AccessControl',
'except' => ['site/index', 'oauth2/rest/token'],
'rules' => [
[
'allow' => true,
'roles' => ['@'],
],
],
],
'as exceptionFilter' => [
'class' => 'filsh\yii2\oauth2server\filters\ErrorToExceptionFilter',
],
'params' => $params,
];
和我的common / auth / Identity.php
<?php
namespace common\auth;
use filsh\yii2\oauth2server\Module;
use OAuth2\Storage\UserCredentialsInterface;
use common\models\Users;
use common\repositories\UserReadRepository;
use Yii;
use yii\web\IdentityInterface;
class Identity implements IdentityInterface, UserCredentialsInterface
{
private $user;
public function __construct(Users $user)
{
$this->user = $user;
}
public static function findIdentity($id)
{
$user = self::getRepository()->findActiveById($id);
return $user ? new self($user): null;
}
public static function findIdentityByAccessToken($token, $type = null)
{
$data = self::getOauth()->getServer()->getResourceController()->getToken();
return !empty($data['user_id']) ? static::findIdentity($data['user_id']) : null;
}
// public function getId()
public function getId(): int
{
return $this->user->id;
}
// public function getAuthKey()
public function getAuthKey(): string
{
return $this->user->auth_key;
}
// public function validateAuthKey($authKey)
public function validateAuthKey($authKey): bool
{
return $this->getAuthKey() === $authKey;
}
// public function checkUserCredentials($username, $password)
public function checkUserCredentials($username, $password): bool
{
if (!$user = self::getRepository()->findActiveByUsername($username)) {
return false;
}
return $user->validatePassword($password);
}
// public function getUserDetails($username)
public function getUserDetails($username): array
{
$user = self::getRepository()->findActiveByUsername($username);
return ['user_id' => $user->id];
}
// private static function getRepository()
private static function getRepository(): UserReadRepository
{
return Yii::$container->get(UserReadRepository::class);
}
// private static function getOauth()
private static function getOauth(): Module
{
return Yii::$app->getModule('oauth2');
}
}
还有common / models / User.php
<?php
namespace common\models;
use common\entities\AggregateRoot;
use Yii;
use yii\base\NotSupportedException;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;
use yii\db\ActiveQuery;
use common\entities\events\UserSignUpConfirmed;
use common\entities\events\UserSignUpRequested;
use common\entities\EventTrait;
/**
* User model
*
* @property integer $id
* @property string $username
* @property integer $role
* @property string $last_name
* @property integer $phone
* @property integer $count
* @property string $date
* @property string $auth_key
* @property string $password_hash
* @property string $password_reset_token
* @property string $email
* @property integer $status
* @property integer $created_at
* @property integer $updated_at
* @property string $password write-only password
*/
class Users extends ActiveRecord
{
const STATUS_DELETED = 0;
const STATUS_ACTIVE = 10;
/**
* @inheritdoc
*/
public static function tableName()
{
return '{{%users}}';
}
/**
* @inheritdoc
*/
public function behaviors()
{
return [
TimestampBehavior::className(),
];
}
/**
* @inheritdoc
*/
public function rules()
{
return [
['status', 'default', 'value' => self::STATUS_ACTIVE],
['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],
[['username'], 'required'],
[['role', 'phone', 'count', 'status', 'created_at', 'updated_at'], 'integer'],
[['date'], 'safe'],
[['username', 'password_hash', 'password_reset_token', 'email'], 'string', 'max' => 255],
[['last_name'], 'string', 'max' => 25],
[['auth_key'], 'string', 'max' => 32],
[['email'], 'unique'],
[['password_reset_token'], 'unique'],
];
}
/**
* @inheritdoc
*/
// public static function findIdentity($id)
// {
// return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]);
// }
/**
* @inheritdoc
*/
// public static function findIdentityByAccessToken($token, $type = null)
// {
//// throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
// return static::findOne(['auth_key' => $token]);
// }
/**
* Finds user by username
*
* @param string $username
* @return static|null
*/
public static function findByUsername($username)
{
return static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE]);
}
/**
* Finds user by password reset token
*
* @param string $token password reset token
* @return static|null
*/
public static function findByPasswordResetToken($token)
{
if (!static::isPasswordResetTokenValid($token)) {
return null;
}
return static::findOne([
'password_reset_token' => $token,
'status' => self::STATUS_ACTIVE,
]);
}
/**
* Finds out if password reset token is valid
*
* @param string $token password reset token
* @return bool
*/
public static function isPasswordResetTokenValid($token)
{
if (empty($token)) {
return false;
}
$timestamp = (int) substr($token, strrpos($token, '_') + 1);
$expire = Yii::$app->params['user.passwordResetTokenExpire'];
return $timestamp + $expire >= time();
}
/**
* @inheritdoc
*/
public function getId()
{
return $this->getPrimaryKey();
}
public function isActive(): bool
{
return $this->status === self::STATUS_ACTIVE;
}
/**
* @inheritdoc
*/
// public function getAuthKey()
// {
// return $this->auth_key;
// }
/**
* @inheritdoc
*/
// public function validateAuthKey($authKey)
// {
// return $this->getAuthKey() === $authKey;
// }
/**
* Validates password
*
* @param string $password password to validate
* @return bool if password provided is valid for current user
*/
public function validatePassword($password)
{
return Yii::$app->security->validatePassword($password, $this->password_hash);
}
/**
* Generates password hash from password and sets it to the model
*
* @param string $password
*/
public function setPassword($password)
{
$this->password_hash = Yii::$app->security->generatePasswordHash($password);
}
/**
* Generates "remember me" authentication key
*/
public function generateAuthKey()
{
$this->auth_key = Yii::$app->security->generateRandomString();
}
/**
* Generates new password reset token
*/
public function generatePasswordResetToken()
{
$this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
}
/**
* Removes password reset token
*/
public function removePasswordResetToken()
{
$this->password_reset_token = null;
}
// public function fields()
// {
// return [
// 'id' => 'id',
// 'last_name' => 'last_name',
// 'username' => 'username',
// 'email' => 'email',
// ];
// }
public function attributeLabels()
{
return [
'id' => 'ID',
'username' => 'Username',
'role' => 'Role',
'last_name' => 'Last Name',
'phone' => 'Phone',
'count' => 'Count',
'date' => 'Date',
'auth_key' => 'Auth Key',
'password_hash' => 'Password Hash',
'password_reset_token' => 'Password Reset Token',
'email' => 'Email',
'status' => 'Status',
'created_at' => 'Created At',
'updated_at' => 'Updated At',
];
}
}
UserRepository.php
<?php
namespace common\repositories;
//use common\dispatchers\EventDispatcher;
use common\models\Users;
class UserRepository
{
// private $dispatcher;
// public function __construct(EventDispatcher $dispatcher)
// {
// $this->dispatcher = $dispatcher;
// }
public function findByUsernameOrEmail($value): ?Users
{
return Users::find()->andWhere(['or', ['username' => $value], ['email' => $value]])->one();
}
public function get($id): Users
{
return $this->getBy(['id' => $id]);
}
public function getByEmailConfirmToken($token): Users
{
return $this->getBy(['email_confirm_token' => $token]);
}
public function getByEmail($email): Users
{
return $this->getBy(['email' => $email]);
}
public function getByPasswordResetToken($token): Users
{
return $this->getBy(['password_reset_token' => $token]);
}
public function existsByPasswordResetToken(string $token): bool
{
return (bool) Users::findByPasswordResetToken($token);
}
// public function save(Users $user): void
// {
// if (!$user->save()) {
// throw new \RuntimeException('Saving error.');
// }
// $this->dispatcher->dispatchAll($user-> releaseEvents());
// }
//
// public function remove(Users $user): void
// {
// if (!$user->delete()) {
// throw new \RuntimeException('Removing error.');
// }
// $this->dispatcher->dispatchAll($user->releaseEvents());
// }
private function getBy(array $condition): Users
{
if (!$user = Users::find()->andWhere($condition)->limit(1)->one()) {
throw new NotFoundException('User not found.');
}
return $user;
}
}
我无法找到解决此问题的方法,或者可能有另一种解决方法(不分割用户权限)
如果我将oauth2添加到&#34;组件&#34;数组然后我有
<response>
<name>Exception</name>
<message>Call to a member function getServer() on null</message>
<code>0</code>
<type>Error</type>
<file>
D:\OSPanel\domains\warehouse.elisDN\vendor\filsh\yii2-oauth2-server\filters\auth\CompositeAuth.php
</file>
<line>14</line>
<stack-trace>
<item>
#0 D:\OSPanel\domains\warehouse.elisDN\vendor\yiisoft\yii2\base\ActionFilter.php(75): filsh\yii2\oauth2server\filters\auth\CompositeAuth->beforeAction(Object(yii\rest\IndexAction))
</item>
<item>
#1 [internal function]: yii\base\ActionFilter->beforeFilter(Object(yii\base\ActionEvent))
</item>
<item>
#2 D:\OSPanel\domains\warehouse.elisDN\vendor\yiisoft\yii2\base\Component.php(557): call_user_func(Array, Object(yii\base\ActionEvent))
</item>
<item>
#3 D:\OSPanel\domains\warehouse.elisDN\vendor\yiisoft\yii2\base\Module.php(682): yii\base\Component->trigger('beforeAction', Object(yii\base\ActionEvent))
</item>
<item>
#4 D:\OSPanel\domains\warehouse.elisDN\vendor\yiisoft\yii2\base\Controller.php(145): yii\base\Module->beforeAction(Object(yii\rest\IndexAction))
</item>
<item>
#5 D:\OSPanel\domains\warehouse.elisDN\vendor\yiisoft\yii2\base\Module.php(528): yii\base\Controller->runAction('index', Array)
</item>
<item>
#6 D:\OSPanel\domains\warehouse.elisDN\vendor\yiisoft\yii2\web\Application.php(103): yii\base\Module->runAction('client/index', Array)
</item>
<item>
#7 D:\OSPanel\domains\warehouse.elisDN\vendor\yiisoft\yii2\base\Application.php(386): yii\web\Application->handleRequest(Object(yii\web\Request))
</item>
<item>
#8 D:\OSPanel\domains\warehouse.elisDN\api\web\index.php(17): yii\base\Application->run()
</item>
<item>#9 {main}</item>
</stack-trace>
</response>
这里是配置文件
<?php
$params = array_merge(
require(__DIR__ . '/../../common/config/params.php'),
require(__DIR__ . '/../../common/config/params-local.php'),
require(__DIR__ . '/params.php'),
require(__DIR__ . '/params-local.php')
);
return [
'id' => 'app-api',
'basePath' => dirname(__DIR__),
'controllerNamespace' => 'api\controllers',
'bootstrap' => [
'log',
[
'class' => 'yii\filters\ContentNegotiator',
'formats' => [
'application/json' => 'json',
'application/xml' => 'xml',
],
],
],
'modules' => [],
///api/web/oauth2/token
'components' => [
'oauth2' => [
'class' => 'filsh\yii2\oauth2server\Module',
'tokenParamName' => 'accessToken',
'tokenAccessLifetime' => 3600 * 24,
'storageMap' => [
'user_credentials' => 'common\auth\Identity',
],
'grantTypes' => [
'user_credentials' => [
'class' => 'OAuth2\GrantType\UserCredentials',
],
'refresh_token' => [
'class' => 'OAuth2\GrantType\RefreshToken',
'always_issue_new_refresh_token' => true
]
]
],
'request' => [
'parsers' => [
'application/json' => 'yii\web\JsonParser',
// 'application/xml' => 'yii\web\XmlParser',
],
],
'response' => [
'formatters' => [
'json' => [
'class' => 'yii\web\JsonResponseFormatter',
'prettyPrint' => YII_DEBUG,
'encodeOptions' => JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE,
],
],
],
'user' => [
// 'identityClass' => 'common\models\User',
'identityClass' => 'common\auth\Identity',
'enableAutoLogin' => false,
'enableSession' => false,
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
'' => 'site/index',
'POST oauth2/<action:\w+>' => 'oauth2/rest/<action>',
'GET product' => 'product/index',
'GET client' => 'client/index',
// 'GET product/<id:\d+>' => 'product/view',
'GET user' => 'user/index',
'PUT,PATCH client' => 'client/update',
'PUT,PATCH product' => 'product/update',
'PUT,PATCH user' => 'user/update',
[
'class' => 'yii\rest\UrlRule',
'controller' => [
'product',
'client'
],
'pluralize' => false,
],
],
],
],
'as authenticator' => [
'class' => 'filsh\yii2\oauth2server\filters\auth\CompositeAuth',
'except' => ['site/index', 'oauth2/rest/token'],
'authMethods' => [
['class' => 'yii\filters\auth\HttpBearerAuth'],
['class' => 'yii\filters\auth\QueryParamAuth', 'tokenParam' => 'accessToken'],
]
],
'as access' => [
'class' => 'yii\filters\AccessControl',
'except' => ['site/index', 'oauth2/rest/token'],
'rules' => [
[
'allow' => true,
'roles' => ['@'],
],
],
],
'as exceptionFilter' => [
'class' => 'filsh\yii2\oauth2server\filters\ErrorToExceptionFilter',
],
'params' => $params,
];
答案 0 :(得分:1)
问题在于您的配置。您必须将oauth2配置部分包含在components数组中,而不是包含在modules数组中。