1 | <?php |
||
27 | abstract class AuthorizationEndpoint implements MiddlewareInterface |
||
28 | { |
||
29 | /** |
||
30 | * @var UserAccountDiscoveryManager |
||
31 | */ |
||
32 | private $userAccountDiscoveryManager; |
||
33 | |||
34 | /** |
||
35 | * @var BeforeConsentScreenManager |
||
36 | */ |
||
37 | private $beforeConsentScreenManager; |
||
38 | |||
39 | /** |
||
40 | * @var AfterConsentScreenManager |
||
41 | */ |
||
42 | private $afterConsentScreenManager; |
||
43 | |||
44 | /** |
||
45 | * @var AuthorizationFactory |
||
46 | */ |
||
47 | private $authorizationFactory; |
||
48 | |||
49 | /** |
||
50 | * AuthorizationEndpoint constructor. |
||
51 | * |
||
52 | * @param AuthorizationFactory $authorizationFactory |
||
53 | * @param UserAccountDiscoveryManager $userAccountDiscoveryManager |
||
54 | * @param BeforeConsentScreenManager $beforeConsentScreenManager |
||
55 | * @param AfterConsentScreenManager $afterConsentScreenManager |
||
56 | */ |
||
57 | public function __construct(AuthorizationFactory $authorizationFactory, UserAccountDiscoveryManager $userAccountDiscoveryManager, BeforeConsentScreenManager $beforeConsentScreenManager, AfterConsentScreenManager $afterConsentScreenManager) |
||
64 | |||
65 | /** |
||
66 | * @param Authorization $authorization |
||
67 | * @param ServerRequestInterface $request |
||
68 | * |
||
69 | * @return ResponseInterface |
||
70 | */ |
||
71 | abstract protected function redirectToLoginPage(Authorization $authorization, ServerRequestInterface $request): ResponseInterface; |
||
72 | |||
73 | /** |
||
74 | * @param ServerRequestInterface $request |
||
75 | * @param Authorization $authorization |
||
76 | * |
||
77 | * @return ResponseInterface |
||
78 | */ |
||
79 | abstract protected function processConsentScreen(ServerRequestInterface $request, Authorization $authorization): ResponseInterface; |
||
80 | |||
81 | /** |
||
82 | * {@inheritdoc} |
||
83 | */ |
||
84 | public function process(ServerRequestInterface $request, RequestHandlerInterface $requestHandler) |
||
85 | { |
||
86 | try { |
||
87 | $authorization = $this->authorizationFactory->createAuthorizationFromRequest($request); |
||
88 | $authorization = $this->userAccountDiscoveryManager->find($request, $authorization); |
||
89 | |||
90 | if (null === $authorization->getUserAccount()) { |
||
91 | return $this->redirectToLoginPage($authorization, $request); |
||
92 | } |
||
93 | |||
94 | $authorization = $this->beforeConsentScreenManager->process($request, $authorization); |
||
95 | |||
96 | return $this->processConsentScreen($request, $authorization); |
||
97 | } catch (OAuth2Exception $e) { |
||
98 | $data = $e->getData(); |
||
99 | if (null !== $e->getAuthorization()) { |
||
100 | $redirectUri = $e->getAuthorization()->getRedirectUri(); |
||
101 | $responseMode = $e->getAuthorization()->getResponseMode(); |
||
102 | if (null !== $redirectUri && null !== $responseMode) { |
||
103 | $data['redirect_uri'] = $redirectUri; |
||
104 | $data['response_mode'] = $responseMode; |
||
105 | |||
106 | throw new OAuth2Exception(302, $data, $e->getAuthorization(), $e); |
||
107 | } |
||
108 | } |
||
109 | |||
110 | throw $e; |
||
111 | } catch (Exception\ProcessAuthorizationException $e) { |
||
112 | $authorization = $e->getAuthorization(); |
||
113 | $authorization = $this->afterConsentScreenManager->process($request, $authorization); |
||
114 | if ($authorization->isAuthorized() === false) { |
||
115 | $this->throwRedirectionException($authorization, OAuth2ResponseFactoryManager::ERROR_ACCESS_DENIED, 'The resource owner denied access to your client.'); |
||
116 | } |
||
117 | |||
118 | $responseTypeProcessor = ResponseTypeProcessor::create($authorization); |
||
119 | |||
120 | try { |
||
121 | $authorization = $responseTypeProcessor->process(); |
||
122 | } catch (OAuth2Exception $e) { |
||
123 | $this->throwRedirectionException($authorization, $e->getData()['error'], $e->getData()['error_description']); |
||
124 | } |
||
125 | |||
126 | return $this->buildResponse($authorization); |
||
127 | } catch (Exception\CreateRedirectionException $e) { |
||
128 | $this->throwRedirectionException($e->getAuthorization(), $e->getMessage(), $e->getDescription()); |
||
129 | } catch (Exception\ShowConsentScreenException $e) { |
||
130 | return $this->processConsentScreen($request, $e->getAuthorization()); |
||
131 | } catch (Exception\RedirectToLoginPageException $e) { |
||
132 | return $this->redirectToLoginPage($e->getAuthorization(), $request); |
||
133 | } |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * @param Authorization $authorization |
||
138 | * |
||
139 | * @throws OAuth2Exception |
||
140 | * |
||
141 | * @return ResponseInterface |
||
142 | */ |
||
143 | private function buildResponse(Authorization $authorization): ResponseInterface |
||
159 | |||
160 | /** |
||
161 | * @param Authorization $authorization |
||
162 | * @param string $error |
||
163 | * @param string $error_description |
||
164 | * |
||
165 | * @throws OAuth2Exception |
||
166 | */ |
||
167 | private function throwRedirectionException(Authorization $authorization, string $error, string $error_description) |
||
184 | } |
||
185 |