Using ZF2 Oauth2

2019-05-31 08:08发布

问题:

I'm trying to get https://github.com/zfcampus/zf-oauth2 working with my Application (mainly because I have installed apigility and zf-oauth2 comes with it).

I'm reading the very last section and it says to protect, I just simply use the following code (for instance, at the top of a controller):

if (!$this->server->verifyResourceRequest(OAuth2Request::createFromGlobals())) {
    // Not authorized return 401 error
    $this->getResponse()->setStatusCode(401);
    return;
}
// where $this->server is an instance of OAuth2\Server (see the AuthController.php).

However, $this->server has to be injected somehow. But, I can't seem to find how and what to inject. By clicking on the link to see AuthController.php, I get a page out found...

Edit: Thanks to Tom and Ujjwal, I think I am one step closer.

In my controller, now I have the following:

use ZF\OAuth2\Controller\AuthController;

class BaseController extends AuthController
{

}

In my Module.php, I try injecting OAuth2Server as such:

public function getServiceConfig()
{
    return array(
       'factories' => array(
            '\Stand\Controller\BaseController' =>  function ($sm) {
                $cls = new BaseController($sm->get('ZF\OAuth2\Service\OAuth2Server'));
                return $cls;
            },
        )
    );
}

But, when I tried to render the page, it is not catching my inject. I get

Catchable fatal error: Argument 1 passed to ZF\OAuth2\Controller\AuthController::__construct() must be an instance of OAuth2\Server

Please advice!

Thanks

回答1:

You can create a factory in this way:

module.config.php

<?php
return array(
    'controllers' => array(
        'factories' => array(
            'Test\Controller\Test' => function($sm) {
                $locator = $sm->getServiceLocator();
                $server = $locator->get('ZF\OAuth2\Service\OAuth2Server');
                $provider = $locator->get('ZF\OAuth2\Provider\UserId');
                return new Test\Controller\TestController($server, $provider);
            }
        )
    ),
...

And then you can use it in your controller class:

<?php
namespace Test\Controller;

use ZF\OAuth2\Controller\AuthController;
use ZF\OAuth2\Provider\UserId\UserIdProviderInterface;
use Zend\View\Model\JsonModel

class TestController extends AuthController {

    public function __construct($serverFactory, UserIdProviderInterface $userIdProvider) {
        parent::__construct($serverFactory, $userIdProvider);                
    }

    public function indexAction() { 

        $server = call_user_func($this->serverFactory, "oauth");        

        if (! $server->verifyResourceRequest($this->getOAuth2Request())) {
            $response   = $server->getResponse();
            return $this->getApiProblemResponse($response);
        } 

        return new JsonModel(array("Hello" => "World!"));
    }    

}

Reference: https://framework.zend.com/manual/2.1/en/modules/zend.service-manager.quick-start.html