Help grabbing container


#1

Hi,

I’m trying to get a middleware to work, in my dependencies i an oAuth is being attached into my container, and i don’t know how to retrieve it, with the following code i get the error Call to a member function getContainer() on null

Code:

$oathMiddleWare = function ($request,$response,$next){

$container = $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;

};


#2

$this->$app->getContainer();

Helps me get past that error and now i get a

Type: Slim\Exception\ContainerValueNotFoundException
Message: Identifier “” is not defined.

What am i doing wrong? i am trying to change an oAuth controller to oAuth being a middleware. the dependency is being configured like this.

$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’));
};

And the controller was like this:

public function validateToken($request)
{

    // 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 also works
public function getAll(Request $request, Response $response, $args) {
if ($this->validateToken($request)) {
parent::getAll($request, $response, $args);

	}    		
}