1 | <?php |
||
32 | abstract class AuthorizationEndpoint extends AbstractEndpoint |
||
33 | { |
||
34 | private $authorizationRequestLoader; |
||
35 | |||
36 | private $parameterCheckerManager; |
||
37 | |||
38 | private $userAccountDiscovery; |
||
39 | |||
40 | private $userCheckerManager; |
||
41 | |||
42 | private $consentRepository; |
||
43 | |||
44 | public function __construct(ResponseFactory $responseFactory, AuthorizationRequestLoader $authorizationRequestLoader, ParameterCheckerManager $parameterCheckerManager, UserAccountDiscovery $userAccountDiscovery, UserAuthenticationCheckerManager $userCheckerManager, SessionInterface $session, ?ConsentRepository $consentRepository) |
||
45 | { |
||
46 | parent::__construct($responseFactory, $session); |
||
47 | $this->authorizationRequestLoader = $authorizationRequestLoader; |
||
48 | $this->parameterCheckerManager = $parameterCheckerManager; |
||
49 | $this->userAccountDiscovery = $userAccountDiscovery; |
||
50 | $this->userCheckerManager = $userCheckerManager; |
||
51 | $this->consentRepository = $consentRepository; |
||
52 | } |
||
53 | |||
54 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
||
55 | { |
||
56 | $authorization = $this->loadAuthorization($request); |
||
57 | |||
58 | try { |
||
59 | $userAccount = $this->userAccountDiscovery->getCurrentAccount(); |
||
60 | |||
61 | if (null !== $userAccount) { |
||
62 | return $this->processWithAuthenticatedUser($authorization, $userAccount); |
||
63 | } else { |
||
64 | return $this->processWithUnauthenticatedUser($authorization); |
||
65 | } |
||
66 | } catch (OAuth2AuthorizationException $e) { |
||
67 | throw $e; |
||
68 | } catch (OAuth2Error $e) { |
||
69 | throw new OAuth2AuthorizationException($e->getMessage(), $e->getErrorDescription(), $authorization, $e); |
||
70 | } catch (\Exception $e) { |
||
71 | throw new OAuth2AuthorizationException(OAuth2Error::ERROR_INVALID_REQUEST, $e->getMessage(), $authorization, $e); |
||
72 | } |
||
73 | } |
||
74 | |||
75 | private function processWithAuthenticatedUser(AuthorizationRequest $authorization, UserAccount $userAccount): ResponseInterface |
||
76 | { |
||
77 | $authorization->setUserAccount($userAccount); |
||
78 | $isAuthenticationNeeded = $this->userCheckerManager->isAuthenticationNeeded($authorization); |
||
79 | $isConsentNeeded = !$this->consentRepository || !$this->consentRepository->hasConsentBeenGiven($authorization); |
||
80 | |||
81 | switch (true) { |
||
82 | case $authorization->hasPrompt('none'): |
||
83 | if ($isConsentNeeded) { |
||
84 | throw new OAuth2AuthorizationException(OAuth2Error::ERROR_INTERACTION_REQUIRED, 'The resource owner consent is required.', $authorization); |
||
85 | } |
||
86 | $authorization->allow(); |
||
87 | $routeName = 'oauth2_server_process_endpoint'; |
||
88 | break; |
||
89 | case $authorization->hasPrompt('select_account'): |
||
90 | $routeName = 'oauth2_server_select_account_endpoint'; |
||
91 | break; |
||
92 | case $authorization->hasPrompt('login') || $isAuthenticationNeeded: |
||
93 | $routeName = 'oauth2_server_login_endpoint'; |
||
94 | break; |
||
95 | case $authorization->hasPrompt('consent') || $isConsentNeeded: |
||
96 | $routeName = 'oauth2_server_consent_endpoint'; |
||
97 | break; |
||
98 | default: |
||
99 | $routeName = 'oauth2_server_consent_endpoint'; |
||
100 | break; |
||
101 | } |
||
102 | |||
103 | $authorizationId = Base64Url::encode(random_bytes(64)); |
||
104 | $this->saveAuthorization($authorizationId, $authorization); |
||
105 | $redirectTo = $this->getRouteFor($routeName, $authorizationId); |
||
106 | |||
107 | return $this->createRedirectResponse($redirectTo); |
||
108 | } |
||
109 | |||
110 | private function processWithUnauthenticatedUser(AuthorizationRequest $authorization): ResponseInterface |
||
111 | { |
||
112 | if ($authorization->hasPrompt('none')) { |
||
113 | $isConsentNeeded = !$this->consentRepository || !$this->consentRepository->hasConsentBeenGiven($authorization); |
||
114 | if ($isConsentNeeded) { |
||
115 | throw new OAuth2AuthorizationException(OAuth2Error::ERROR_LOGIN_REQUIRED, 'The resource owner is not logged in.', $authorization); |
||
116 | } |
||
117 | $authorization->allow(); |
||
118 | $routeName = 'oauth2_server_process_endpoint'; |
||
119 | } else { |
||
120 | $routeName = 'oauth2_server_login_endpoint'; |
||
121 | } |
||
122 | |||
123 | $authorizationId = Base64Url::encode(random_bytes(64)); |
||
124 | $this->saveAuthorization($authorizationId, $authorization); |
||
125 | $redirectTo = $this->getRouteFor($routeName, $authorizationId); |
||
126 | |||
127 | return $this->createRedirectResponse($redirectTo); |
||
128 | } |
||
129 | |||
130 | private function loadAuthorization(ServerRequestInterface $request): AuthorizationRequest |
||
145 | |||
146 | abstract protected function getRouteFor(string $action, string $authorizationId): string; |
||
147 | } |
||
148 |