@@ -43,267 +43,267 @@ |
||
43 | 43 | use OCP\Session\Exceptions\SessionNotAvailableException; |
44 | 44 | |
45 | 45 | class ClientFlowLoginController extends Controller { |
46 | - /** @var IUserSession */ |
|
47 | - private $userSession; |
|
48 | - /** @var IL10N */ |
|
49 | - private $l10n; |
|
50 | - /** @var Defaults */ |
|
51 | - private $defaults; |
|
52 | - /** @var ISession */ |
|
53 | - private $session; |
|
54 | - /** @var IProvider */ |
|
55 | - private $tokenProvider; |
|
56 | - /** @var ISecureRandom */ |
|
57 | - private $random; |
|
58 | - /** @var IURLGenerator */ |
|
59 | - private $urlGenerator; |
|
60 | - /** @var ClientMapper */ |
|
61 | - private $clientMapper; |
|
62 | - /** @var AccessTokenMapper */ |
|
63 | - private $accessTokenMapper; |
|
64 | - /** @var ICrypto */ |
|
65 | - private $crypto; |
|
46 | + /** @var IUserSession */ |
|
47 | + private $userSession; |
|
48 | + /** @var IL10N */ |
|
49 | + private $l10n; |
|
50 | + /** @var Defaults */ |
|
51 | + private $defaults; |
|
52 | + /** @var ISession */ |
|
53 | + private $session; |
|
54 | + /** @var IProvider */ |
|
55 | + private $tokenProvider; |
|
56 | + /** @var ISecureRandom */ |
|
57 | + private $random; |
|
58 | + /** @var IURLGenerator */ |
|
59 | + private $urlGenerator; |
|
60 | + /** @var ClientMapper */ |
|
61 | + private $clientMapper; |
|
62 | + /** @var AccessTokenMapper */ |
|
63 | + private $accessTokenMapper; |
|
64 | + /** @var ICrypto */ |
|
65 | + private $crypto; |
|
66 | 66 | |
67 | - const stateName = 'client.flow.state.token'; |
|
67 | + const stateName = 'client.flow.state.token'; |
|
68 | 68 | |
69 | - /** |
|
70 | - * @param string $appName |
|
71 | - * @param IRequest $request |
|
72 | - * @param IUserSession $userSession |
|
73 | - * @param IL10N $l10n |
|
74 | - * @param Defaults $defaults |
|
75 | - * @param ISession $session |
|
76 | - * @param IProvider $tokenProvider |
|
77 | - * @param ISecureRandom $random |
|
78 | - * @param IURLGenerator $urlGenerator |
|
79 | - * @param ClientMapper $clientMapper |
|
80 | - * @param AccessTokenMapper $accessTokenMapper |
|
81 | - * @param ICrypto $crypto |
|
82 | - */ |
|
83 | - public function __construct($appName, |
|
84 | - IRequest $request, |
|
85 | - IUserSession $userSession, |
|
86 | - IL10N $l10n, |
|
87 | - Defaults $defaults, |
|
88 | - ISession $session, |
|
89 | - IProvider $tokenProvider, |
|
90 | - ISecureRandom $random, |
|
91 | - IURLGenerator $urlGenerator, |
|
92 | - ClientMapper $clientMapper, |
|
93 | - AccessTokenMapper $accessTokenMapper, |
|
94 | - ICrypto $crypto) { |
|
95 | - parent::__construct($appName, $request); |
|
96 | - $this->userSession = $userSession; |
|
97 | - $this->l10n = $l10n; |
|
98 | - $this->defaults = $defaults; |
|
99 | - $this->session = $session; |
|
100 | - $this->tokenProvider = $tokenProvider; |
|
101 | - $this->random = $random; |
|
102 | - $this->urlGenerator = $urlGenerator; |
|
103 | - $this->clientMapper = $clientMapper; |
|
104 | - $this->accessTokenMapper = $accessTokenMapper; |
|
105 | - $this->crypto = $crypto; |
|
106 | - } |
|
69 | + /** |
|
70 | + * @param string $appName |
|
71 | + * @param IRequest $request |
|
72 | + * @param IUserSession $userSession |
|
73 | + * @param IL10N $l10n |
|
74 | + * @param Defaults $defaults |
|
75 | + * @param ISession $session |
|
76 | + * @param IProvider $tokenProvider |
|
77 | + * @param ISecureRandom $random |
|
78 | + * @param IURLGenerator $urlGenerator |
|
79 | + * @param ClientMapper $clientMapper |
|
80 | + * @param AccessTokenMapper $accessTokenMapper |
|
81 | + * @param ICrypto $crypto |
|
82 | + */ |
|
83 | + public function __construct($appName, |
|
84 | + IRequest $request, |
|
85 | + IUserSession $userSession, |
|
86 | + IL10N $l10n, |
|
87 | + Defaults $defaults, |
|
88 | + ISession $session, |
|
89 | + IProvider $tokenProvider, |
|
90 | + ISecureRandom $random, |
|
91 | + IURLGenerator $urlGenerator, |
|
92 | + ClientMapper $clientMapper, |
|
93 | + AccessTokenMapper $accessTokenMapper, |
|
94 | + ICrypto $crypto) { |
|
95 | + parent::__construct($appName, $request); |
|
96 | + $this->userSession = $userSession; |
|
97 | + $this->l10n = $l10n; |
|
98 | + $this->defaults = $defaults; |
|
99 | + $this->session = $session; |
|
100 | + $this->tokenProvider = $tokenProvider; |
|
101 | + $this->random = $random; |
|
102 | + $this->urlGenerator = $urlGenerator; |
|
103 | + $this->clientMapper = $clientMapper; |
|
104 | + $this->accessTokenMapper = $accessTokenMapper; |
|
105 | + $this->crypto = $crypto; |
|
106 | + } |
|
107 | 107 | |
108 | - /** |
|
109 | - * @return string |
|
110 | - */ |
|
111 | - private function getClientName() { |
|
112 | - return $this->request->getHeader('USER_AGENT') !== null ? $this->request->getHeader('USER_AGENT') : 'unknown'; |
|
113 | - } |
|
108 | + /** |
|
109 | + * @return string |
|
110 | + */ |
|
111 | + private function getClientName() { |
|
112 | + return $this->request->getHeader('USER_AGENT') !== null ? $this->request->getHeader('USER_AGENT') : 'unknown'; |
|
113 | + } |
|
114 | 114 | |
115 | - /** |
|
116 | - * @param string $stateToken |
|
117 | - * @return bool |
|
118 | - */ |
|
119 | - private function isValidToken($stateToken) { |
|
120 | - $currentToken = $this->session->get(self::stateName); |
|
121 | - if(!is_string($stateToken) || !is_string($currentToken)) { |
|
122 | - return false; |
|
123 | - } |
|
124 | - return hash_equals($currentToken, $stateToken); |
|
125 | - } |
|
115 | + /** |
|
116 | + * @param string $stateToken |
|
117 | + * @return bool |
|
118 | + */ |
|
119 | + private function isValidToken($stateToken) { |
|
120 | + $currentToken = $this->session->get(self::stateName); |
|
121 | + if(!is_string($stateToken) || !is_string($currentToken)) { |
|
122 | + return false; |
|
123 | + } |
|
124 | + return hash_equals($currentToken, $stateToken); |
|
125 | + } |
|
126 | 126 | |
127 | - /** |
|
128 | - * @return TemplateResponse |
|
129 | - */ |
|
130 | - private function stateTokenForbiddenResponse() { |
|
131 | - $response = new TemplateResponse( |
|
132 | - $this->appName, |
|
133 | - '403', |
|
134 | - [ |
|
135 | - 'file' => $this->l10n->t('State token does not match'), |
|
136 | - ], |
|
137 | - 'guest' |
|
138 | - ); |
|
139 | - $response->setStatus(Http::STATUS_FORBIDDEN); |
|
140 | - return $response; |
|
141 | - } |
|
127 | + /** |
|
128 | + * @return TemplateResponse |
|
129 | + */ |
|
130 | + private function stateTokenForbiddenResponse() { |
|
131 | + $response = new TemplateResponse( |
|
132 | + $this->appName, |
|
133 | + '403', |
|
134 | + [ |
|
135 | + 'file' => $this->l10n->t('State token does not match'), |
|
136 | + ], |
|
137 | + 'guest' |
|
138 | + ); |
|
139 | + $response->setStatus(Http::STATUS_FORBIDDEN); |
|
140 | + return $response; |
|
141 | + } |
|
142 | 142 | |
143 | - /** |
|
144 | - * @PublicPage |
|
145 | - * @NoCSRFRequired |
|
146 | - * @UseSession |
|
147 | - * |
|
148 | - * @param string $clientIdentifier |
|
149 | - * |
|
150 | - * @return TemplateResponse |
|
151 | - */ |
|
152 | - public function showAuthPickerPage($clientIdentifier = '', |
|
153 | - $oauthState = '') { |
|
143 | + /** |
|
144 | + * @PublicPage |
|
145 | + * @NoCSRFRequired |
|
146 | + * @UseSession |
|
147 | + * |
|
148 | + * @param string $clientIdentifier |
|
149 | + * |
|
150 | + * @return TemplateResponse |
|
151 | + */ |
|
152 | + public function showAuthPickerPage($clientIdentifier = '', |
|
153 | + $oauthState = '') { |
|
154 | 154 | |
155 | 155 | |
156 | - $clientName = $this->getClientName(); |
|
157 | - $client = null; |
|
158 | - if($clientIdentifier !== '') { |
|
159 | - $client = $this->clientMapper->getByIdentifier($clientIdentifier); |
|
160 | - $clientName = $client->getName(); |
|
161 | - } |
|
156 | + $clientName = $this->getClientName(); |
|
157 | + $client = null; |
|
158 | + if($clientIdentifier !== '') { |
|
159 | + $client = $this->clientMapper->getByIdentifier($clientIdentifier); |
|
160 | + $clientName = $client->getName(); |
|
161 | + } |
|
162 | 162 | |
163 | - $validClient = $client !== null && $client->getClientIdentifier() !== null; |
|
164 | - $cookieCheckSuccessful = $this->request->passesStrictCookieCheck(); |
|
163 | + $validClient = $client !== null && $client->getClientIdentifier() !== null; |
|
164 | + $cookieCheckSuccessful = $this->request->passesStrictCookieCheck(); |
|
165 | 165 | |
166 | - // no valid clientIdentifier given and no valid API Request (APIRequest header not set) |
|
167 | - if ($cookieCheckSuccessful === false && $validClient === false) { |
|
168 | - return new TemplateResponse( |
|
169 | - $this->appName, |
|
170 | - 'error', |
|
171 | - ['errors' => |
|
172 | - [ |
|
173 | - ['error' => 'Access Forbidden', 'hint' => 'Invalid request'] |
|
174 | - ] |
|
175 | - ] |
|
176 | - ); |
|
177 | - } |
|
166 | + // no valid clientIdentifier given and no valid API Request (APIRequest header not set) |
|
167 | + if ($cookieCheckSuccessful === false && $validClient === false) { |
|
168 | + return new TemplateResponse( |
|
169 | + $this->appName, |
|
170 | + 'error', |
|
171 | + ['errors' => |
|
172 | + [ |
|
173 | + ['error' => 'Access Forbidden', 'hint' => 'Invalid request'] |
|
174 | + ] |
|
175 | + ] |
|
176 | + ); |
|
177 | + } |
|
178 | 178 | |
179 | - $stateToken = $this->random->generate( |
|
180 | - 64, |
|
181 | - ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_DIGITS |
|
182 | - ); |
|
183 | - $this->session->set(self::stateName, $stateToken); |
|
179 | + $stateToken = $this->random->generate( |
|
180 | + 64, |
|
181 | + ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_DIGITS |
|
182 | + ); |
|
183 | + $this->session->set(self::stateName, $stateToken); |
|
184 | 184 | |
185 | - return new TemplateResponse( |
|
186 | - $this->appName, |
|
187 | - 'loginflow/authpicker', |
|
188 | - [ |
|
189 | - 'client' => $clientName, |
|
190 | - 'clientIdentifier' => $clientIdentifier, |
|
191 | - 'oauthState' => $oauthState, |
|
192 | - 'instanceName' => $this->defaults->getName(), |
|
193 | - 'urlGenerator' => $this->urlGenerator, |
|
194 | - 'stateToken' => $stateToken, |
|
195 | - 'serverHost' => $this->request->getServerHost(), |
|
196 | - ], |
|
197 | - 'guest' |
|
198 | - ); |
|
199 | - } |
|
185 | + return new TemplateResponse( |
|
186 | + $this->appName, |
|
187 | + 'loginflow/authpicker', |
|
188 | + [ |
|
189 | + 'client' => $clientName, |
|
190 | + 'clientIdentifier' => $clientIdentifier, |
|
191 | + 'oauthState' => $oauthState, |
|
192 | + 'instanceName' => $this->defaults->getName(), |
|
193 | + 'urlGenerator' => $this->urlGenerator, |
|
194 | + 'stateToken' => $stateToken, |
|
195 | + 'serverHost' => $this->request->getServerHost(), |
|
196 | + ], |
|
197 | + 'guest' |
|
198 | + ); |
|
199 | + } |
|
200 | 200 | |
201 | - /** |
|
202 | - * @NoAdminRequired |
|
203 | - * @NoCSRFRequired |
|
204 | - * @UseSession |
|
205 | - * |
|
206 | - * @param string $stateToken |
|
207 | - * @param string $clientIdentifier |
|
208 | - * @param string $oauthState |
|
209 | - * @return TemplateResponse |
|
210 | - */ |
|
211 | - public function redirectPage($stateToken = '', |
|
212 | - $clientIdentifier = '', |
|
213 | - $oauthState = '') { |
|
214 | - if(!$this->isValidToken($stateToken)) { |
|
215 | - return $this->stateTokenForbiddenResponse(); |
|
216 | - } |
|
201 | + /** |
|
202 | + * @NoAdminRequired |
|
203 | + * @NoCSRFRequired |
|
204 | + * @UseSession |
|
205 | + * |
|
206 | + * @param string $stateToken |
|
207 | + * @param string $clientIdentifier |
|
208 | + * @param string $oauthState |
|
209 | + * @return TemplateResponse |
|
210 | + */ |
|
211 | + public function redirectPage($stateToken = '', |
|
212 | + $clientIdentifier = '', |
|
213 | + $oauthState = '') { |
|
214 | + if(!$this->isValidToken($stateToken)) { |
|
215 | + return $this->stateTokenForbiddenResponse(); |
|
216 | + } |
|
217 | 217 | |
218 | - return new TemplateResponse( |
|
219 | - $this->appName, |
|
220 | - 'loginflow/redirect', |
|
221 | - [ |
|
222 | - 'urlGenerator' => $this->urlGenerator, |
|
223 | - 'stateToken' => $stateToken, |
|
224 | - 'clientIdentifier' => $clientIdentifier, |
|
225 | - 'oauthState' => $oauthState, |
|
226 | - ], |
|
227 | - 'empty' |
|
228 | - ); |
|
229 | - } |
|
218 | + return new TemplateResponse( |
|
219 | + $this->appName, |
|
220 | + 'loginflow/redirect', |
|
221 | + [ |
|
222 | + 'urlGenerator' => $this->urlGenerator, |
|
223 | + 'stateToken' => $stateToken, |
|
224 | + 'clientIdentifier' => $clientIdentifier, |
|
225 | + 'oauthState' => $oauthState, |
|
226 | + ], |
|
227 | + 'empty' |
|
228 | + ); |
|
229 | + } |
|
230 | 230 | |
231 | - /** |
|
232 | - * @NoAdminRequired |
|
233 | - * @UseSession |
|
234 | - * |
|
235 | - * @param string $stateToken |
|
236 | - * @param string $clientIdentifier |
|
237 | - * @param string $state |
|
238 | - * @param string $oauthState |
|
239 | - * @return Http\RedirectResponse|Response |
|
240 | - */ |
|
241 | - public function generateAppPassword($stateToken, |
|
242 | - $clientIdentifier = '', |
|
243 | - $state = '', |
|
244 | - $oauthState = '') { |
|
245 | - if(!$this->isValidToken($stateToken)) { |
|
246 | - $this->session->remove(self::stateName); |
|
247 | - return $this->stateTokenForbiddenResponse(); |
|
248 | - } |
|
231 | + /** |
|
232 | + * @NoAdminRequired |
|
233 | + * @UseSession |
|
234 | + * |
|
235 | + * @param string $stateToken |
|
236 | + * @param string $clientIdentifier |
|
237 | + * @param string $state |
|
238 | + * @param string $oauthState |
|
239 | + * @return Http\RedirectResponse|Response |
|
240 | + */ |
|
241 | + public function generateAppPassword($stateToken, |
|
242 | + $clientIdentifier = '', |
|
243 | + $state = '', |
|
244 | + $oauthState = '') { |
|
245 | + if(!$this->isValidToken($stateToken)) { |
|
246 | + $this->session->remove(self::stateName); |
|
247 | + return $this->stateTokenForbiddenResponse(); |
|
248 | + } |
|
249 | 249 | |
250 | - $this->session->remove(self::stateName); |
|
250 | + $this->session->remove(self::stateName); |
|
251 | 251 | |
252 | - try { |
|
253 | - $sessionId = $this->session->getId(); |
|
254 | - } catch (SessionNotAvailableException $ex) { |
|
255 | - $response = new Response(); |
|
256 | - $response->setStatus(Http::STATUS_FORBIDDEN); |
|
257 | - return $response; |
|
258 | - } |
|
252 | + try { |
|
253 | + $sessionId = $this->session->getId(); |
|
254 | + } catch (SessionNotAvailableException $ex) { |
|
255 | + $response = new Response(); |
|
256 | + $response->setStatus(Http::STATUS_FORBIDDEN); |
|
257 | + return $response; |
|
258 | + } |
|
259 | 259 | |
260 | - try { |
|
261 | - $sessionToken = $this->tokenProvider->getToken($sessionId); |
|
262 | - $loginName = $sessionToken->getLoginName(); |
|
263 | - try { |
|
264 | - $password = $this->tokenProvider->getPassword($sessionToken, $sessionId); |
|
265 | - } catch (PasswordlessTokenException $ex) { |
|
266 | - $password = null; |
|
267 | - } |
|
268 | - } catch (InvalidTokenException $ex) { |
|
269 | - $response = new Response(); |
|
270 | - $response->setStatus(Http::STATUS_FORBIDDEN); |
|
271 | - return $response; |
|
272 | - } |
|
260 | + try { |
|
261 | + $sessionToken = $this->tokenProvider->getToken($sessionId); |
|
262 | + $loginName = $sessionToken->getLoginName(); |
|
263 | + try { |
|
264 | + $password = $this->tokenProvider->getPassword($sessionToken, $sessionId); |
|
265 | + } catch (PasswordlessTokenException $ex) { |
|
266 | + $password = null; |
|
267 | + } |
|
268 | + } catch (InvalidTokenException $ex) { |
|
269 | + $response = new Response(); |
|
270 | + $response->setStatus(Http::STATUS_FORBIDDEN); |
|
271 | + return $response; |
|
272 | + } |
|
273 | 273 | |
274 | - $token = $this->random->generate(72, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS); |
|
275 | - $uid = $this->userSession->getUser()->getUID(); |
|
276 | - $generatedToken = $this->tokenProvider->generateToken( |
|
277 | - $token, |
|
278 | - $uid, |
|
279 | - $loginName, |
|
280 | - $password, |
|
281 | - $this->getClientName(), |
|
282 | - IToken::PERMANENT_TOKEN, |
|
283 | - IToken::DO_NOT_REMEMBER |
|
284 | - ); |
|
274 | + $token = $this->random->generate(72, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS); |
|
275 | + $uid = $this->userSession->getUser()->getUID(); |
|
276 | + $generatedToken = $this->tokenProvider->generateToken( |
|
277 | + $token, |
|
278 | + $uid, |
|
279 | + $loginName, |
|
280 | + $password, |
|
281 | + $this->getClientName(), |
|
282 | + IToken::PERMANENT_TOKEN, |
|
283 | + IToken::DO_NOT_REMEMBER |
|
284 | + ); |
|
285 | 285 | |
286 | - if($clientIdentifier !== '') { |
|
287 | - $client = $this->clientMapper->getByIdentifier($clientIdentifier); |
|
286 | + if($clientIdentifier !== '') { |
|
287 | + $client = $this->clientMapper->getByIdentifier($clientIdentifier); |
|
288 | 288 | |
289 | - $code = $this->random->generate(128); |
|
290 | - $accessToken = new AccessToken(); |
|
291 | - $accessToken->setClientId($client->getId()); |
|
292 | - $accessToken->setEncryptedToken($this->crypto->encrypt($token, $code)); |
|
293 | - $accessToken->setHashedCode(hash('sha512', $code)); |
|
294 | - $accessToken->setTokenId($generatedToken->getId()); |
|
295 | - $this->accessTokenMapper->insert($accessToken); |
|
289 | + $code = $this->random->generate(128); |
|
290 | + $accessToken = new AccessToken(); |
|
291 | + $accessToken->setClientId($client->getId()); |
|
292 | + $accessToken->setEncryptedToken($this->crypto->encrypt($token, $code)); |
|
293 | + $accessToken->setHashedCode(hash('sha512', $code)); |
|
294 | + $accessToken->setTokenId($generatedToken->getId()); |
|
295 | + $this->accessTokenMapper->insert($accessToken); |
|
296 | 296 | |
297 | - $redirectUri = sprintf( |
|
298 | - '%s?state=%s&code=%s', |
|
299 | - $client->getRedirectUri(), |
|
300 | - urlencode($oauthState), |
|
301 | - urlencode($code) |
|
302 | - ); |
|
303 | - } else { |
|
304 | - $redirectUri = 'nc://login/server:' . $this->request->getServerHost() . '&user:' . urlencode($loginName) . '&password:' . urlencode($token); |
|
305 | - } |
|
297 | + $redirectUri = sprintf( |
|
298 | + '%s?state=%s&code=%s', |
|
299 | + $client->getRedirectUri(), |
|
300 | + urlencode($oauthState), |
|
301 | + urlencode($code) |
|
302 | + ); |
|
303 | + } else { |
|
304 | + $redirectUri = 'nc://login/server:' . $this->request->getServerHost() . '&user:' . urlencode($loginName) . '&password:' . urlencode($token); |
|
305 | + } |
|
306 | 306 | |
307 | - return new Http\RedirectResponse($redirectUri); |
|
308 | - } |
|
307 | + return new Http\RedirectResponse($redirectUri); |
|
308 | + } |
|
309 | 309 | } |
@@ -20,26 +20,26 @@ |
||
20 | 20 | */ |
21 | 21 | |
22 | 22 | return [ |
23 | - 'routes' => [ |
|
24 | - [ |
|
25 | - 'name' => 'Settings#addClient', |
|
26 | - 'url' => '/settings', |
|
27 | - 'verb' => 'POST', |
|
28 | - ], |
|
29 | - [ |
|
30 | - 'name' => 'Settings#deleteClient', |
|
31 | - 'url' => '/clients/{id}/delete', |
|
32 | - 'verb' => 'POST' |
|
33 | - ], |
|
34 | - [ |
|
35 | - 'name' => 'LoginRedirector#authorize', |
|
36 | - 'url' => '/authorize', |
|
37 | - 'verb' => 'GET', |
|
38 | - ], |
|
39 | - [ |
|
40 | - 'name' => 'OauthApi#getToken', |
|
41 | - 'url' => '/api/v1/token', |
|
42 | - 'verb' => 'POST' |
|
43 | - ], |
|
44 | - ], |
|
23 | + 'routes' => [ |
|
24 | + [ |
|
25 | + 'name' => 'Settings#addClient', |
|
26 | + 'url' => '/settings', |
|
27 | + 'verb' => 'POST', |
|
28 | + ], |
|
29 | + [ |
|
30 | + 'name' => 'Settings#deleteClient', |
|
31 | + 'url' => '/clients/{id}/delete', |
|
32 | + 'verb' => 'POST' |
|
33 | + ], |
|
34 | + [ |
|
35 | + 'name' => 'LoginRedirector#authorize', |
|
36 | + 'url' => '/authorize', |
|
37 | + 'verb' => 'GET', |
|
38 | + ], |
|
39 | + [ |
|
40 | + 'name' => 'OauthApi#getToken', |
|
41 | + 'url' => '/api/v1/token', |
|
42 | + 'verb' => 'POST' |
|
43 | + ], |
|
44 | + ], |
|
45 | 45 | ]; |
46 | 46 | \ No newline at end of file |
@@ -34,20 +34,20 @@ |
||
34 | 34 | * @method void setName(string $name) |
35 | 35 | */ |
36 | 36 | class Client extends Entity { |
37 | - /** @var string */ |
|
38 | - protected $name; |
|
39 | - /** @var string */ |
|
40 | - protected $redirectUri; |
|
41 | - /** @var string */ |
|
42 | - protected $clientIdentifier; |
|
43 | - /** @var string */ |
|
44 | - protected $secret; |
|
37 | + /** @var string */ |
|
38 | + protected $name; |
|
39 | + /** @var string */ |
|
40 | + protected $redirectUri; |
|
41 | + /** @var string */ |
|
42 | + protected $clientIdentifier; |
|
43 | + /** @var string */ |
|
44 | + protected $secret; |
|
45 | 45 | |
46 | - public function __construct() { |
|
47 | - $this->addType('id', 'int'); |
|
48 | - $this->addType('name', 'string'); |
|
49 | - $this->addType('redirect_uri', 'string'); |
|
50 | - $this->addType('client_identifier', 'string'); |
|
51 | - $this->addType('secret', 'string'); |
|
52 | - } |
|
46 | + public function __construct() { |
|
47 | + $this->addType('id', 'int'); |
|
48 | + $this->addType('name', 'string'); |
|
49 | + $this->addType('redirect_uri', 'string'); |
|
50 | + $this->addType('client_identifier', 'string'); |
|
51 | + $this->addType('secret', 'string'); |
|
52 | + } |
|
53 | 53 | } |
@@ -34,20 +34,20 @@ |
||
34 | 34 | * @method void setHashedCode(string $token) |
35 | 35 | */ |
36 | 36 | class AccessToken extends Entity { |
37 | - /** @var int */ |
|
38 | - protected $tokenId; |
|
39 | - /** @var int */ |
|
40 | - protected $clientId; |
|
41 | - /** @var string */ |
|
42 | - protected $hashedCode; |
|
43 | - /** @var string */ |
|
44 | - protected $encryptedToken; |
|
37 | + /** @var int */ |
|
38 | + protected $tokenId; |
|
39 | + /** @var int */ |
|
40 | + protected $clientId; |
|
41 | + /** @var string */ |
|
42 | + protected $hashedCode; |
|
43 | + /** @var string */ |
|
44 | + protected $encryptedToken; |
|
45 | 45 | |
46 | - public function __construct() { |
|
47 | - $this->addType('id', 'int'); |
|
48 | - $this->addType('token_id', 'int'); |
|
49 | - $this->addType('client_id', 'int'); |
|
50 | - $this->addType('hashed_code', 'string'); |
|
51 | - $this->addType('encrypted_token', 'string'); |
|
52 | - } |
|
46 | + public function __construct() { |
|
47 | + $this->addType('id', 'int'); |
|
48 | + $this->addType('token_id', 'int'); |
|
49 | + $this->addType('client_id', 'int'); |
|
50 | + $this->addType('hashed_code', 'string'); |
|
51 | + $this->addType('encrypted_token', 'string'); |
|
52 | + } |
|
53 | 53 | } |
@@ -26,43 +26,43 @@ |
||
26 | 26 | |
27 | 27 | class ClientMapper extends Mapper { |
28 | 28 | |
29 | - /** |
|
30 | - * @param IDBConnection $db |
|
31 | - */ |
|
32 | - public function __construct(IDBConnection $db) { |
|
33 | - parent::__construct($db, 'oauth2_clients'); |
|
34 | - } |
|
29 | + /** |
|
30 | + * @param IDBConnection $db |
|
31 | + */ |
|
32 | + public function __construct(IDBConnection $db) { |
|
33 | + parent::__construct($db, 'oauth2_clients'); |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * @param string $clientIdentifier |
|
38 | - * @return Client |
|
39 | - */ |
|
40 | - public function getByIdentifier($clientIdentifier) { |
|
41 | - $qb = $this->db->getQueryBuilder(); |
|
42 | - $qb |
|
43 | - ->select('*') |
|
44 | - ->from($this->tableName) |
|
45 | - ->where($qb->expr()->eq('client_identifier', $qb->createNamedParameter($clientIdentifier))); |
|
46 | - $result = $qb->execute(); |
|
47 | - $row = $result->fetch(); |
|
48 | - $result->closeCursor(); |
|
36 | + /** |
|
37 | + * @param string $clientIdentifier |
|
38 | + * @return Client |
|
39 | + */ |
|
40 | + public function getByIdentifier($clientIdentifier) { |
|
41 | + $qb = $this->db->getQueryBuilder(); |
|
42 | + $qb |
|
43 | + ->select('*') |
|
44 | + ->from($this->tableName) |
|
45 | + ->where($qb->expr()->eq('client_identifier', $qb->createNamedParameter($clientIdentifier))); |
|
46 | + $result = $qb->execute(); |
|
47 | + $row = $result->fetch(); |
|
48 | + $result->closeCursor(); |
|
49 | 49 | |
50 | - if (!is_array($row)) { |
|
51 | - $row = []; |
|
52 | - } |
|
50 | + if (!is_array($row)) { |
|
51 | + $row = []; |
|
52 | + } |
|
53 | 53 | |
54 | - return Client::fromRow($row); |
|
55 | - } |
|
54 | + return Client::fromRow($row); |
|
55 | + } |
|
56 | 56 | |
57 | - /** |
|
58 | - * @return Client[] |
|
59 | - */ |
|
60 | - public function getClients() { |
|
61 | - $qb = $this->db->getQueryBuilder(); |
|
62 | - $qb |
|
63 | - ->select('*') |
|
64 | - ->from($this->tableName); |
|
57 | + /** |
|
58 | + * @return Client[] |
|
59 | + */ |
|
60 | + public function getClients() { |
|
61 | + $qb = $this->db->getQueryBuilder(); |
|
62 | + $qb |
|
63 | + ->select('*') |
|
64 | + ->from($this->tableName); |
|
65 | 65 | |
66 | - return $this->findEntities($qb->getSQL()); |
|
67 | - } |
|
66 | + return $this->findEntities($qb->getSQL()); |
|
67 | + } |
|
68 | 68 | } |
@@ -28,51 +28,51 @@ |
||
28 | 28 | use OCP\IURLGenerator; |
29 | 29 | |
30 | 30 | class LoginRedirectorController extends Controller { |
31 | - /** @var IURLGenerator */ |
|
32 | - private $urlGenerator; |
|
33 | - /** @var ClientMapper */ |
|
34 | - private $clientMapper; |
|
31 | + /** @var IURLGenerator */ |
|
32 | + private $urlGenerator; |
|
33 | + /** @var ClientMapper */ |
|
34 | + private $clientMapper; |
|
35 | 35 | |
36 | - /** |
|
37 | - * @param string $appName |
|
38 | - * @param IRequest $request |
|
39 | - * @param IURLGenerator $urlGenerator |
|
40 | - * @param ClientMapper $clientMapper |
|
41 | - */ |
|
42 | - public function __construct($appName, |
|
43 | - IRequest $request, |
|
44 | - IURLGenerator $urlGenerator, |
|
45 | - ClientMapper $clientMapper) { |
|
46 | - parent::__construct($appName, $request); |
|
47 | - $this->urlGenerator = $urlGenerator; |
|
48 | - $this->clientMapper = $clientMapper; |
|
49 | - } |
|
36 | + /** |
|
37 | + * @param string $appName |
|
38 | + * @param IRequest $request |
|
39 | + * @param IURLGenerator $urlGenerator |
|
40 | + * @param ClientMapper $clientMapper |
|
41 | + */ |
|
42 | + public function __construct($appName, |
|
43 | + IRequest $request, |
|
44 | + IURLGenerator $urlGenerator, |
|
45 | + ClientMapper $clientMapper) { |
|
46 | + parent::__construct($appName, $request); |
|
47 | + $this->urlGenerator = $urlGenerator; |
|
48 | + $this->clientMapper = $clientMapper; |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * @PublicPage |
|
53 | - * @NoCSRFRequired |
|
54 | - * |
|
55 | - * @param string $client_id |
|
56 | - * @param string $redirect_uri |
|
57 | - * @param string $state |
|
58 | - * @return RedirectResponse |
|
59 | - */ |
|
60 | - public function authorize($client_id, |
|
61 | - $redirect_uri, |
|
62 | - $state) { |
|
63 | - $client = $this->clientMapper->getByIdentifier($client_id); |
|
51 | + /** |
|
52 | + * @PublicPage |
|
53 | + * @NoCSRFRequired |
|
54 | + * |
|
55 | + * @param string $client_id |
|
56 | + * @param string $redirect_uri |
|
57 | + * @param string $state |
|
58 | + * @return RedirectResponse |
|
59 | + */ |
|
60 | + public function authorize($client_id, |
|
61 | + $redirect_uri, |
|
62 | + $state) { |
|
63 | + $client = $this->clientMapper->getByIdentifier($client_id); |
|
64 | 64 | |
65 | - if($client->getRedirectUri() !== $redirect_uri) { |
|
66 | - throw new \Exception('Redirect URI does not match'); |
|
67 | - } |
|
65 | + if($client->getRedirectUri() !== $redirect_uri) { |
|
66 | + throw new \Exception('Redirect URI does not match'); |
|
67 | + } |
|
68 | 68 | |
69 | - $targetUrl = $this->urlGenerator->linkToRouteAbsolute( |
|
70 | - 'core.ClientFlowLogin.showAuthPickerPage', |
|
71 | - [ |
|
72 | - 'clientIdentifier' => $client->getClientIdentifier(), |
|
73 | - 'oauthState' => $state, |
|
74 | - ] |
|
75 | - ); |
|
76 | - return new RedirectResponse($targetUrl); |
|
77 | - } |
|
69 | + $targetUrl = $this->urlGenerator->linkToRouteAbsolute( |
|
70 | + 'core.ClientFlowLogin.showAuthPickerPage', |
|
71 | + [ |
|
72 | + 'clientIdentifier' => $client->getClientIdentifier(), |
|
73 | + 'oauthState' => $state, |
|
74 | + ] |
|
75 | + ); |
|
76 | + return new RedirectResponse($targetUrl); |
|
77 | + } |
|
78 | 78 | } |
@@ -30,59 +30,59 @@ |
||
30 | 30 | use OCP\Security\ISecureRandom; |
31 | 31 | |
32 | 32 | class OauthApiController extends Controller { |
33 | - /** @var AccessTokenMapper */ |
|
34 | - private $accessTokenMapper; |
|
35 | - /** @var ICrypto */ |
|
36 | - private $crypto; |
|
37 | - /** @var DefaultTokenMapper */ |
|
38 | - private $defaultTokenMapper; |
|
39 | - /** @var ISecureRandom */ |
|
40 | - private $secureRandom; |
|
33 | + /** @var AccessTokenMapper */ |
|
34 | + private $accessTokenMapper; |
|
35 | + /** @var ICrypto */ |
|
36 | + private $crypto; |
|
37 | + /** @var DefaultTokenMapper */ |
|
38 | + private $defaultTokenMapper; |
|
39 | + /** @var ISecureRandom */ |
|
40 | + private $secureRandom; |
|
41 | 41 | |
42 | - /** |
|
43 | - * @param string $appName |
|
44 | - * @param IRequest $request |
|
45 | - * @param ICrypto $crypto |
|
46 | - * @param AccessTokenMapper $accessTokenMapper |
|
47 | - * @param DefaultTokenMapper $defaultTokenMapper |
|
48 | - * @param ISecureRandom $secureRandom |
|
49 | - */ |
|
50 | - public function __construct($appName, |
|
51 | - IRequest $request, |
|
52 | - ICrypto $crypto, |
|
53 | - AccessTokenMapper $accessTokenMapper, |
|
54 | - DefaultTokenMapper $defaultTokenMapper, |
|
55 | - ISecureRandom $secureRandom) { |
|
56 | - parent::__construct($appName, $request); |
|
57 | - $this->crypto = $crypto; |
|
58 | - $this->accessTokenMapper = $accessTokenMapper; |
|
59 | - $this->defaultTokenMapper = $defaultTokenMapper; |
|
60 | - $this->secureRandom = $secureRandom; |
|
61 | - } |
|
42 | + /** |
|
43 | + * @param string $appName |
|
44 | + * @param IRequest $request |
|
45 | + * @param ICrypto $crypto |
|
46 | + * @param AccessTokenMapper $accessTokenMapper |
|
47 | + * @param DefaultTokenMapper $defaultTokenMapper |
|
48 | + * @param ISecureRandom $secureRandom |
|
49 | + */ |
|
50 | + public function __construct($appName, |
|
51 | + IRequest $request, |
|
52 | + ICrypto $crypto, |
|
53 | + AccessTokenMapper $accessTokenMapper, |
|
54 | + DefaultTokenMapper $defaultTokenMapper, |
|
55 | + ISecureRandom $secureRandom) { |
|
56 | + parent::__construct($appName, $request); |
|
57 | + $this->crypto = $crypto; |
|
58 | + $this->accessTokenMapper = $accessTokenMapper; |
|
59 | + $this->defaultTokenMapper = $defaultTokenMapper; |
|
60 | + $this->secureRandom = $secureRandom; |
|
61 | + } |
|
62 | 62 | |
63 | - /** |
|
64 | - * @PublicPage |
|
65 | - * @NoCSRFRequired |
|
66 | - * |
|
67 | - * @param string $code |
|
68 | - * @return JSONResponse |
|
69 | - */ |
|
70 | - public function getToken($code) { |
|
71 | - $accessToken = $this->accessTokenMapper->getByCode($code); |
|
72 | - $decryptedToken = $this->crypto->decrypt($accessToken->getEncryptedToken(), $code); |
|
73 | - $newCode = $this->secureRandom->generate(128); |
|
74 | - $accessToken->setHashedCode(hash('sha512', $newCode)); |
|
75 | - $accessToken->setEncryptedToken($this->crypto->encrypt($decryptedToken, $newCode)); |
|
76 | - $this->accessTokenMapper->update($accessToken); |
|
63 | + /** |
|
64 | + * @PublicPage |
|
65 | + * @NoCSRFRequired |
|
66 | + * |
|
67 | + * @param string $code |
|
68 | + * @return JSONResponse |
|
69 | + */ |
|
70 | + public function getToken($code) { |
|
71 | + $accessToken = $this->accessTokenMapper->getByCode($code); |
|
72 | + $decryptedToken = $this->crypto->decrypt($accessToken->getEncryptedToken(), $code); |
|
73 | + $newCode = $this->secureRandom->generate(128); |
|
74 | + $accessToken->setHashedCode(hash('sha512', $newCode)); |
|
75 | + $accessToken->setEncryptedToken($this->crypto->encrypt($decryptedToken, $newCode)); |
|
76 | + $this->accessTokenMapper->update($accessToken); |
|
77 | 77 | |
78 | - return new JSONResponse( |
|
79 | - [ |
|
80 | - 'access_token' => $decryptedToken, |
|
81 | - 'token_type' => 'Bearer', |
|
82 | - 'expires_in' => 3600, |
|
83 | - 'refresh_token' => $newCode, |
|
84 | - 'user_id' => ($this->defaultTokenMapper->getTokenById($accessToken->getTokenId()))->getUID(), |
|
85 | - ] |
|
86 | - ); |
|
87 | - } |
|
78 | + return new JSONResponse( |
|
79 | + [ |
|
80 | + 'access_token' => $decryptedToken, |
|
81 | + 'token_type' => 'Bearer', |
|
82 | + 'expires_in' => 3600, |
|
83 | + 'refresh_token' => $newCode, |
|
84 | + 'user_id' => ($this->defaultTokenMapper->getTokenById($accessToken->getTokenId()))->getUID(), |
|
85 | + ] |
|
86 | + ); |
|
87 | + } |
|
88 | 88 | } |
@@ -27,41 +27,41 @@ |
||
27 | 27 | use OCP\Settings\ISettings; |
28 | 28 | |
29 | 29 | class Admin implements ISettings { |
30 | - /** @var ClientMapper */ |
|
31 | - private $clientMapper; |
|
30 | + /** @var ClientMapper */ |
|
31 | + private $clientMapper; |
|
32 | 32 | |
33 | - /** |
|
34 | - * @param ClientMapper $clientMapper |
|
35 | - */ |
|
36 | - public function __construct(ClientMapper $clientMapper) { |
|
37 | - $this->clientMapper = $clientMapper; |
|
38 | - } |
|
33 | + /** |
|
34 | + * @param ClientMapper $clientMapper |
|
35 | + */ |
|
36 | + public function __construct(ClientMapper $clientMapper) { |
|
37 | + $this->clientMapper = $clientMapper; |
|
38 | + } |
|
39 | 39 | |
40 | - /** |
|
41 | - * @return TemplateResponse |
|
42 | - */ |
|
43 | - public function getForm() { |
|
44 | - return new TemplateResponse( |
|
45 | - 'oauth2', |
|
46 | - 'admin', |
|
47 | - [ |
|
48 | - 'clients' => $this->clientMapper->getClients(), |
|
49 | - ], |
|
50 | - '' |
|
51 | - ); |
|
52 | - } |
|
40 | + /** |
|
41 | + * @return TemplateResponse |
|
42 | + */ |
|
43 | + public function getForm() { |
|
44 | + return new TemplateResponse( |
|
45 | + 'oauth2', |
|
46 | + 'admin', |
|
47 | + [ |
|
48 | + 'clients' => $this->clientMapper->getClients(), |
|
49 | + ], |
|
50 | + '' |
|
51 | + ); |
|
52 | + } |
|
53 | 53 | |
54 | - /** |
|
55 | - * {@inheritdoc} |
|
56 | - */ |
|
57 | - public function getSection() { |
|
58 | - return 'security'; |
|
59 | - } |
|
54 | + /** |
|
55 | + * {@inheritdoc} |
|
56 | + */ |
|
57 | + public function getSection() { |
|
58 | + return 'security'; |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * {@inheritdoc} |
|
63 | - */ |
|
64 | - public function getPriority() { |
|
65 | - return 0; |
|
66 | - } |
|
61 | + /** |
|
62 | + * {@inheritdoc} |
|
63 | + */ |
|
64 | + public function getPriority() { |
|
65 | + return 0; |
|
66 | + } |
|
67 | 67 | } |
@@ -48,225 +48,225 @@ |
||
48 | 48 | class Auth extends AbstractBasic { |
49 | 49 | |
50 | 50 | |
51 | - const DAV_AUTHENTICATED = 'AUTHENTICATED_TO_DAV_BACKEND'; |
|
51 | + const DAV_AUTHENTICATED = 'AUTHENTICATED_TO_DAV_BACKEND'; |
|
52 | 52 | |
53 | - /** @var ISession */ |
|
54 | - private $session; |
|
55 | - /** @var Session */ |
|
56 | - private $userSession; |
|
57 | - /** @var IRequest */ |
|
58 | - private $request; |
|
59 | - /** @var string */ |
|
60 | - private $currentUser; |
|
61 | - /** @var Manager */ |
|
62 | - private $twoFactorManager; |
|
63 | - /** @var Throttler */ |
|
64 | - private $throttler; |
|
53 | + /** @var ISession */ |
|
54 | + private $session; |
|
55 | + /** @var Session */ |
|
56 | + private $userSession; |
|
57 | + /** @var IRequest */ |
|
58 | + private $request; |
|
59 | + /** @var string */ |
|
60 | + private $currentUser; |
|
61 | + /** @var Manager */ |
|
62 | + private $twoFactorManager; |
|
63 | + /** @var Throttler */ |
|
64 | + private $throttler; |
|
65 | 65 | |
66 | - /** |
|
67 | - * @param ISession $session |
|
68 | - * @param Session $userSession |
|
69 | - * @param IRequest $request |
|
70 | - * @param Manager $twoFactorManager |
|
71 | - * @param Throttler $throttler |
|
72 | - * @param string $principalPrefix |
|
73 | - */ |
|
74 | - public function __construct(ISession $session, |
|
75 | - Session $userSession, |
|
76 | - IRequest $request, |
|
77 | - Manager $twoFactorManager, |
|
78 | - Throttler $throttler, |
|
79 | - $principalPrefix = 'principals/users/') { |
|
80 | - $this->session = $session; |
|
81 | - $this->userSession = $userSession; |
|
82 | - $this->twoFactorManager = $twoFactorManager; |
|
83 | - $this->request = $request; |
|
84 | - $this->throttler = $throttler; |
|
85 | - $this->principalPrefix = $principalPrefix; |
|
66 | + /** |
|
67 | + * @param ISession $session |
|
68 | + * @param Session $userSession |
|
69 | + * @param IRequest $request |
|
70 | + * @param Manager $twoFactorManager |
|
71 | + * @param Throttler $throttler |
|
72 | + * @param string $principalPrefix |
|
73 | + */ |
|
74 | + public function __construct(ISession $session, |
|
75 | + Session $userSession, |
|
76 | + IRequest $request, |
|
77 | + Manager $twoFactorManager, |
|
78 | + Throttler $throttler, |
|
79 | + $principalPrefix = 'principals/users/') { |
|
80 | + $this->session = $session; |
|
81 | + $this->userSession = $userSession; |
|
82 | + $this->twoFactorManager = $twoFactorManager; |
|
83 | + $this->request = $request; |
|
84 | + $this->throttler = $throttler; |
|
85 | + $this->principalPrefix = $principalPrefix; |
|
86 | 86 | |
87 | - // setup realm |
|
88 | - $defaults = new \OCP\Defaults(); |
|
89 | - $this->realm = $defaults->getName(); |
|
90 | - } |
|
87 | + // setup realm |
|
88 | + $defaults = new \OCP\Defaults(); |
|
89 | + $this->realm = $defaults->getName(); |
|
90 | + } |
|
91 | 91 | |
92 | - /** |
|
93 | - * Whether the user has initially authenticated via DAV |
|
94 | - * |
|
95 | - * This is required for WebDAV clients that resent the cookies even when the |
|
96 | - * account was changed. |
|
97 | - * |
|
98 | - * @see https://github.com/owncloud/core/issues/13245 |
|
99 | - * |
|
100 | - * @param string $username |
|
101 | - * @return bool |
|
102 | - */ |
|
103 | - public function isDavAuthenticated($username) { |
|
104 | - return !is_null($this->session->get(self::DAV_AUTHENTICATED)) && |
|
105 | - $this->session->get(self::DAV_AUTHENTICATED) === $username; |
|
106 | - } |
|
92 | + /** |
|
93 | + * Whether the user has initially authenticated via DAV |
|
94 | + * |
|
95 | + * This is required for WebDAV clients that resent the cookies even when the |
|
96 | + * account was changed. |
|
97 | + * |
|
98 | + * @see https://github.com/owncloud/core/issues/13245 |
|
99 | + * |
|
100 | + * @param string $username |
|
101 | + * @return bool |
|
102 | + */ |
|
103 | + public function isDavAuthenticated($username) { |
|
104 | + return !is_null($this->session->get(self::DAV_AUTHENTICATED)) && |
|
105 | + $this->session->get(self::DAV_AUTHENTICATED) === $username; |
|
106 | + } |
|
107 | 107 | |
108 | - /** |
|
109 | - * Validates a username and password |
|
110 | - * |
|
111 | - * This method should return true or false depending on if login |
|
112 | - * succeeded. |
|
113 | - * |
|
114 | - * @param string $username |
|
115 | - * @param string $password |
|
116 | - * @return bool |
|
117 | - * @throws PasswordLoginForbidden |
|
118 | - */ |
|
119 | - protected function validateUserPass($username, $password) { |
|
120 | - if ($this->userSession->isLoggedIn() && |
|
121 | - $this->isDavAuthenticated($this->userSession->getUser()->getUID()) |
|
122 | - ) { |
|
123 | - \OC_Util::setupFS($this->userSession->getUser()->getUID()); |
|
124 | - $this->session->close(); |
|
125 | - return true; |
|
126 | - } else { |
|
127 | - \OC_Util::setupFS(); //login hooks may need early access to the filesystem |
|
128 | - try { |
|
129 | - if ($this->userSession->logClientIn($username, $password, $this->request, $this->throttler)) { |
|
130 | - \OC_Util::setupFS($this->userSession->getUser()->getUID()); |
|
131 | - $this->session->set(self::DAV_AUTHENTICATED, $this->userSession->getUser()->getUID()); |
|
132 | - $this->session->close(); |
|
133 | - return true; |
|
134 | - } else { |
|
135 | - $this->session->close(); |
|
136 | - return false; |
|
137 | - } |
|
138 | - } catch (PasswordLoginForbiddenException $ex) { |
|
139 | - $this->session->close(); |
|
140 | - throw new PasswordLoginForbidden(); |
|
141 | - } |
|
142 | - } |
|
143 | - } |
|
108 | + /** |
|
109 | + * Validates a username and password |
|
110 | + * |
|
111 | + * This method should return true or false depending on if login |
|
112 | + * succeeded. |
|
113 | + * |
|
114 | + * @param string $username |
|
115 | + * @param string $password |
|
116 | + * @return bool |
|
117 | + * @throws PasswordLoginForbidden |
|
118 | + */ |
|
119 | + protected function validateUserPass($username, $password) { |
|
120 | + if ($this->userSession->isLoggedIn() && |
|
121 | + $this->isDavAuthenticated($this->userSession->getUser()->getUID()) |
|
122 | + ) { |
|
123 | + \OC_Util::setupFS($this->userSession->getUser()->getUID()); |
|
124 | + $this->session->close(); |
|
125 | + return true; |
|
126 | + } else { |
|
127 | + \OC_Util::setupFS(); //login hooks may need early access to the filesystem |
|
128 | + try { |
|
129 | + if ($this->userSession->logClientIn($username, $password, $this->request, $this->throttler)) { |
|
130 | + \OC_Util::setupFS($this->userSession->getUser()->getUID()); |
|
131 | + $this->session->set(self::DAV_AUTHENTICATED, $this->userSession->getUser()->getUID()); |
|
132 | + $this->session->close(); |
|
133 | + return true; |
|
134 | + } else { |
|
135 | + $this->session->close(); |
|
136 | + return false; |
|
137 | + } |
|
138 | + } catch (PasswordLoginForbiddenException $ex) { |
|
139 | + $this->session->close(); |
|
140 | + throw new PasswordLoginForbidden(); |
|
141 | + } |
|
142 | + } |
|
143 | + } |
|
144 | 144 | |
145 | - /** |
|
146 | - * @param RequestInterface $request |
|
147 | - * @param ResponseInterface $response |
|
148 | - * @return array |
|
149 | - * @throws NotAuthenticated |
|
150 | - * @throws ServiceUnavailable |
|
151 | - */ |
|
152 | - function check(RequestInterface $request, ResponseInterface $response) { |
|
153 | - try { |
|
154 | - $result = $this->auth($request, $response); |
|
155 | - return $result; |
|
156 | - } catch (NotAuthenticated $e) { |
|
157 | - throw $e; |
|
158 | - } catch (Exception $e) { |
|
159 | - $class = get_class($e); |
|
160 | - $msg = $e->getMessage(); |
|
161 | - \OC::$server->getLogger()->logException($e); |
|
162 | - throw new ServiceUnavailable("$class: $msg"); |
|
163 | - } |
|
164 | - } |
|
145 | + /** |
|
146 | + * @param RequestInterface $request |
|
147 | + * @param ResponseInterface $response |
|
148 | + * @return array |
|
149 | + * @throws NotAuthenticated |
|
150 | + * @throws ServiceUnavailable |
|
151 | + */ |
|
152 | + function check(RequestInterface $request, ResponseInterface $response) { |
|
153 | + try { |
|
154 | + $result = $this->auth($request, $response); |
|
155 | + return $result; |
|
156 | + } catch (NotAuthenticated $e) { |
|
157 | + throw $e; |
|
158 | + } catch (Exception $e) { |
|
159 | + $class = get_class($e); |
|
160 | + $msg = $e->getMessage(); |
|
161 | + \OC::$server->getLogger()->logException($e); |
|
162 | + throw new ServiceUnavailable("$class: $msg"); |
|
163 | + } |
|
164 | + } |
|
165 | 165 | |
166 | - /** |
|
167 | - * Checks whether a CSRF check is required on the request |
|
168 | - * |
|
169 | - * @return bool |
|
170 | - */ |
|
171 | - private function requiresCSRFCheck() { |
|
172 | - // GET requires no check at all |
|
173 | - if($this->request->getMethod() === 'GET') { |
|
174 | - return false; |
|
175 | - } |
|
166 | + /** |
|
167 | + * Checks whether a CSRF check is required on the request |
|
168 | + * |
|
169 | + * @return bool |
|
170 | + */ |
|
171 | + private function requiresCSRFCheck() { |
|
172 | + // GET requires no check at all |
|
173 | + if($this->request->getMethod() === 'GET') { |
|
174 | + return false; |
|
175 | + } |
|
176 | 176 | |
177 | - // Official Nextcloud clients require no checks |
|
178 | - if($this->request->isUserAgent([ |
|
179 | - IRequest::USER_AGENT_CLIENT_DESKTOP, |
|
180 | - IRequest::USER_AGENT_CLIENT_ANDROID, |
|
181 | - IRequest::USER_AGENT_CLIENT_IOS, |
|
182 | - ])) { |
|
183 | - return false; |
|
184 | - } |
|
177 | + // Official Nextcloud clients require no checks |
|
178 | + if($this->request->isUserAgent([ |
|
179 | + IRequest::USER_AGENT_CLIENT_DESKTOP, |
|
180 | + IRequest::USER_AGENT_CLIENT_ANDROID, |
|
181 | + IRequest::USER_AGENT_CLIENT_IOS, |
|
182 | + ])) { |
|
183 | + return false; |
|
184 | + } |
|
185 | 185 | |
186 | - // If not logged-in no check is required |
|
187 | - if(!$this->userSession->isLoggedIn()) { |
|
188 | - return false; |
|
189 | - } |
|
186 | + // If not logged-in no check is required |
|
187 | + if(!$this->userSession->isLoggedIn()) { |
|
188 | + return false; |
|
189 | + } |
|
190 | 190 | |
191 | - // POST always requires a check |
|
192 | - if($this->request->getMethod() === 'POST') { |
|
193 | - return true; |
|
194 | - } |
|
191 | + // POST always requires a check |
|
192 | + if($this->request->getMethod() === 'POST') { |
|
193 | + return true; |
|
194 | + } |
|
195 | 195 | |
196 | - // If logged-in AND DAV authenticated no check is required |
|
197 | - if($this->userSession->isLoggedIn() && |
|
198 | - $this->isDavAuthenticated($this->userSession->getUser()->getUID())) { |
|
199 | - return false; |
|
200 | - } |
|
196 | + // If logged-in AND DAV authenticated no check is required |
|
197 | + if($this->userSession->isLoggedIn() && |
|
198 | + $this->isDavAuthenticated($this->userSession->getUser()->getUID())) { |
|
199 | + return false; |
|
200 | + } |
|
201 | 201 | |
202 | - return true; |
|
203 | - } |
|
202 | + return true; |
|
203 | + } |
|
204 | 204 | |
205 | - /** |
|
206 | - * @param RequestInterface $request |
|
207 | - * @param ResponseInterface $response |
|
208 | - * @return array |
|
209 | - * @throws NotAuthenticated |
|
210 | - */ |
|
211 | - private function auth(RequestInterface $request, ResponseInterface $response) { |
|
212 | - $forcedLogout = false; |
|
205 | + /** |
|
206 | + * @param RequestInterface $request |
|
207 | + * @param ResponseInterface $response |
|
208 | + * @return array |
|
209 | + * @throws NotAuthenticated |
|
210 | + */ |
|
211 | + private function auth(RequestInterface $request, ResponseInterface $response) { |
|
212 | + $forcedLogout = false; |
|
213 | 213 | |
214 | - $authHeader = $request->getHeader('Authorization'); |
|
215 | - if (strpos($authHeader, 'Bearer ') !== false) { |
|
216 | - if($this->userSession->tryTokenLogin($this->request)) { |
|
217 | - $this->session->set(self::DAV_AUTHENTICATED, $this->userSession->getUser()->getUID()); |
|
218 | - $user = $this->userSession->getUser()->getUID(); |
|
219 | - \OC_Util::setupFS($user); |
|
220 | - $this->currentUser = $user; |
|
221 | - $this->session->close(); |
|
222 | - return [true, $this->principalPrefix . $user]; |
|
223 | - } |
|
224 | - } |
|
214 | + $authHeader = $request->getHeader('Authorization'); |
|
215 | + if (strpos($authHeader, 'Bearer ') !== false) { |
|
216 | + if($this->userSession->tryTokenLogin($this->request)) { |
|
217 | + $this->session->set(self::DAV_AUTHENTICATED, $this->userSession->getUser()->getUID()); |
|
218 | + $user = $this->userSession->getUser()->getUID(); |
|
219 | + \OC_Util::setupFS($user); |
|
220 | + $this->currentUser = $user; |
|
221 | + $this->session->close(); |
|
222 | + return [true, $this->principalPrefix . $user]; |
|
223 | + } |
|
224 | + } |
|
225 | 225 | |
226 | - if(!$this->request->passesCSRFCheck() && |
|
227 | - $this->requiresCSRFCheck()) { |
|
228 | - // In case of a fail with POST we need to recheck the credentials |
|
229 | - if($this->request->getMethod() === 'POST') { |
|
230 | - $forcedLogout = true; |
|
231 | - } else { |
|
232 | - $response->setStatus(401); |
|
233 | - throw new \Sabre\DAV\Exception\NotAuthenticated('CSRF check not passed.'); |
|
234 | - } |
|
235 | - } |
|
226 | + if(!$this->request->passesCSRFCheck() && |
|
227 | + $this->requiresCSRFCheck()) { |
|
228 | + // In case of a fail with POST we need to recheck the credentials |
|
229 | + if($this->request->getMethod() === 'POST') { |
|
230 | + $forcedLogout = true; |
|
231 | + } else { |
|
232 | + $response->setStatus(401); |
|
233 | + throw new \Sabre\DAV\Exception\NotAuthenticated('CSRF check not passed.'); |
|
234 | + } |
|
235 | + } |
|
236 | 236 | |
237 | - if($forcedLogout) { |
|
238 | - $this->userSession->logout(); |
|
239 | - } else { |
|
240 | - if($this->twoFactorManager->needsSecondFactor($this->userSession->getUser())) { |
|
241 | - throw new \Sabre\DAV\Exception\NotAuthenticated('2FA challenge not passed.'); |
|
242 | - } |
|
243 | - if (\OC_User::handleApacheAuth() || |
|
244 | - //Fix for broken webdav clients |
|
245 | - ($this->userSession->isLoggedIn() && is_null($this->session->get(self::DAV_AUTHENTICATED))) || |
|
246 | - //Well behaved clients that only send the cookie are allowed |
|
247 | - ($this->userSession->isLoggedIn() && $this->session->get(self::DAV_AUTHENTICATED) === $this->userSession->getUser()->getUID() && $request->getHeader('Authorization') === null) |
|
248 | - ) { |
|
249 | - $user = $this->userSession->getUser()->getUID(); |
|
250 | - \OC_Util::setupFS($user); |
|
251 | - $this->currentUser = $user; |
|
252 | - $this->session->close(); |
|
253 | - return [true, $this->principalPrefix . $user]; |
|
254 | - } |
|
255 | - } |
|
237 | + if($forcedLogout) { |
|
238 | + $this->userSession->logout(); |
|
239 | + } else { |
|
240 | + if($this->twoFactorManager->needsSecondFactor($this->userSession->getUser())) { |
|
241 | + throw new \Sabre\DAV\Exception\NotAuthenticated('2FA challenge not passed.'); |
|
242 | + } |
|
243 | + if (\OC_User::handleApacheAuth() || |
|
244 | + //Fix for broken webdav clients |
|
245 | + ($this->userSession->isLoggedIn() && is_null($this->session->get(self::DAV_AUTHENTICATED))) || |
|
246 | + //Well behaved clients that only send the cookie are allowed |
|
247 | + ($this->userSession->isLoggedIn() && $this->session->get(self::DAV_AUTHENTICATED) === $this->userSession->getUser()->getUID() && $request->getHeader('Authorization') === null) |
|
248 | + ) { |
|
249 | + $user = $this->userSession->getUser()->getUID(); |
|
250 | + \OC_Util::setupFS($user); |
|
251 | + $this->currentUser = $user; |
|
252 | + $this->session->close(); |
|
253 | + return [true, $this->principalPrefix . $user]; |
|
254 | + } |
|
255 | + } |
|
256 | 256 | |
257 | - if (!$this->userSession->isLoggedIn() && in_array('XMLHttpRequest', explode(',', $request->getHeader('X-Requested-With')))) { |
|
258 | - // do not re-authenticate over ajax, use dummy auth name to prevent browser popup |
|
259 | - $response->addHeader('WWW-Authenticate','DummyBasic realm="' . $this->realm . '"'); |
|
260 | - $response->setStatus(401); |
|
261 | - throw new \Sabre\DAV\Exception\NotAuthenticated('Cannot authenticate over ajax calls'); |
|
262 | - } |
|
257 | + if (!$this->userSession->isLoggedIn() && in_array('XMLHttpRequest', explode(',', $request->getHeader('X-Requested-With')))) { |
|
258 | + // do not re-authenticate over ajax, use dummy auth name to prevent browser popup |
|
259 | + $response->addHeader('WWW-Authenticate','DummyBasic realm="' . $this->realm . '"'); |
|
260 | + $response->setStatus(401); |
|
261 | + throw new \Sabre\DAV\Exception\NotAuthenticated('Cannot authenticate over ajax calls'); |
|
262 | + } |
|
263 | 263 | |
264 | - $data = parent::check($request, $response); |
|
265 | - if($data[0] === true) { |
|
266 | - $startPos = strrpos($data[1], '/') + 1; |
|
267 | - $user = $this->userSession->getUser()->getUID(); |
|
268 | - $data[1] = substr_replace($data[1], $user, $startPos); |
|
269 | - } |
|
270 | - return $data; |
|
271 | - } |
|
264 | + $data = parent::check($request, $response); |
|
265 | + if($data[0] === true) { |
|
266 | + $startPos = strrpos($data[1], '/') + 1; |
|
267 | + $user = $this->userSession->getUser()->getUID(); |
|
268 | + $data[1] = substr_replace($data[1], $user, $startPos); |
|
269 | + } |
|
270 | + return $data; |
|
271 | + } |
|
272 | 272 | } |