|
5 | 5 | use Zend\Mvc\Controller\AbstractActionController; |
6 | 6 | use Zend\View\Model\JsonModel; |
7 | 7 | use OAuth2\ZendHttpPhpEnvironmentBridge\Request; |
| 8 | +use OAuth2\Server; |
8 | 9 |
|
9 | 10 | class AuthorizeController extends AbstractActionController |
10 | 11 | { |
11 | 12 | protected $userId; |
12 | 13 | protected $preAuthorized = false; |
13 | 14 |
|
| 15 | + private $server; |
| 16 | + private $authorizeForm; |
| 17 | + |
| 18 | + public function __construct(Server $server, $authorizeForm) |
| 19 | + { |
| 20 | + $this->server = $server; |
| 21 | + $this->authorizeForm = $authorizeForm; |
| 22 | + } |
| 23 | + |
14 | 24 | public function authorizeAction() |
15 | 25 | { |
16 | 26 | $this->getEventManager()->trigger('authorize.pre', $this); |
17 | 27 |
|
18 | | - $serviceManager = $this->getServiceLocator(); |
19 | | - $request = Request::createFromRequest($this->getRequest()); |
| 28 | + $request = Request::createFromRequest($this->getRequest()); |
20 | 29 | $response = $this->getResponse(); |
21 | | - $server = $serviceManager->get('OAuth2Server\Server'); |
22 | 30 |
|
23 | 31 | if ($this->preAuthorized) { |
24 | 32 | $isAuthorized = true; |
25 | 33 | } else { |
26 | | - $form = $serviceManager->get('OAuth2Server\AuthorizeForm'); |
27 | | - $form->setData($request->getQuery()); |
28 | | - if ($request->getQuery('authorize') && $form->isValid()) { |
| 34 | + $this->authorizeForm->setData($request->getQuery()); |
| 35 | + if ($request->getQuery('authorize') && $this->authorizeForm->isValid()) { |
29 | 36 | $isAuthorized = true; |
30 | | - } elseif ($request->getQuery('deny') && $form->isValid()) { |
| 37 | + } elseif ($request->getQuery('deny') && $this->authorizeForm->isValid()) { |
31 | 38 | $isAuthorized = false; |
32 | 39 | } |
33 | 40 | } |
34 | 41 | if (isset($isAuthorized)) { |
35 | | - $this->getEventManager()->trigger('authorize.preHandle', $this, ['isAuthorized' => $isAuthorized, 'preAuthorized' => $this->preAuthorized]); |
36 | | - |
37 | | - $response = $server->handleAuthorizeRequest($request, $this->getResponse(), $isAuthorized, $this->userId); |
| 42 | + $this->getEventManager()->trigger( |
| 43 | + 'authorize.preHandle', |
| 44 | + $this, |
| 45 | + ['isAuthorized' => $isAuthorized, 'preAuthorized' => $this->preAuthorized] |
| 46 | + ); |
| 47 | + |
| 48 | + $response = $this->server->handleAuthorizeRequest( |
| 49 | + $request, |
| 50 | + $this->getResponse(), |
| 51 | + $isAuthorized, |
| 52 | + $this->userId |
| 53 | + ); |
38 | 54 | $response->sendHeaders(); |
39 | 55 | return new JsonModel($response->getContent()); |
40 | 56 | } |
41 | 57 |
|
42 | | - if (!$server->validateAuthorizeRequest($request, $response)) { |
43 | | - $headers = $response->getHeaders(); |
| 58 | + if (!$this->server->validateAuthorizeRequest($request, $response)) { |
| 59 | + $headers = $response->getHeaders(); |
44 | 60 | $location = $headers->get('location'); |
45 | 61 | if ($location) { |
46 | 62 | $headers->removeHeader($location); |
47 | 63 | } |
48 | 64 | return new JsonModel($response->getContent()); |
49 | 65 | } |
50 | 66 |
|
51 | | - $form->setData($request->getQuery()); |
52 | | - $client = $server->getStorage('client')->getClientDetails($request->getQuery('client_id')); |
| 67 | + $this->authorizeForm->setData($request->getQuery()); |
| 68 | + $client = $this->server->getStorage('client')->getClientDetails($request->getQuery('client_id')); |
53 | 69 |
|
54 | 70 | $this->getEventManager()->trigger('authorize.post', $this); |
55 | 71 |
|
56 | | - return ['form' => $form, 'appname' => $client['name']]; |
| 72 | + return ['form' => $this->authorizeForm, 'appname' => $client['name']]; |
57 | 73 | } |
58 | 74 |
|
59 | 75 | public function tokenAction() |
60 | 76 | { |
61 | | - $server = $this->getServiceLocator()->get('OAuth2Server\Server'); |
62 | | - $request = Request::createFromRequest($this->getRequest()); |
63 | | - $response = $server->handleTokenRequest($request, $this->getResponse()); |
| 77 | + $request = Request::createFromRequest($this->getRequest()); |
| 78 | + $response = $this->server->handleTokenRequest($request, $this->getResponse()); |
64 | 79 |
|
65 | 80 | return new JsonModel($response->getContent()); |
66 | 81 | } |
|
0 commit comments