This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * Copyright (C) 2016 SURFnet. |
||
4 | * |
||
5 | * This program is free software: you can redistribute it and/or modify |
||
6 | * it under the terms of the GNU Affero General Public License as |
||
7 | * published by the Free Software Foundation, either version 3 of the |
||
8 | * License, or (at your option) any later version. |
||
9 | * |
||
10 | * This program is distributed in the hope that it will be useful, |
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
13 | * GNU Affero General Public License for more details. |
||
14 | * |
||
15 | * You should have received a copy of the GNU Affero General Public License |
||
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
17 | */ |
||
18 | |||
19 | namespace fkooman\RemoteStorage\OAuth; |
||
20 | |||
21 | use DateInterval; |
||
22 | use DateTime; |
||
23 | use fkooman\RemoteStorage\Http\Exception\HttpException; |
||
24 | use fkooman\RemoteStorage\Http\HtmlResponse; |
||
25 | use fkooman\RemoteStorage\Http\RedirectResponse; |
||
26 | use fkooman\RemoteStorage\Http\Request; |
||
27 | use fkooman\RemoteStorage\RandomInterface; |
||
28 | use fkooman\RemoteStorage\TplInterface; |
||
29 | |||
30 | class OAuthModule |
||
31 | { |
||
32 | /** @var \fkooman\RemoteStorage\TplInterface */ |
||
33 | private $tpl; |
||
34 | |||
35 | /** @var \fkooman\RemoteStorage\RandomInterface */ |
||
36 | private $random; |
||
37 | |||
38 | /** @var TokenStorage */ |
||
39 | private $tokenStorage; |
||
40 | |||
41 | /** @var \DateTime */ |
||
42 | private $dateTime; |
||
43 | |||
44 | /** @var int */ |
||
45 | private $expiresIn = 7776000; /* 90 days */ |
||
46 | |||
47 | public function __construct(TplInterface $tpl, TokenStorage $tokenStorage, RandomInterface $random, DateTime $dateTime) |
||
48 | { |
||
49 | $this->tpl = $tpl; |
||
50 | $this->tokenStorage = $tokenStorage; |
||
51 | $this->random = $random; |
||
52 | $this->dateTime = $dateTime; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @param int $expiresIn |
||
57 | */ |
||
58 | public function setExpiresIn($expiresIn) |
||
59 | { |
||
60 | $this->expiresIn = (int) $expiresIn; |
||
61 | } |
||
62 | |||
63 | public function getAuthorize(Request $request, $userId) |
||
64 | { |
||
65 | $this->validateRequest($request); |
||
66 | $this->validateClient($request); |
||
67 | |||
68 | // ask for approving this client/scope |
||
69 | return new HtmlResponse( |
||
70 | $this->tpl->render( |
||
71 | 'authorize', |
||
72 | [ |
||
73 | 'user_id' => $userId, |
||
74 | 'client_id' => $request->getQueryParameter('client_id'), |
||
75 | 'scope' => $request->getQueryParameter('scope'), |
||
76 | 'redirect_uri' => $request->getQueryParameter('redirect_uri'), |
||
77 | ] |
||
78 | ) |
||
79 | ); |
||
80 | } |
||
81 | |||
82 | public function postAuthorize(Request $request, $userId) |
||
83 | { |
||
84 | $this->validateRequest($request); |
||
85 | $this->validateClient($request); |
||
86 | |||
87 | // state is OPTIONAL in remoteStorage specification |
||
88 | $state = $request->getQueryParameter('state', false, null); |
||
89 | $returnUriPattern = '%s#%s'; |
||
90 | |||
91 | if ('no' === $request->getPostParameter('approve')) { |
||
92 | $redirectParameters = [ |
||
93 | 'error' => 'access_denied', |
||
94 | 'error_description' => 'user refused authorization', |
||
95 | ]; |
||
96 | if (!is_null($state)) { |
||
97 | $redirectParameters['state'] = $state; |
||
98 | } |
||
99 | $redirectQuery = http_build_query($redirectParameters); |
||
100 | |||
101 | $redirectUri = sprintf($returnUriPattern, $request->getQueryParameter('redirect_uri'), $redirectQuery); |
||
102 | |||
103 | return new RedirectResponse($redirectUri, 302); |
||
104 | } |
||
105 | |||
106 | $accessToken = $this->getAccessToken( |
||
107 | $userId, |
||
108 | $request->getQueryParameter('client_id'), |
||
109 | $request->getQueryParameter('scope') |
||
110 | ); |
||
111 | |||
112 | // add access_token, expires_in (and optionally state) to redirect_uri |
||
113 | $redirectParameters = [ |
||
114 | 'access_token' => $accessToken, |
||
115 | 'expires_in' => $this->expiresIn, |
||
116 | ]; |
||
117 | if (!is_null($state)) { |
||
118 | $redirectParameters['state'] = $state; |
||
119 | } |
||
120 | $redirectQuery = http_build_query($redirectParameters); |
||
121 | $redirectUri = sprintf($returnUriPattern, $request->getQueryParameter('redirect_uri'), $redirectQuery); |
||
122 | |||
123 | return new RedirectResponse($redirectUri, 302); |
||
124 | } |
||
125 | |||
126 | private function getAccessToken($userId, $clientId, $scope) |
||
127 | { |
||
128 | $existingToken = $this->tokenStorage->getExistingToken( |
||
129 | $userId, |
||
130 | $clientId, |
||
131 | $scope |
||
132 | ); |
||
133 | |||
134 | if (false !== $existingToken && $this->dateTime < new DateTime($existingToken['expires_at'])) { |
||
135 | // if the user already has an access_token for this client and |
||
136 | // scope, reuse it |
||
137 | $accessTokenKey = $existingToken['access_token_key']; |
||
138 | $accessToken = $existingToken['access_token']; |
||
139 | } else { |
||
140 | // generate a new one |
||
141 | $accessTokenKey = $this->random->get(16); |
||
142 | $accessToken = $this->random->get(16); |
||
143 | $expiresAt = date_add(clone $this->dateTime, new DateInterval(sprintf('PT%dS', $this->expiresIn))); |
||
144 | |||
145 | // store it |
||
146 | $this->tokenStorage->store( |
||
147 | $userId, |
||
148 | $accessTokenKey, |
||
149 | $accessToken, |
||
150 | $clientId, |
||
151 | $scope, |
||
152 | $expiresAt |
||
153 | ); |
||
154 | } |
||
155 | |||
156 | return sprintf('%s.%s', $accessTokenKey, $accessToken); |
||
157 | } |
||
158 | |||
159 | private function validateRequest(Request $request) |
||
160 | { |
||
161 | // we enforce that all parameter are set, nothing is "OPTIONAL" |
||
162 | $clientId = $request->getQueryParameter('client_id'); |
||
163 | if (1 !== preg_match('/^[\x20-\x7E]+$/', $clientId)) { |
||
164 | throw new HttpException('invalid client_id', 400); |
||
165 | } |
||
166 | |||
167 | // XXX we also should enforce HTTPS |
||
168 | $redirectUri = $request->getQueryParameter('redirect_uri'); |
||
169 | View Code Duplication | if (false === filter_var($redirectUri, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED | FILTER_FLAG_HOST_REQUIRED | FILTER_FLAG_PATH_REQUIRED)) { |
|
0 ignored issues
–
show
|
|||
170 | throw new HttpException('invalid redirect_uri', 400); |
||
171 | } |
||
172 | if (false !== strpos($redirectUri, '?')) { |
||
173 | throw new HttpException('invalid redirect_uri', 400); |
||
174 | } |
||
175 | $responseType = $request->getQueryParameter('response_type'); |
||
176 | if ('token' !== $responseType) { |
||
177 | throw new HttpException('invalid response_type', 400); |
||
178 | } |
||
179 | // XXX make sure this regexp/code is actually correct! |
||
180 | $scope = $request->getQueryParameter('scope'); |
||
181 | $scopeTokens = explode(' ', $scope); |
||
182 | foreach ($scopeTokens as $scopeToken) { |
||
183 | if (1 !== preg_match('/^[\x21\x23-\x5B\x5D-\x7E]+$/', $scopeToken)) { |
||
184 | throw new HttpException('invalid scope', 400); |
||
185 | } |
||
186 | } |
||
187 | |||
188 | // state is OPTIONAL in remoteStorage |
||
189 | $state = $request->getQueryParameter('state', false, null); |
||
190 | if (!is_null($state)) { |
||
191 | if (1 !== preg_match('/^[\x20-\x7E]+$/', $state)) { |
||
192 | throw new HttpException('invalid state', 400); |
||
193 | } |
||
194 | } |
||
195 | } |
||
196 | |||
197 | private function validateClient(Request $request) |
||
198 | { |
||
199 | $clientId = $request->getQueryParameter('client_id'); |
||
200 | $redirectUri = $request->getQueryParameter('redirect_uri'); |
||
201 | |||
202 | // redirectUri has to start with clientId (or be equal) |
||
203 | if (0 !== strpos($redirectUri, $clientId)) { |
||
204 | throw new HttpException('"redirect_uri" does not match "client_id"', 400); |
||
205 | } |
||
206 | } |
||
207 | } |
||
208 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.