【发布时间】:2018-08-09 19:31:42
【问题描述】:
我正在使用This 库来实现一个带有 Oauth 的 API,我想在某些路由中添加中间件,但我希望 ouath 在其他中间件之前运行,所以如果我错了,请纠正我,但方法要做到这一点是通过使 oAuth 成为中间件而不是控制器的一部分。
实际的设置是这样的。
依赖文件是这样的
$container['oAuth'] = function ($c) {
$storage = new App\DataAccess\_oAuth2_CustomStorage($c->get('pdo'));
// Pass a storage object or array of storage objects to the OAuth2 server class
$server = new OAuth2\Server($storage);
// add grant types
$server->addGrantType(new OAuth2\GrantType\UserCredentials($storage));
$server->addGrantType(new OAuth2\GrantType\ClientCredentials($storage));
$server->addGrantType(new OAuth2\GrantType\RefreshToken($storage));
return $server;
};
$container['App\Controllers\_Controller_oAuth2'] = function ($c) {
return new _Controller_oAuth2($c->get('logger'), $c->get('App\DataAccess\_DataAccess'), $c->get('oAuth'));
};
而实际的控制器是这样的:
public function __construct(LoggerInterface $logger, _DataAccess $dataaccess, $server)
{
parent::__construct($logger,$dataaccess);
$this->oAuth2server = $server;
}
/**
* @param \Psr\Http\Message\ServerRequestInterface $request
* @param \Psr\Http\Message\ResponseInterface $response
* @param array $next
*
* @return \Psr\Http\Message\ResponseInterface
*/
public function validateToken($request)
{
$this->logger->info(substr(strrchr(rtrim(__CLASS__, '\\'), '\\'), 1).': '.__FUNCTION__);
// convert a request from PSR7 to hhtpFoundation
$httpFoundationFactory = new HttpFoundationFactory();
$symfonyRequest = $httpFoundationFactory->createRequest($request);
$bridgeRequest = BridgeRequest::createFromRequest($symfonyRequest);
$token = $this->oAuth2server->getAccessTokenData($bridgeRequest);
if (!$this->oAuth2server->verifyResourceRequest($bridgeRequest)) {
$this->oAuth2server->getResponse()->send();
die;
}
// store the user_id
$token = $this->oAuth2server->getAccessTokenData($bridgeRequest);
$this->user = $token['user_id'];
return TRUE;
}
// needs an oAuth2 Client credentials grant
// with Resource owner credentials grant alseo works
public function getAll(Request $request, Response $response, $args) {
if ($this->validateToken($request)) {
parent::getAll($request, $response, $args);
}
}
现在,我尝试在我的中间件文件上创建我的函数以将其添加到所需的路由中,它看起来像这样:
$oathMiddleWare = function ($request,$response,$next){
$container = $this->$app->getContainer();
$responsen = $response->withHeader('Content-Type', 'application/json');
$this->oAuth2server = $container->get('oAuth');
$httpFoundationFactory = new HttpFoundationFactory();
$symfonyRequest = $httpFoundationFactory->createRequest($request);
$bridgeRequest = BridgeRequest::createFromRequest($symfonyRequest);
$token = $this->oAuth2server->getAccessTokenData($bridgeRequest);
if (!$this->oAuth2server->verifyResourceRequest($bridgeRequest)) {
$this->oAuth2server->getResponse()->send();
$responsen = $responsen ->withStatus(400);
return $responsen;
}
// store the user_id
$token = $this->oAuth2server->getAccessTokenData($bridgeRequest);
$this->user = $token['user_id'];
$next($request,$responsen);
return $responsen;
};
我第一次尝试$container = $app->getContainer();,但它给了我一个错误说Call to a member function getContainer() on null
现在使用我刚刚分享的代码,我收到一个错误Type: Slim\Exception\ContainerValueNotFoundException
Message: Identifier “” is not defined.
对完成这项工作有什么建议吗?
【问题讨论】:
-
您设法解决了这个问题吗?我相信您应该将中间件中的容器引用为
$this->getContainer()。
标签: php slim middleware slim-3