@@ -54,6 +54,6 @@ |
||
| 54 | 54 | </fieldset> |
| 55 | 55 | </div> |
| 56 | 56 | |
| 57 | -<?php if(empty($_['oauthState'])): ?> |
|
| 57 | +<?php if (empty($_['oauthState'])): ?> |
|
| 58 | 58 | <a id="app-token-login" class="warning" href="#"><?php p($l->t('Alternative login using app token')) ?></a> |
| 59 | 59 | <?php endif; ?> |
@@ -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 | } |
@@ -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 | } |
@@ -80,817 +80,817 @@ |
||
| 80 | 80 | */ |
| 81 | 81 | class Session implements IUserSession, Emitter { |
| 82 | 82 | |
| 83 | - /** @var IUserManager|PublicEmitter $manager */ |
|
| 84 | - private $manager; |
|
| 85 | - |
|
| 86 | - /** @var ISession $session */ |
|
| 87 | - private $session; |
|
| 88 | - |
|
| 89 | - /** @var ITimeFactory */ |
|
| 90 | - private $timeFactory; |
|
| 91 | - |
|
| 92 | - /** @var IProvider */ |
|
| 93 | - private $tokenProvider; |
|
| 94 | - |
|
| 95 | - /** @var IConfig */ |
|
| 96 | - private $config; |
|
| 97 | - |
|
| 98 | - /** @var User $activeUser */ |
|
| 99 | - protected $activeUser; |
|
| 100 | - |
|
| 101 | - /** @var ISecureRandom */ |
|
| 102 | - private $random; |
|
| 103 | - |
|
| 104 | - /** @var ILockdownManager */ |
|
| 105 | - private $lockdownManager; |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * @param IUserManager $manager |
|
| 109 | - * @param ISession $session |
|
| 110 | - * @param ITimeFactory $timeFactory |
|
| 111 | - * @param IProvider $tokenProvider |
|
| 112 | - * @param IConfig $config |
|
| 113 | - * @param ISecureRandom $random |
|
| 114 | - * @param ILockdownManager $lockdownManager |
|
| 115 | - */ |
|
| 116 | - public function __construct(IUserManager $manager, |
|
| 117 | - ISession $session, |
|
| 118 | - ITimeFactory $timeFactory, |
|
| 119 | - $tokenProvider, |
|
| 120 | - IConfig $config, |
|
| 121 | - ISecureRandom $random, |
|
| 122 | - ILockdownManager $lockdownManager |
|
| 123 | - ) { |
|
| 124 | - $this->manager = $manager; |
|
| 125 | - $this->session = $session; |
|
| 126 | - $this->timeFactory = $timeFactory; |
|
| 127 | - $this->tokenProvider = $tokenProvider; |
|
| 128 | - $this->config = $config; |
|
| 129 | - $this->random = $random; |
|
| 130 | - $this->lockdownManager = $lockdownManager; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * @param IProvider $provider |
|
| 135 | - */ |
|
| 136 | - public function setTokenProvider(IProvider $provider) { |
|
| 137 | - $this->tokenProvider = $provider; |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * @param string $scope |
|
| 142 | - * @param string $method |
|
| 143 | - * @param callable $callback |
|
| 144 | - */ |
|
| 145 | - public function listen($scope, $method, callable $callback) { |
|
| 146 | - $this->manager->listen($scope, $method, $callback); |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * @param string $scope optional |
|
| 151 | - * @param string $method optional |
|
| 152 | - * @param callable $callback optional |
|
| 153 | - */ |
|
| 154 | - public function removeListener($scope = null, $method = null, callable $callback = null) { |
|
| 155 | - $this->manager->removeListener($scope, $method, $callback); |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * get the manager object |
|
| 160 | - * |
|
| 161 | - * @return Manager|PublicEmitter |
|
| 162 | - */ |
|
| 163 | - public function getManager() { |
|
| 164 | - return $this->manager; |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * get the session object |
|
| 169 | - * |
|
| 170 | - * @return ISession |
|
| 171 | - */ |
|
| 172 | - public function getSession() { |
|
| 173 | - return $this->session; |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * set the session object |
|
| 178 | - * |
|
| 179 | - * @param ISession $session |
|
| 180 | - */ |
|
| 181 | - public function setSession(ISession $session) { |
|
| 182 | - if ($this->session instanceof ISession) { |
|
| 183 | - $this->session->close(); |
|
| 184 | - } |
|
| 185 | - $this->session = $session; |
|
| 186 | - $this->activeUser = null; |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * set the currently active user |
|
| 191 | - * |
|
| 192 | - * @param IUser|null $user |
|
| 193 | - */ |
|
| 194 | - public function setUser($user) { |
|
| 195 | - if (is_null($user)) { |
|
| 196 | - $this->session->remove('user_id'); |
|
| 197 | - } else { |
|
| 198 | - $this->session->set('user_id', $user->getUID()); |
|
| 199 | - } |
|
| 200 | - $this->activeUser = $user; |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - /** |
|
| 204 | - * get the current active user |
|
| 205 | - * |
|
| 206 | - * @return IUser|null Current user, otherwise null |
|
| 207 | - */ |
|
| 208 | - public function getUser() { |
|
| 209 | - // FIXME: This is a quick'n dirty work-around for the incognito mode as |
|
| 210 | - // described at https://github.com/owncloud/core/pull/12912#issuecomment-67391155 |
|
| 211 | - if (OC_User::isIncognitoMode()) { |
|
| 212 | - return null; |
|
| 213 | - } |
|
| 214 | - if (is_null($this->activeUser)) { |
|
| 215 | - $uid = $this->session->get('user_id'); |
|
| 216 | - if (is_null($uid)) { |
|
| 217 | - return null; |
|
| 218 | - } |
|
| 219 | - $this->activeUser = $this->manager->get($uid); |
|
| 220 | - if (is_null($this->activeUser)) { |
|
| 221 | - return null; |
|
| 222 | - } |
|
| 223 | - $this->validateSession(); |
|
| 224 | - } |
|
| 225 | - return $this->activeUser; |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - /** |
|
| 229 | - * Validate whether the current session is valid |
|
| 230 | - * |
|
| 231 | - * - For token-authenticated clients, the token validity is checked |
|
| 232 | - * - For browsers, the session token validity is checked |
|
| 233 | - */ |
|
| 234 | - protected function validateSession() { |
|
| 235 | - $token = null; |
|
| 236 | - $appPassword = $this->session->get('app_password'); |
|
| 237 | - |
|
| 238 | - if (is_null($appPassword)) { |
|
| 239 | - try { |
|
| 240 | - $token = $this->session->getId(); |
|
| 241 | - } catch (SessionNotAvailableException $ex) { |
|
| 242 | - return; |
|
| 243 | - } |
|
| 244 | - } else { |
|
| 245 | - $token = $appPassword; |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - if (!$this->validateToken($token)) { |
|
| 249 | - // Session was invalidated |
|
| 250 | - $this->logout(); |
|
| 251 | - } |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - /** |
|
| 255 | - * Checks whether the user is logged in |
|
| 256 | - * |
|
| 257 | - * @return bool if logged in |
|
| 258 | - */ |
|
| 259 | - public function isLoggedIn() { |
|
| 260 | - $user = $this->getUser(); |
|
| 261 | - if (is_null($user)) { |
|
| 262 | - return false; |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - return $user->isEnabled(); |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - /** |
|
| 269 | - * set the login name |
|
| 270 | - * |
|
| 271 | - * @param string|null $loginName for the logged in user |
|
| 272 | - */ |
|
| 273 | - public function setLoginName($loginName) { |
|
| 274 | - if (is_null($loginName)) { |
|
| 275 | - $this->session->remove('loginname'); |
|
| 276 | - } else { |
|
| 277 | - $this->session->set('loginname', $loginName); |
|
| 278 | - } |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - /** |
|
| 282 | - * get the login name of the current user |
|
| 283 | - * |
|
| 284 | - * @return string |
|
| 285 | - */ |
|
| 286 | - public function getLoginName() { |
|
| 287 | - if ($this->activeUser) { |
|
| 288 | - return $this->session->get('loginname'); |
|
| 289 | - } else { |
|
| 290 | - $uid = $this->session->get('user_id'); |
|
| 291 | - if ($uid) { |
|
| 292 | - $this->activeUser = $this->manager->get($uid); |
|
| 293 | - return $this->session->get('loginname'); |
|
| 294 | - } else { |
|
| 295 | - return null; |
|
| 296 | - } |
|
| 297 | - } |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - /** |
|
| 301 | - * set the token id |
|
| 302 | - * |
|
| 303 | - * @param int|null $token that was used to log in |
|
| 304 | - */ |
|
| 305 | - protected function setToken($token) { |
|
| 306 | - if ($token === null) { |
|
| 307 | - $this->session->remove('token-id'); |
|
| 308 | - } else { |
|
| 309 | - $this->session->set('token-id', $token); |
|
| 310 | - } |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - /** |
|
| 314 | - * try to log in with the provided credentials |
|
| 315 | - * |
|
| 316 | - * @param string $uid |
|
| 317 | - * @param string $password |
|
| 318 | - * @return boolean|null |
|
| 319 | - * @throws LoginException |
|
| 320 | - */ |
|
| 321 | - public function login($uid, $password) { |
|
| 322 | - $this->session->regenerateId(); |
|
| 323 | - if ($this->validateToken($password, $uid)) { |
|
| 324 | - return $this->loginWithToken($password); |
|
| 325 | - } |
|
| 326 | - return $this->loginWithPassword($uid, $password); |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - /** |
|
| 330 | - * @param IUser $user |
|
| 331 | - * @param array $loginDetails |
|
| 332 | - * @param bool $regenerateSessionId |
|
| 333 | - * @return true returns true if login successful or an exception otherwise |
|
| 334 | - * @throws LoginException |
|
| 335 | - */ |
|
| 336 | - public function completeLogin(IUser $user, array $loginDetails, $regenerateSessionId = true) { |
|
| 337 | - if (!$user->isEnabled()) { |
|
| 338 | - // disabled users can not log in |
|
| 339 | - // injecting l10n does not work - there is a circular dependency between session and \OCP\L10N\IFactory |
|
| 340 | - $message = \OC::$server->getL10N('lib')->t('User disabled'); |
|
| 341 | - throw new LoginException($message); |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - if($regenerateSessionId) { |
|
| 345 | - $this->session->regenerateId(); |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - $this->setUser($user); |
|
| 349 | - $this->setLoginName($loginDetails['loginName']); |
|
| 350 | - |
|
| 351 | - if(isset($loginDetails['token']) && $loginDetails['token'] instanceof IToken) { |
|
| 352 | - $this->setToken($loginDetails['token']->getId()); |
|
| 353 | - $this->lockdownManager->setToken($loginDetails['token']); |
|
| 354 | - $firstTimeLogin = false; |
|
| 355 | - } else { |
|
| 356 | - $this->setToken(null); |
|
| 357 | - $firstTimeLogin = $user->updateLastLoginTimestamp(); |
|
| 358 | - } |
|
| 359 | - $this->manager->emit('\OC\User', 'postLogin', [$user, $loginDetails['password']]); |
|
| 360 | - if($this->isLoggedIn()) { |
|
| 361 | - $this->prepareUserLogin($firstTimeLogin); |
|
| 362 | - return true; |
|
| 363 | - } else { |
|
| 364 | - $message = \OC::$server->getL10N('lib')->t('Login canceled by app'); |
|
| 365 | - throw new LoginException($message); |
|
| 366 | - } |
|
| 367 | - } |
|
| 368 | - |
|
| 369 | - /** |
|
| 370 | - * Tries to log in a client |
|
| 371 | - * |
|
| 372 | - * Checks token auth enforced |
|
| 373 | - * Checks 2FA enabled |
|
| 374 | - * |
|
| 375 | - * @param string $user |
|
| 376 | - * @param string $password |
|
| 377 | - * @param IRequest $request |
|
| 378 | - * @param OC\Security\Bruteforce\Throttler $throttler |
|
| 379 | - * @throws LoginException |
|
| 380 | - * @throws PasswordLoginForbiddenException |
|
| 381 | - * @return boolean |
|
| 382 | - */ |
|
| 383 | - public function logClientIn($user, |
|
| 384 | - $password, |
|
| 385 | - IRequest $request, |
|
| 386 | - OC\Security\Bruteforce\Throttler $throttler) { |
|
| 387 | - $currentDelay = $throttler->sleepDelay($request->getRemoteAddress(), 'login'); |
|
| 388 | - |
|
| 389 | - if ($this->manager instanceof PublicEmitter) { |
|
| 390 | - $this->manager->emit('\OC\User', 'preLogin', array($user, $password)); |
|
| 391 | - } |
|
| 392 | - |
|
| 393 | - $isTokenPassword = $this->isTokenPassword($password); |
|
| 394 | - if (!$isTokenPassword && $this->isTokenAuthEnforced()) { |
|
| 395 | - throw new PasswordLoginForbiddenException(); |
|
| 396 | - } |
|
| 397 | - if (!$isTokenPassword && $this->isTwoFactorEnforced($user)) { |
|
| 398 | - throw new PasswordLoginForbiddenException(); |
|
| 399 | - } |
|
| 400 | - if (!$this->login($user, $password) ) { |
|
| 401 | - $users = $this->manager->getByEmail($user); |
|
| 402 | - if (count($users) === 1) { |
|
| 403 | - return $this->login($users[0]->getUID(), $password); |
|
| 404 | - } |
|
| 405 | - |
|
| 406 | - $throttler->registerAttempt('login', $request->getRemoteAddress(), ['uid' => $user]); |
|
| 407 | - if($currentDelay === 0) { |
|
| 408 | - $throttler->sleepDelay($request->getRemoteAddress(), 'login'); |
|
| 409 | - } |
|
| 410 | - return false; |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - if ($isTokenPassword) { |
|
| 414 | - $this->session->set('app_password', $password); |
|
| 415 | - } else if($this->supportsCookies($request)) { |
|
| 416 | - // Password login, but cookies supported -> create (browser) session token |
|
| 417 | - $this->createSessionToken($request, $this->getUser()->getUID(), $user, $password); |
|
| 418 | - } |
|
| 419 | - |
|
| 420 | - return true; |
|
| 421 | - } |
|
| 422 | - |
|
| 423 | - protected function supportsCookies(IRequest $request) { |
|
| 424 | - if (!is_null($request->getCookie('cookie_test'))) { |
|
| 425 | - return true; |
|
| 426 | - } |
|
| 427 | - setcookie('cookie_test', 'test', $this->timeFactory->getTime() + 3600); |
|
| 428 | - return false; |
|
| 429 | - } |
|
| 430 | - |
|
| 431 | - private function isTokenAuthEnforced() { |
|
| 432 | - return $this->config->getSystemValue('token_auth_enforced', false); |
|
| 433 | - } |
|
| 434 | - |
|
| 435 | - protected function isTwoFactorEnforced($username) { |
|
| 436 | - Util::emitHook( |
|
| 437 | - '\OCA\Files_Sharing\API\Server2Server', |
|
| 438 | - 'preLoginNameUsedAsUserName', |
|
| 439 | - array('uid' => &$username) |
|
| 440 | - ); |
|
| 441 | - $user = $this->manager->get($username); |
|
| 442 | - if (is_null($user)) { |
|
| 443 | - $users = $this->manager->getByEmail($username); |
|
| 444 | - if (empty($users)) { |
|
| 445 | - return false; |
|
| 446 | - } |
|
| 447 | - if (count($users) !== 1) { |
|
| 448 | - return true; |
|
| 449 | - } |
|
| 450 | - $user = $users[0]; |
|
| 451 | - } |
|
| 452 | - // DI not possible due to cyclic dependencies :'-/ |
|
| 453 | - return OC::$server->getTwoFactorAuthManager()->isTwoFactorAuthenticated($user); |
|
| 454 | - } |
|
| 455 | - |
|
| 456 | - /** |
|
| 457 | - * Check if the given 'password' is actually a device token |
|
| 458 | - * |
|
| 459 | - * @param string $password |
|
| 460 | - * @return boolean |
|
| 461 | - */ |
|
| 462 | - public function isTokenPassword($password) { |
|
| 463 | - try { |
|
| 464 | - $this->tokenProvider->getToken($password); |
|
| 465 | - return true; |
|
| 466 | - } catch (InvalidTokenException $ex) { |
|
| 467 | - return false; |
|
| 468 | - } |
|
| 469 | - } |
|
| 470 | - |
|
| 471 | - protected function prepareUserLogin($firstTimeLogin) { |
|
| 472 | - // TODO: mock/inject/use non-static |
|
| 473 | - // Refresh the token |
|
| 474 | - \OC::$server->getCsrfTokenManager()->refreshToken(); |
|
| 475 | - //we need to pass the user name, which may differ from login name |
|
| 476 | - $user = $this->getUser()->getUID(); |
|
| 477 | - OC_Util::setupFS($user); |
|
| 478 | - |
|
| 479 | - if ($firstTimeLogin) { |
|
| 480 | - // TODO: lock necessary? |
|
| 481 | - //trigger creation of user home and /files folder |
|
| 482 | - $userFolder = \OC::$server->getUserFolder($user); |
|
| 483 | - |
|
| 484 | - try { |
|
| 485 | - // copy skeleton |
|
| 486 | - \OC_Util::copySkeleton($user, $userFolder); |
|
| 487 | - } catch (NotPermittedException $ex) { |
|
| 488 | - // read only uses |
|
| 489 | - } |
|
| 490 | - |
|
| 491 | - // trigger any other initialization |
|
| 492 | - \OC::$server->getEventDispatcher()->dispatch(IUser::class . '::firstLogin', new GenericEvent($this->getUser())); |
|
| 493 | - } |
|
| 494 | - } |
|
| 495 | - |
|
| 496 | - /** |
|
| 497 | - * Tries to login the user with HTTP Basic Authentication |
|
| 498 | - * |
|
| 499 | - * @todo do not allow basic auth if the user is 2FA enforced |
|
| 500 | - * @param IRequest $request |
|
| 501 | - * @param OC\Security\Bruteforce\Throttler $throttler |
|
| 502 | - * @return boolean if the login was successful |
|
| 503 | - */ |
|
| 504 | - public function tryBasicAuthLogin(IRequest $request, |
|
| 505 | - OC\Security\Bruteforce\Throttler $throttler) { |
|
| 506 | - if (!empty($request->server['PHP_AUTH_USER']) && !empty($request->server['PHP_AUTH_PW'])) { |
|
| 507 | - try { |
|
| 508 | - if ($this->logClientIn($request->server['PHP_AUTH_USER'], $request->server['PHP_AUTH_PW'], $request, $throttler)) { |
|
| 509 | - /** |
|
| 510 | - * Add DAV authenticated. This should in an ideal world not be |
|
| 511 | - * necessary but the iOS App reads cookies from anywhere instead |
|
| 512 | - * only the DAV endpoint. |
|
| 513 | - * This makes sure that the cookies will be valid for the whole scope |
|
| 514 | - * @see https://github.com/owncloud/core/issues/22893 |
|
| 515 | - */ |
|
| 516 | - $this->session->set( |
|
| 517 | - Auth::DAV_AUTHENTICATED, $this->getUser()->getUID() |
|
| 518 | - ); |
|
| 519 | - |
|
| 520 | - // Set the last-password-confirm session to make the sudo mode work |
|
| 521 | - $this->session->set('last-password-confirm', $this->timeFactory->getTime()); |
|
| 522 | - |
|
| 523 | - return true; |
|
| 524 | - } |
|
| 525 | - } catch (PasswordLoginForbiddenException $ex) { |
|
| 526 | - // Nothing to do |
|
| 527 | - } |
|
| 528 | - } |
|
| 529 | - return false; |
|
| 530 | - } |
|
| 531 | - |
|
| 532 | - /** |
|
| 533 | - * Log an user in via login name and password |
|
| 534 | - * |
|
| 535 | - * @param string $uid |
|
| 536 | - * @param string $password |
|
| 537 | - * @return boolean |
|
| 538 | - * @throws LoginException if an app canceld the login process or the user is not enabled |
|
| 539 | - */ |
|
| 540 | - private function loginWithPassword($uid, $password) { |
|
| 541 | - $user = $this->manager->checkPassword($uid, $password); |
|
| 542 | - if ($user === false) { |
|
| 543 | - // Password check failed |
|
| 544 | - return false; |
|
| 545 | - } |
|
| 546 | - |
|
| 547 | - return $this->completeLogin($user, ['loginName' => $uid, 'password' => $password], false); |
|
| 548 | - } |
|
| 549 | - |
|
| 550 | - /** |
|
| 551 | - * Log an user in with a given token (id) |
|
| 552 | - * |
|
| 553 | - * @param string $token |
|
| 554 | - * @return boolean |
|
| 555 | - * @throws LoginException if an app canceled the login process or the user is not enabled |
|
| 556 | - */ |
|
| 557 | - private function loginWithToken($token) { |
|
| 558 | - try { |
|
| 559 | - $dbToken = $this->tokenProvider->getToken($token); |
|
| 560 | - } catch (InvalidTokenException $ex) { |
|
| 561 | - return false; |
|
| 562 | - } |
|
| 563 | - $uid = $dbToken->getUID(); |
|
| 564 | - |
|
| 565 | - // When logging in with token, the password must be decrypted first before passing to login hook |
|
| 566 | - $password = ''; |
|
| 567 | - try { |
|
| 568 | - $password = $this->tokenProvider->getPassword($dbToken, $token); |
|
| 569 | - } catch (PasswordlessTokenException $ex) { |
|
| 570 | - // Ignore and use empty string instead |
|
| 571 | - } |
|
| 572 | - |
|
| 573 | - $this->manager->emit('\OC\User', 'preLogin', array($uid, $password)); |
|
| 574 | - |
|
| 575 | - $user = $this->manager->get($uid); |
|
| 576 | - if (is_null($user)) { |
|
| 577 | - // user does not exist |
|
| 578 | - return false; |
|
| 579 | - } |
|
| 580 | - |
|
| 581 | - return $this->completeLogin( |
|
| 582 | - $user, |
|
| 583 | - [ |
|
| 584 | - 'loginName' => $dbToken->getLoginName(), |
|
| 585 | - 'password' => $password, |
|
| 586 | - 'token' => $dbToken |
|
| 587 | - ], |
|
| 588 | - false); |
|
| 589 | - } |
|
| 590 | - |
|
| 591 | - /** |
|
| 592 | - * Create a new session token for the given user credentials |
|
| 593 | - * |
|
| 594 | - * @param IRequest $request |
|
| 595 | - * @param string $uid user UID |
|
| 596 | - * @param string $loginName login name |
|
| 597 | - * @param string $password |
|
| 598 | - * @param int $remember |
|
| 599 | - * @return boolean |
|
| 600 | - */ |
|
| 601 | - public function createSessionToken(IRequest $request, $uid, $loginName, $password = null, $remember = IToken::DO_NOT_REMEMBER) { |
|
| 602 | - if (is_null($this->manager->get($uid))) { |
|
| 603 | - // User does not exist |
|
| 604 | - return false; |
|
| 605 | - } |
|
| 606 | - $name = isset($request->server['HTTP_USER_AGENT']) ? $request->server['HTTP_USER_AGENT'] : 'unknown browser'; |
|
| 607 | - try { |
|
| 608 | - $sessionId = $this->session->getId(); |
|
| 609 | - $pwd = $this->getPassword($password); |
|
| 610 | - $this->tokenProvider->generateToken($sessionId, $uid, $loginName, $pwd, $name, IToken::TEMPORARY_TOKEN, $remember); |
|
| 611 | - return true; |
|
| 612 | - } catch (SessionNotAvailableException $ex) { |
|
| 613 | - // This can happen with OCC, where a memory session is used |
|
| 614 | - // if a memory session is used, we shouldn't create a session token anyway |
|
| 615 | - return false; |
|
| 616 | - } |
|
| 617 | - } |
|
| 618 | - |
|
| 619 | - /** |
|
| 620 | - * Checks if the given password is a token. |
|
| 621 | - * If yes, the password is extracted from the token. |
|
| 622 | - * If no, the same password is returned. |
|
| 623 | - * |
|
| 624 | - * @param string $password either the login password or a device token |
|
| 625 | - * @return string|null the password or null if none was set in the token |
|
| 626 | - */ |
|
| 627 | - private function getPassword($password) { |
|
| 628 | - if (is_null($password)) { |
|
| 629 | - // This is surely no token ;-) |
|
| 630 | - return null; |
|
| 631 | - } |
|
| 632 | - try { |
|
| 633 | - $token = $this->tokenProvider->getToken($password); |
|
| 634 | - try { |
|
| 635 | - return $this->tokenProvider->getPassword($token, $password); |
|
| 636 | - } catch (PasswordlessTokenException $ex) { |
|
| 637 | - return null; |
|
| 638 | - } |
|
| 639 | - } catch (InvalidTokenException $ex) { |
|
| 640 | - return $password; |
|
| 641 | - } |
|
| 642 | - } |
|
| 643 | - |
|
| 644 | - /** |
|
| 645 | - * @param IToken $dbToken |
|
| 646 | - * @param string $token |
|
| 647 | - * @return boolean |
|
| 648 | - */ |
|
| 649 | - private function checkTokenCredentials(IToken $dbToken, $token) { |
|
| 650 | - // Check whether login credentials are still valid and the user was not disabled |
|
| 651 | - // This check is performed each 5 minutes |
|
| 652 | - $lastCheck = $dbToken->getLastCheck() ? : 0; |
|
| 653 | - $now = $this->timeFactory->getTime(); |
|
| 654 | - if ($lastCheck > ($now - 60 * 5)) { |
|
| 655 | - // Checked performed recently, nothing to do now |
|
| 656 | - return true; |
|
| 657 | - } |
|
| 658 | - |
|
| 659 | - try { |
|
| 660 | - $pwd = $this->tokenProvider->getPassword($dbToken, $token); |
|
| 661 | - } catch (InvalidTokenException $ex) { |
|
| 662 | - // An invalid token password was used -> log user out |
|
| 663 | - return false; |
|
| 664 | - } catch (PasswordlessTokenException $ex) { |
|
| 665 | - // Token has no password |
|
| 666 | - |
|
| 667 | - if (!is_null($this->activeUser) && !$this->activeUser->isEnabled()) { |
|
| 668 | - $this->tokenProvider->invalidateToken($token); |
|
| 669 | - return false; |
|
| 670 | - } |
|
| 671 | - |
|
| 672 | - $dbToken->setLastCheck($now); |
|
| 673 | - return true; |
|
| 674 | - } |
|
| 675 | - |
|
| 676 | - if ($this->manager->checkPassword($dbToken->getLoginName(), $pwd) === false |
|
| 677 | - || (!is_null($this->activeUser) && !$this->activeUser->isEnabled())) { |
|
| 678 | - $this->tokenProvider->invalidateToken($token); |
|
| 679 | - // Password has changed or user was disabled -> log user out |
|
| 680 | - return false; |
|
| 681 | - } |
|
| 682 | - $dbToken->setLastCheck($now); |
|
| 683 | - return true; |
|
| 684 | - } |
|
| 685 | - |
|
| 686 | - /** |
|
| 687 | - * Check if the given token exists and performs password/user-enabled checks |
|
| 688 | - * |
|
| 689 | - * Invalidates the token if checks fail |
|
| 690 | - * |
|
| 691 | - * @param string $token |
|
| 692 | - * @param string $user login name |
|
| 693 | - * @return boolean |
|
| 694 | - */ |
|
| 695 | - private function validateToken($token, $user = null) { |
|
| 696 | - try { |
|
| 697 | - $dbToken = $this->tokenProvider->getToken($token); |
|
| 698 | - } catch (InvalidTokenException $ex) { |
|
| 699 | - return false; |
|
| 700 | - } |
|
| 701 | - |
|
| 702 | - // Check if login names match |
|
| 703 | - if (!is_null($user) && $dbToken->getLoginName() !== $user) { |
|
| 704 | - // TODO: this makes it imposssible to use different login names on browser and client |
|
| 705 | - // e.g. login by e-mail '[email protected]' on browser for generating the token will not |
|
| 706 | - // allow to use the client token with the login name 'user'. |
|
| 707 | - return false; |
|
| 708 | - } |
|
| 709 | - |
|
| 710 | - if (!$this->checkTokenCredentials($dbToken, $token)) { |
|
| 711 | - return false; |
|
| 712 | - } |
|
| 713 | - |
|
| 714 | - $this->tokenProvider->updateTokenActivity($dbToken); |
|
| 715 | - |
|
| 716 | - return true; |
|
| 717 | - } |
|
| 718 | - |
|
| 719 | - /** |
|
| 720 | - * Tries to login the user with auth token header |
|
| 721 | - * |
|
| 722 | - * @param IRequest $request |
|
| 723 | - * @todo check remember me cookie |
|
| 724 | - * @return boolean |
|
| 725 | - */ |
|
| 726 | - public function tryTokenLogin(IRequest $request) { |
|
| 727 | - $authHeader = $request->getHeader('Authorization'); |
|
| 728 | - if (strpos($authHeader, 'Bearer ') === false) { |
|
| 729 | - // No auth header, let's try session id |
|
| 730 | - try { |
|
| 731 | - $token = $this->session->getId(); |
|
| 732 | - } catch (SessionNotAvailableException $ex) { |
|
| 733 | - return false; |
|
| 734 | - } |
|
| 735 | - } else { |
|
| 736 | - $token = substr($authHeader, 7); |
|
| 737 | - } |
|
| 738 | - |
|
| 739 | - if (!$this->loginWithToken($token)) { |
|
| 740 | - return false; |
|
| 741 | - } |
|
| 742 | - if(!$this->validateToken($token)) { |
|
| 743 | - return false; |
|
| 744 | - } |
|
| 745 | - return true; |
|
| 746 | - } |
|
| 747 | - |
|
| 748 | - /** |
|
| 749 | - * perform login using the magic cookie (remember login) |
|
| 750 | - * |
|
| 751 | - * @param string $uid the username |
|
| 752 | - * @param string $currentToken |
|
| 753 | - * @param string $oldSessionId |
|
| 754 | - * @return bool |
|
| 755 | - */ |
|
| 756 | - public function loginWithCookie($uid, $currentToken, $oldSessionId) { |
|
| 757 | - $this->session->regenerateId(); |
|
| 758 | - $this->manager->emit('\OC\User', 'preRememberedLogin', array($uid)); |
|
| 759 | - $user = $this->manager->get($uid); |
|
| 760 | - if (is_null($user)) { |
|
| 761 | - // user does not exist |
|
| 762 | - return false; |
|
| 763 | - } |
|
| 764 | - |
|
| 765 | - // get stored tokens |
|
| 766 | - $tokens = $this->config->getUserKeys($uid, 'login_token'); |
|
| 767 | - // test cookies token against stored tokens |
|
| 768 | - if (!in_array($currentToken, $tokens, true)) { |
|
| 769 | - return false; |
|
| 770 | - } |
|
| 771 | - // replace successfully used token with a new one |
|
| 772 | - $this->config->deleteUserValue($uid, 'login_token', $currentToken); |
|
| 773 | - $newToken = $this->random->generate(32); |
|
| 774 | - $this->config->setUserValue($uid, 'login_token', $newToken, $this->timeFactory->getTime()); |
|
| 775 | - |
|
| 776 | - try { |
|
| 777 | - $sessionId = $this->session->getId(); |
|
| 778 | - $this->tokenProvider->renewSessionToken($oldSessionId, $sessionId); |
|
| 779 | - } catch (SessionNotAvailableException $ex) { |
|
| 780 | - return false; |
|
| 781 | - } catch (InvalidTokenException $ex) { |
|
| 782 | - \OC::$server->getLogger()->warning('Renewing session token failed', ['app' => 'core']); |
|
| 783 | - return false; |
|
| 784 | - } |
|
| 785 | - |
|
| 786 | - $this->setMagicInCookie($user->getUID(), $newToken); |
|
| 787 | - $token = $this->tokenProvider->getToken($sessionId); |
|
| 788 | - |
|
| 789 | - //login |
|
| 790 | - $this->setUser($user); |
|
| 791 | - $this->setLoginName($token->getLoginName()); |
|
| 792 | - $this->setToken($token->getId()); |
|
| 793 | - $this->lockdownManager->setToken($token); |
|
| 794 | - $user->updateLastLoginTimestamp(); |
|
| 795 | - $password = null; |
|
| 796 | - try { |
|
| 797 | - $password = $this->tokenProvider->getPassword($token, $sessionId); |
|
| 798 | - } catch (PasswordlessTokenException $ex) { |
|
| 799 | - // Ignore |
|
| 800 | - } |
|
| 801 | - $this->manager->emit('\OC\User', 'postRememberedLogin', [$user, $password]); |
|
| 802 | - return true; |
|
| 803 | - } |
|
| 804 | - |
|
| 805 | - /** |
|
| 806 | - * @param IUser $user |
|
| 807 | - */ |
|
| 808 | - public function createRememberMeToken(IUser $user) { |
|
| 809 | - $token = $this->random->generate(32); |
|
| 810 | - $this->config->setUserValue($user->getUID(), 'login_token', $token, $this->timeFactory->getTime()); |
|
| 811 | - $this->setMagicInCookie($user->getUID(), $token); |
|
| 812 | - } |
|
| 813 | - |
|
| 814 | - /** |
|
| 815 | - * logout the user from the session |
|
| 816 | - */ |
|
| 817 | - public function logout() { |
|
| 818 | - $this->manager->emit('\OC\User', 'logout'); |
|
| 819 | - $user = $this->getUser(); |
|
| 820 | - if (!is_null($user)) { |
|
| 821 | - try { |
|
| 822 | - $this->tokenProvider->invalidateToken($this->session->getId()); |
|
| 823 | - } catch (SessionNotAvailableException $ex) { |
|
| 824 | - |
|
| 825 | - } |
|
| 826 | - } |
|
| 827 | - $this->setUser(null); |
|
| 828 | - $this->setLoginName(null); |
|
| 829 | - $this->setToken(null); |
|
| 830 | - $this->unsetMagicInCookie(); |
|
| 831 | - $this->session->clear(); |
|
| 832 | - $this->manager->emit('\OC\User', 'postLogout'); |
|
| 833 | - } |
|
| 834 | - |
|
| 835 | - /** |
|
| 836 | - * Set cookie value to use in next page load |
|
| 837 | - * |
|
| 838 | - * @param string $username username to be set |
|
| 839 | - * @param string $token |
|
| 840 | - */ |
|
| 841 | - public function setMagicInCookie($username, $token) { |
|
| 842 | - $secureCookie = OC::$server->getRequest()->getServerProtocol() === 'https'; |
|
| 843 | - $webRoot = \OC::$WEBROOT; |
|
| 844 | - if ($webRoot === '') { |
|
| 845 | - $webRoot = '/'; |
|
| 846 | - } |
|
| 847 | - |
|
| 848 | - $expires = $this->timeFactory->getTime() + $this->config->getSystemValue('remember_login_cookie_lifetime', 60 * 60 * 24 * 15); |
|
| 849 | - setcookie('nc_username', $username, $expires, $webRoot, '', $secureCookie, true); |
|
| 850 | - setcookie('nc_token', $token, $expires, $webRoot, '', $secureCookie, true); |
|
| 851 | - try { |
|
| 852 | - setcookie('nc_session_id', $this->session->getId(), $expires, $webRoot, '', $secureCookie, true); |
|
| 853 | - } catch (SessionNotAvailableException $ex) { |
|
| 854 | - // ignore |
|
| 855 | - } |
|
| 856 | - } |
|
| 857 | - |
|
| 858 | - /** |
|
| 859 | - * Remove cookie for "remember username" |
|
| 860 | - */ |
|
| 861 | - public function unsetMagicInCookie() { |
|
| 862 | - //TODO: DI for cookies and IRequest |
|
| 863 | - $secureCookie = OC::$server->getRequest()->getServerProtocol() === 'https'; |
|
| 864 | - |
|
| 865 | - unset($_COOKIE['nc_username']); //TODO: DI |
|
| 866 | - unset($_COOKIE['nc_token']); |
|
| 867 | - unset($_COOKIE['nc_session_id']); |
|
| 868 | - setcookie('nc_username', '', $this->timeFactory->getTime() - 3600, OC::$WEBROOT, '', $secureCookie, true); |
|
| 869 | - setcookie('nc_token', '', $this->timeFactory->getTime() - 3600, OC::$WEBROOT, '', $secureCookie, true); |
|
| 870 | - setcookie('nc_session_id', '', $this->timeFactory->getTime() - 3600, OC::$WEBROOT, '', $secureCookie, true); |
|
| 871 | - // old cookies might be stored under /webroot/ instead of /webroot |
|
| 872 | - // and Firefox doesn't like it! |
|
| 873 | - setcookie('nc_username', '', $this->timeFactory->getTime() - 3600, OC::$WEBROOT . '/', '', $secureCookie, true); |
|
| 874 | - setcookie('nc_token', '', $this->timeFactory->getTime() - 3600, OC::$WEBROOT . '/', '', $secureCookie, true); |
|
| 875 | - setcookie('nc_session_id', '', $this->timeFactory->getTime() - 3600, OC::$WEBROOT . '/', '', $secureCookie, true); |
|
| 876 | - } |
|
| 877 | - |
|
| 878 | - /** |
|
| 879 | - * Update password of the browser session token if there is one |
|
| 880 | - * |
|
| 881 | - * @param string $password |
|
| 882 | - */ |
|
| 883 | - public function updateSessionTokenPassword($password) { |
|
| 884 | - try { |
|
| 885 | - $sessionId = $this->session->getId(); |
|
| 886 | - $token = $this->tokenProvider->getToken($sessionId); |
|
| 887 | - $this->tokenProvider->setPassword($token, $sessionId, $password); |
|
| 888 | - } catch (SessionNotAvailableException $ex) { |
|
| 889 | - // Nothing to do |
|
| 890 | - } catch (InvalidTokenException $ex) { |
|
| 891 | - // Nothing to do |
|
| 892 | - } |
|
| 893 | - } |
|
| 83 | + /** @var IUserManager|PublicEmitter $manager */ |
|
| 84 | + private $manager; |
|
| 85 | + |
|
| 86 | + /** @var ISession $session */ |
|
| 87 | + private $session; |
|
| 88 | + |
|
| 89 | + /** @var ITimeFactory */ |
|
| 90 | + private $timeFactory; |
|
| 91 | + |
|
| 92 | + /** @var IProvider */ |
|
| 93 | + private $tokenProvider; |
|
| 94 | + |
|
| 95 | + /** @var IConfig */ |
|
| 96 | + private $config; |
|
| 97 | + |
|
| 98 | + /** @var User $activeUser */ |
|
| 99 | + protected $activeUser; |
|
| 100 | + |
|
| 101 | + /** @var ISecureRandom */ |
|
| 102 | + private $random; |
|
| 103 | + |
|
| 104 | + /** @var ILockdownManager */ |
|
| 105 | + private $lockdownManager; |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * @param IUserManager $manager |
|
| 109 | + * @param ISession $session |
|
| 110 | + * @param ITimeFactory $timeFactory |
|
| 111 | + * @param IProvider $tokenProvider |
|
| 112 | + * @param IConfig $config |
|
| 113 | + * @param ISecureRandom $random |
|
| 114 | + * @param ILockdownManager $lockdownManager |
|
| 115 | + */ |
|
| 116 | + public function __construct(IUserManager $manager, |
|
| 117 | + ISession $session, |
|
| 118 | + ITimeFactory $timeFactory, |
|
| 119 | + $tokenProvider, |
|
| 120 | + IConfig $config, |
|
| 121 | + ISecureRandom $random, |
|
| 122 | + ILockdownManager $lockdownManager |
|
| 123 | + ) { |
|
| 124 | + $this->manager = $manager; |
|
| 125 | + $this->session = $session; |
|
| 126 | + $this->timeFactory = $timeFactory; |
|
| 127 | + $this->tokenProvider = $tokenProvider; |
|
| 128 | + $this->config = $config; |
|
| 129 | + $this->random = $random; |
|
| 130 | + $this->lockdownManager = $lockdownManager; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * @param IProvider $provider |
|
| 135 | + */ |
|
| 136 | + public function setTokenProvider(IProvider $provider) { |
|
| 137 | + $this->tokenProvider = $provider; |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * @param string $scope |
|
| 142 | + * @param string $method |
|
| 143 | + * @param callable $callback |
|
| 144 | + */ |
|
| 145 | + public function listen($scope, $method, callable $callback) { |
|
| 146 | + $this->manager->listen($scope, $method, $callback); |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * @param string $scope optional |
|
| 151 | + * @param string $method optional |
|
| 152 | + * @param callable $callback optional |
|
| 153 | + */ |
|
| 154 | + public function removeListener($scope = null, $method = null, callable $callback = null) { |
|
| 155 | + $this->manager->removeListener($scope, $method, $callback); |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * get the manager object |
|
| 160 | + * |
|
| 161 | + * @return Manager|PublicEmitter |
|
| 162 | + */ |
|
| 163 | + public function getManager() { |
|
| 164 | + return $this->manager; |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * get the session object |
|
| 169 | + * |
|
| 170 | + * @return ISession |
|
| 171 | + */ |
|
| 172 | + public function getSession() { |
|
| 173 | + return $this->session; |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * set the session object |
|
| 178 | + * |
|
| 179 | + * @param ISession $session |
|
| 180 | + */ |
|
| 181 | + public function setSession(ISession $session) { |
|
| 182 | + if ($this->session instanceof ISession) { |
|
| 183 | + $this->session->close(); |
|
| 184 | + } |
|
| 185 | + $this->session = $session; |
|
| 186 | + $this->activeUser = null; |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * set the currently active user |
|
| 191 | + * |
|
| 192 | + * @param IUser|null $user |
|
| 193 | + */ |
|
| 194 | + public function setUser($user) { |
|
| 195 | + if (is_null($user)) { |
|
| 196 | + $this->session->remove('user_id'); |
|
| 197 | + } else { |
|
| 198 | + $this->session->set('user_id', $user->getUID()); |
|
| 199 | + } |
|
| 200 | + $this->activeUser = $user; |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + /** |
|
| 204 | + * get the current active user |
|
| 205 | + * |
|
| 206 | + * @return IUser|null Current user, otherwise null |
|
| 207 | + */ |
|
| 208 | + public function getUser() { |
|
| 209 | + // FIXME: This is a quick'n dirty work-around for the incognito mode as |
|
| 210 | + // described at https://github.com/owncloud/core/pull/12912#issuecomment-67391155 |
|
| 211 | + if (OC_User::isIncognitoMode()) { |
|
| 212 | + return null; |
|
| 213 | + } |
|
| 214 | + if (is_null($this->activeUser)) { |
|
| 215 | + $uid = $this->session->get('user_id'); |
|
| 216 | + if (is_null($uid)) { |
|
| 217 | + return null; |
|
| 218 | + } |
|
| 219 | + $this->activeUser = $this->manager->get($uid); |
|
| 220 | + if (is_null($this->activeUser)) { |
|
| 221 | + return null; |
|
| 222 | + } |
|
| 223 | + $this->validateSession(); |
|
| 224 | + } |
|
| 225 | + return $this->activeUser; |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + /** |
|
| 229 | + * Validate whether the current session is valid |
|
| 230 | + * |
|
| 231 | + * - For token-authenticated clients, the token validity is checked |
|
| 232 | + * - For browsers, the session token validity is checked |
|
| 233 | + */ |
|
| 234 | + protected function validateSession() { |
|
| 235 | + $token = null; |
|
| 236 | + $appPassword = $this->session->get('app_password'); |
|
| 237 | + |
|
| 238 | + if (is_null($appPassword)) { |
|
| 239 | + try { |
|
| 240 | + $token = $this->session->getId(); |
|
| 241 | + } catch (SessionNotAvailableException $ex) { |
|
| 242 | + return; |
|
| 243 | + } |
|
| 244 | + } else { |
|
| 245 | + $token = $appPassword; |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + if (!$this->validateToken($token)) { |
|
| 249 | + // Session was invalidated |
|
| 250 | + $this->logout(); |
|
| 251 | + } |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + /** |
|
| 255 | + * Checks whether the user is logged in |
|
| 256 | + * |
|
| 257 | + * @return bool if logged in |
|
| 258 | + */ |
|
| 259 | + public function isLoggedIn() { |
|
| 260 | + $user = $this->getUser(); |
|
| 261 | + if (is_null($user)) { |
|
| 262 | + return false; |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + return $user->isEnabled(); |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + /** |
|
| 269 | + * set the login name |
|
| 270 | + * |
|
| 271 | + * @param string|null $loginName for the logged in user |
|
| 272 | + */ |
|
| 273 | + public function setLoginName($loginName) { |
|
| 274 | + if (is_null($loginName)) { |
|
| 275 | + $this->session->remove('loginname'); |
|
| 276 | + } else { |
|
| 277 | + $this->session->set('loginname', $loginName); |
|
| 278 | + } |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + /** |
|
| 282 | + * get the login name of the current user |
|
| 283 | + * |
|
| 284 | + * @return string |
|
| 285 | + */ |
|
| 286 | + public function getLoginName() { |
|
| 287 | + if ($this->activeUser) { |
|
| 288 | + return $this->session->get('loginname'); |
|
| 289 | + } else { |
|
| 290 | + $uid = $this->session->get('user_id'); |
|
| 291 | + if ($uid) { |
|
| 292 | + $this->activeUser = $this->manager->get($uid); |
|
| 293 | + return $this->session->get('loginname'); |
|
| 294 | + } else { |
|
| 295 | + return null; |
|
| 296 | + } |
|
| 297 | + } |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + /** |
|
| 301 | + * set the token id |
|
| 302 | + * |
|
| 303 | + * @param int|null $token that was used to log in |
|
| 304 | + */ |
|
| 305 | + protected function setToken($token) { |
|
| 306 | + if ($token === null) { |
|
| 307 | + $this->session->remove('token-id'); |
|
| 308 | + } else { |
|
| 309 | + $this->session->set('token-id', $token); |
|
| 310 | + } |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + /** |
|
| 314 | + * try to log in with the provided credentials |
|
| 315 | + * |
|
| 316 | + * @param string $uid |
|
| 317 | + * @param string $password |
|
| 318 | + * @return boolean|null |
|
| 319 | + * @throws LoginException |
|
| 320 | + */ |
|
| 321 | + public function login($uid, $password) { |
|
| 322 | + $this->session->regenerateId(); |
|
| 323 | + if ($this->validateToken($password, $uid)) { |
|
| 324 | + return $this->loginWithToken($password); |
|
| 325 | + } |
|
| 326 | + return $this->loginWithPassword($uid, $password); |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + /** |
|
| 330 | + * @param IUser $user |
|
| 331 | + * @param array $loginDetails |
|
| 332 | + * @param bool $regenerateSessionId |
|
| 333 | + * @return true returns true if login successful or an exception otherwise |
|
| 334 | + * @throws LoginException |
|
| 335 | + */ |
|
| 336 | + public function completeLogin(IUser $user, array $loginDetails, $regenerateSessionId = true) { |
|
| 337 | + if (!$user->isEnabled()) { |
|
| 338 | + // disabled users can not log in |
|
| 339 | + // injecting l10n does not work - there is a circular dependency between session and \OCP\L10N\IFactory |
|
| 340 | + $message = \OC::$server->getL10N('lib')->t('User disabled'); |
|
| 341 | + throw new LoginException($message); |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + if($regenerateSessionId) { |
|
| 345 | + $this->session->regenerateId(); |
|
| 346 | + } |
|
| 347 | + |
|
| 348 | + $this->setUser($user); |
|
| 349 | + $this->setLoginName($loginDetails['loginName']); |
|
| 350 | + |
|
| 351 | + if(isset($loginDetails['token']) && $loginDetails['token'] instanceof IToken) { |
|
| 352 | + $this->setToken($loginDetails['token']->getId()); |
|
| 353 | + $this->lockdownManager->setToken($loginDetails['token']); |
|
| 354 | + $firstTimeLogin = false; |
|
| 355 | + } else { |
|
| 356 | + $this->setToken(null); |
|
| 357 | + $firstTimeLogin = $user->updateLastLoginTimestamp(); |
|
| 358 | + } |
|
| 359 | + $this->manager->emit('\OC\User', 'postLogin', [$user, $loginDetails['password']]); |
|
| 360 | + if($this->isLoggedIn()) { |
|
| 361 | + $this->prepareUserLogin($firstTimeLogin); |
|
| 362 | + return true; |
|
| 363 | + } else { |
|
| 364 | + $message = \OC::$server->getL10N('lib')->t('Login canceled by app'); |
|
| 365 | + throw new LoginException($message); |
|
| 366 | + } |
|
| 367 | + } |
|
| 368 | + |
|
| 369 | + /** |
|
| 370 | + * Tries to log in a client |
|
| 371 | + * |
|
| 372 | + * Checks token auth enforced |
|
| 373 | + * Checks 2FA enabled |
|
| 374 | + * |
|
| 375 | + * @param string $user |
|
| 376 | + * @param string $password |
|
| 377 | + * @param IRequest $request |
|
| 378 | + * @param OC\Security\Bruteforce\Throttler $throttler |
|
| 379 | + * @throws LoginException |
|
| 380 | + * @throws PasswordLoginForbiddenException |
|
| 381 | + * @return boolean |
|
| 382 | + */ |
|
| 383 | + public function logClientIn($user, |
|
| 384 | + $password, |
|
| 385 | + IRequest $request, |
|
| 386 | + OC\Security\Bruteforce\Throttler $throttler) { |
|
| 387 | + $currentDelay = $throttler->sleepDelay($request->getRemoteAddress(), 'login'); |
|
| 388 | + |
|
| 389 | + if ($this->manager instanceof PublicEmitter) { |
|
| 390 | + $this->manager->emit('\OC\User', 'preLogin', array($user, $password)); |
|
| 391 | + } |
|
| 392 | + |
|
| 393 | + $isTokenPassword = $this->isTokenPassword($password); |
|
| 394 | + if (!$isTokenPassword && $this->isTokenAuthEnforced()) { |
|
| 395 | + throw new PasswordLoginForbiddenException(); |
|
| 396 | + } |
|
| 397 | + if (!$isTokenPassword && $this->isTwoFactorEnforced($user)) { |
|
| 398 | + throw new PasswordLoginForbiddenException(); |
|
| 399 | + } |
|
| 400 | + if (!$this->login($user, $password) ) { |
|
| 401 | + $users = $this->manager->getByEmail($user); |
|
| 402 | + if (count($users) === 1) { |
|
| 403 | + return $this->login($users[0]->getUID(), $password); |
|
| 404 | + } |
|
| 405 | + |
|
| 406 | + $throttler->registerAttempt('login', $request->getRemoteAddress(), ['uid' => $user]); |
|
| 407 | + if($currentDelay === 0) { |
|
| 408 | + $throttler->sleepDelay($request->getRemoteAddress(), 'login'); |
|
| 409 | + } |
|
| 410 | + return false; |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + if ($isTokenPassword) { |
|
| 414 | + $this->session->set('app_password', $password); |
|
| 415 | + } else if($this->supportsCookies($request)) { |
|
| 416 | + // Password login, but cookies supported -> create (browser) session token |
|
| 417 | + $this->createSessionToken($request, $this->getUser()->getUID(), $user, $password); |
|
| 418 | + } |
|
| 419 | + |
|
| 420 | + return true; |
|
| 421 | + } |
|
| 422 | + |
|
| 423 | + protected function supportsCookies(IRequest $request) { |
|
| 424 | + if (!is_null($request->getCookie('cookie_test'))) { |
|
| 425 | + return true; |
|
| 426 | + } |
|
| 427 | + setcookie('cookie_test', 'test', $this->timeFactory->getTime() + 3600); |
|
| 428 | + return false; |
|
| 429 | + } |
|
| 430 | + |
|
| 431 | + private function isTokenAuthEnforced() { |
|
| 432 | + return $this->config->getSystemValue('token_auth_enforced', false); |
|
| 433 | + } |
|
| 434 | + |
|
| 435 | + protected function isTwoFactorEnforced($username) { |
|
| 436 | + Util::emitHook( |
|
| 437 | + '\OCA\Files_Sharing\API\Server2Server', |
|
| 438 | + 'preLoginNameUsedAsUserName', |
|
| 439 | + array('uid' => &$username) |
|
| 440 | + ); |
|
| 441 | + $user = $this->manager->get($username); |
|
| 442 | + if (is_null($user)) { |
|
| 443 | + $users = $this->manager->getByEmail($username); |
|
| 444 | + if (empty($users)) { |
|
| 445 | + return false; |
|
| 446 | + } |
|
| 447 | + if (count($users) !== 1) { |
|
| 448 | + return true; |
|
| 449 | + } |
|
| 450 | + $user = $users[0]; |
|
| 451 | + } |
|
| 452 | + // DI not possible due to cyclic dependencies :'-/ |
|
| 453 | + return OC::$server->getTwoFactorAuthManager()->isTwoFactorAuthenticated($user); |
|
| 454 | + } |
|
| 455 | + |
|
| 456 | + /** |
|
| 457 | + * Check if the given 'password' is actually a device token |
|
| 458 | + * |
|
| 459 | + * @param string $password |
|
| 460 | + * @return boolean |
|
| 461 | + */ |
|
| 462 | + public function isTokenPassword($password) { |
|
| 463 | + try { |
|
| 464 | + $this->tokenProvider->getToken($password); |
|
| 465 | + return true; |
|
| 466 | + } catch (InvalidTokenException $ex) { |
|
| 467 | + return false; |
|
| 468 | + } |
|
| 469 | + } |
|
| 470 | + |
|
| 471 | + protected function prepareUserLogin($firstTimeLogin) { |
|
| 472 | + // TODO: mock/inject/use non-static |
|
| 473 | + // Refresh the token |
|
| 474 | + \OC::$server->getCsrfTokenManager()->refreshToken(); |
|
| 475 | + //we need to pass the user name, which may differ from login name |
|
| 476 | + $user = $this->getUser()->getUID(); |
|
| 477 | + OC_Util::setupFS($user); |
|
| 478 | + |
|
| 479 | + if ($firstTimeLogin) { |
|
| 480 | + // TODO: lock necessary? |
|
| 481 | + //trigger creation of user home and /files folder |
|
| 482 | + $userFolder = \OC::$server->getUserFolder($user); |
|
| 483 | + |
|
| 484 | + try { |
|
| 485 | + // copy skeleton |
|
| 486 | + \OC_Util::copySkeleton($user, $userFolder); |
|
| 487 | + } catch (NotPermittedException $ex) { |
|
| 488 | + // read only uses |
|
| 489 | + } |
|
| 490 | + |
|
| 491 | + // trigger any other initialization |
|
| 492 | + \OC::$server->getEventDispatcher()->dispatch(IUser::class . '::firstLogin', new GenericEvent($this->getUser())); |
|
| 493 | + } |
|
| 494 | + } |
|
| 495 | + |
|
| 496 | + /** |
|
| 497 | + * Tries to login the user with HTTP Basic Authentication |
|
| 498 | + * |
|
| 499 | + * @todo do not allow basic auth if the user is 2FA enforced |
|
| 500 | + * @param IRequest $request |
|
| 501 | + * @param OC\Security\Bruteforce\Throttler $throttler |
|
| 502 | + * @return boolean if the login was successful |
|
| 503 | + */ |
|
| 504 | + public function tryBasicAuthLogin(IRequest $request, |
|
| 505 | + OC\Security\Bruteforce\Throttler $throttler) { |
|
| 506 | + if (!empty($request->server['PHP_AUTH_USER']) && !empty($request->server['PHP_AUTH_PW'])) { |
|
| 507 | + try { |
|
| 508 | + if ($this->logClientIn($request->server['PHP_AUTH_USER'], $request->server['PHP_AUTH_PW'], $request, $throttler)) { |
|
| 509 | + /** |
|
| 510 | + * Add DAV authenticated. This should in an ideal world not be |
|
| 511 | + * necessary but the iOS App reads cookies from anywhere instead |
|
| 512 | + * only the DAV endpoint. |
|
| 513 | + * This makes sure that the cookies will be valid for the whole scope |
|
| 514 | + * @see https://github.com/owncloud/core/issues/22893 |
|
| 515 | + */ |
|
| 516 | + $this->session->set( |
|
| 517 | + Auth::DAV_AUTHENTICATED, $this->getUser()->getUID() |
|
| 518 | + ); |
|
| 519 | + |
|
| 520 | + // Set the last-password-confirm session to make the sudo mode work |
|
| 521 | + $this->session->set('last-password-confirm', $this->timeFactory->getTime()); |
|
| 522 | + |
|
| 523 | + return true; |
|
| 524 | + } |
|
| 525 | + } catch (PasswordLoginForbiddenException $ex) { |
|
| 526 | + // Nothing to do |
|
| 527 | + } |
|
| 528 | + } |
|
| 529 | + return false; |
|
| 530 | + } |
|
| 531 | + |
|
| 532 | + /** |
|
| 533 | + * Log an user in via login name and password |
|
| 534 | + * |
|
| 535 | + * @param string $uid |
|
| 536 | + * @param string $password |
|
| 537 | + * @return boolean |
|
| 538 | + * @throws LoginException if an app canceld the login process or the user is not enabled |
|
| 539 | + */ |
|
| 540 | + private function loginWithPassword($uid, $password) { |
|
| 541 | + $user = $this->manager->checkPassword($uid, $password); |
|
| 542 | + if ($user === false) { |
|
| 543 | + // Password check failed |
|
| 544 | + return false; |
|
| 545 | + } |
|
| 546 | + |
|
| 547 | + return $this->completeLogin($user, ['loginName' => $uid, 'password' => $password], false); |
|
| 548 | + } |
|
| 549 | + |
|
| 550 | + /** |
|
| 551 | + * Log an user in with a given token (id) |
|
| 552 | + * |
|
| 553 | + * @param string $token |
|
| 554 | + * @return boolean |
|
| 555 | + * @throws LoginException if an app canceled the login process or the user is not enabled |
|
| 556 | + */ |
|
| 557 | + private function loginWithToken($token) { |
|
| 558 | + try { |
|
| 559 | + $dbToken = $this->tokenProvider->getToken($token); |
|
| 560 | + } catch (InvalidTokenException $ex) { |
|
| 561 | + return false; |
|
| 562 | + } |
|
| 563 | + $uid = $dbToken->getUID(); |
|
| 564 | + |
|
| 565 | + // When logging in with token, the password must be decrypted first before passing to login hook |
|
| 566 | + $password = ''; |
|
| 567 | + try { |
|
| 568 | + $password = $this->tokenProvider->getPassword($dbToken, $token); |
|
| 569 | + } catch (PasswordlessTokenException $ex) { |
|
| 570 | + // Ignore and use empty string instead |
|
| 571 | + } |
|
| 572 | + |
|
| 573 | + $this->manager->emit('\OC\User', 'preLogin', array($uid, $password)); |
|
| 574 | + |
|
| 575 | + $user = $this->manager->get($uid); |
|
| 576 | + if (is_null($user)) { |
|
| 577 | + // user does not exist |
|
| 578 | + return false; |
|
| 579 | + } |
|
| 580 | + |
|
| 581 | + return $this->completeLogin( |
|
| 582 | + $user, |
|
| 583 | + [ |
|
| 584 | + 'loginName' => $dbToken->getLoginName(), |
|
| 585 | + 'password' => $password, |
|
| 586 | + 'token' => $dbToken |
|
| 587 | + ], |
|
| 588 | + false); |
|
| 589 | + } |
|
| 590 | + |
|
| 591 | + /** |
|
| 592 | + * Create a new session token for the given user credentials |
|
| 593 | + * |
|
| 594 | + * @param IRequest $request |
|
| 595 | + * @param string $uid user UID |
|
| 596 | + * @param string $loginName login name |
|
| 597 | + * @param string $password |
|
| 598 | + * @param int $remember |
|
| 599 | + * @return boolean |
|
| 600 | + */ |
|
| 601 | + public function createSessionToken(IRequest $request, $uid, $loginName, $password = null, $remember = IToken::DO_NOT_REMEMBER) { |
|
| 602 | + if (is_null($this->manager->get($uid))) { |
|
| 603 | + // User does not exist |
|
| 604 | + return false; |
|
| 605 | + } |
|
| 606 | + $name = isset($request->server['HTTP_USER_AGENT']) ? $request->server['HTTP_USER_AGENT'] : 'unknown browser'; |
|
| 607 | + try { |
|
| 608 | + $sessionId = $this->session->getId(); |
|
| 609 | + $pwd = $this->getPassword($password); |
|
| 610 | + $this->tokenProvider->generateToken($sessionId, $uid, $loginName, $pwd, $name, IToken::TEMPORARY_TOKEN, $remember); |
|
| 611 | + return true; |
|
| 612 | + } catch (SessionNotAvailableException $ex) { |
|
| 613 | + // This can happen with OCC, where a memory session is used |
|
| 614 | + // if a memory session is used, we shouldn't create a session token anyway |
|
| 615 | + return false; |
|
| 616 | + } |
|
| 617 | + } |
|
| 618 | + |
|
| 619 | + /** |
|
| 620 | + * Checks if the given password is a token. |
|
| 621 | + * If yes, the password is extracted from the token. |
|
| 622 | + * If no, the same password is returned. |
|
| 623 | + * |
|
| 624 | + * @param string $password either the login password or a device token |
|
| 625 | + * @return string|null the password or null if none was set in the token |
|
| 626 | + */ |
|
| 627 | + private function getPassword($password) { |
|
| 628 | + if (is_null($password)) { |
|
| 629 | + // This is surely no token ;-) |
|
| 630 | + return null; |
|
| 631 | + } |
|
| 632 | + try { |
|
| 633 | + $token = $this->tokenProvider->getToken($password); |
|
| 634 | + try { |
|
| 635 | + return $this->tokenProvider->getPassword($token, $password); |
|
| 636 | + } catch (PasswordlessTokenException $ex) { |
|
| 637 | + return null; |
|
| 638 | + } |
|
| 639 | + } catch (InvalidTokenException $ex) { |
|
| 640 | + return $password; |
|
| 641 | + } |
|
| 642 | + } |
|
| 643 | + |
|
| 644 | + /** |
|
| 645 | + * @param IToken $dbToken |
|
| 646 | + * @param string $token |
|
| 647 | + * @return boolean |
|
| 648 | + */ |
|
| 649 | + private function checkTokenCredentials(IToken $dbToken, $token) { |
|
| 650 | + // Check whether login credentials are still valid and the user was not disabled |
|
| 651 | + // This check is performed each 5 minutes |
|
| 652 | + $lastCheck = $dbToken->getLastCheck() ? : 0; |
|
| 653 | + $now = $this->timeFactory->getTime(); |
|
| 654 | + if ($lastCheck > ($now - 60 * 5)) { |
|
| 655 | + // Checked performed recently, nothing to do now |
|
| 656 | + return true; |
|
| 657 | + } |
|
| 658 | + |
|
| 659 | + try { |
|
| 660 | + $pwd = $this->tokenProvider->getPassword($dbToken, $token); |
|
| 661 | + } catch (InvalidTokenException $ex) { |
|
| 662 | + // An invalid token password was used -> log user out |
|
| 663 | + return false; |
|
| 664 | + } catch (PasswordlessTokenException $ex) { |
|
| 665 | + // Token has no password |
|
| 666 | + |
|
| 667 | + if (!is_null($this->activeUser) && !$this->activeUser->isEnabled()) { |
|
| 668 | + $this->tokenProvider->invalidateToken($token); |
|
| 669 | + return false; |
|
| 670 | + } |
|
| 671 | + |
|
| 672 | + $dbToken->setLastCheck($now); |
|
| 673 | + return true; |
|
| 674 | + } |
|
| 675 | + |
|
| 676 | + if ($this->manager->checkPassword($dbToken->getLoginName(), $pwd) === false |
|
| 677 | + || (!is_null($this->activeUser) && !$this->activeUser->isEnabled())) { |
|
| 678 | + $this->tokenProvider->invalidateToken($token); |
|
| 679 | + // Password has changed or user was disabled -> log user out |
|
| 680 | + return false; |
|
| 681 | + } |
|
| 682 | + $dbToken->setLastCheck($now); |
|
| 683 | + return true; |
|
| 684 | + } |
|
| 685 | + |
|
| 686 | + /** |
|
| 687 | + * Check if the given token exists and performs password/user-enabled checks |
|
| 688 | + * |
|
| 689 | + * Invalidates the token if checks fail |
|
| 690 | + * |
|
| 691 | + * @param string $token |
|
| 692 | + * @param string $user login name |
|
| 693 | + * @return boolean |
|
| 694 | + */ |
|
| 695 | + private function validateToken($token, $user = null) { |
|
| 696 | + try { |
|
| 697 | + $dbToken = $this->tokenProvider->getToken($token); |
|
| 698 | + } catch (InvalidTokenException $ex) { |
|
| 699 | + return false; |
|
| 700 | + } |
|
| 701 | + |
|
| 702 | + // Check if login names match |
|
| 703 | + if (!is_null($user) && $dbToken->getLoginName() !== $user) { |
|
| 704 | + // TODO: this makes it imposssible to use different login names on browser and client |
|
| 705 | + // e.g. login by e-mail '[email protected]' on browser for generating the token will not |
|
| 706 | + // allow to use the client token with the login name 'user'. |
|
| 707 | + return false; |
|
| 708 | + } |
|
| 709 | + |
|
| 710 | + if (!$this->checkTokenCredentials($dbToken, $token)) { |
|
| 711 | + return false; |
|
| 712 | + } |
|
| 713 | + |
|
| 714 | + $this->tokenProvider->updateTokenActivity($dbToken); |
|
| 715 | + |
|
| 716 | + return true; |
|
| 717 | + } |
|
| 718 | + |
|
| 719 | + /** |
|
| 720 | + * Tries to login the user with auth token header |
|
| 721 | + * |
|
| 722 | + * @param IRequest $request |
|
| 723 | + * @todo check remember me cookie |
|
| 724 | + * @return boolean |
|
| 725 | + */ |
|
| 726 | + public function tryTokenLogin(IRequest $request) { |
|
| 727 | + $authHeader = $request->getHeader('Authorization'); |
|
| 728 | + if (strpos($authHeader, 'Bearer ') === false) { |
|
| 729 | + // No auth header, let's try session id |
|
| 730 | + try { |
|
| 731 | + $token = $this->session->getId(); |
|
| 732 | + } catch (SessionNotAvailableException $ex) { |
|
| 733 | + return false; |
|
| 734 | + } |
|
| 735 | + } else { |
|
| 736 | + $token = substr($authHeader, 7); |
|
| 737 | + } |
|
| 738 | + |
|
| 739 | + if (!$this->loginWithToken($token)) { |
|
| 740 | + return false; |
|
| 741 | + } |
|
| 742 | + if(!$this->validateToken($token)) { |
|
| 743 | + return false; |
|
| 744 | + } |
|
| 745 | + return true; |
|
| 746 | + } |
|
| 747 | + |
|
| 748 | + /** |
|
| 749 | + * perform login using the magic cookie (remember login) |
|
| 750 | + * |
|
| 751 | + * @param string $uid the username |
|
| 752 | + * @param string $currentToken |
|
| 753 | + * @param string $oldSessionId |
|
| 754 | + * @return bool |
|
| 755 | + */ |
|
| 756 | + public function loginWithCookie($uid, $currentToken, $oldSessionId) { |
|
| 757 | + $this->session->regenerateId(); |
|
| 758 | + $this->manager->emit('\OC\User', 'preRememberedLogin', array($uid)); |
|
| 759 | + $user = $this->manager->get($uid); |
|
| 760 | + if (is_null($user)) { |
|
| 761 | + // user does not exist |
|
| 762 | + return false; |
|
| 763 | + } |
|
| 764 | + |
|
| 765 | + // get stored tokens |
|
| 766 | + $tokens = $this->config->getUserKeys($uid, 'login_token'); |
|
| 767 | + // test cookies token against stored tokens |
|
| 768 | + if (!in_array($currentToken, $tokens, true)) { |
|
| 769 | + return false; |
|
| 770 | + } |
|
| 771 | + // replace successfully used token with a new one |
|
| 772 | + $this->config->deleteUserValue($uid, 'login_token', $currentToken); |
|
| 773 | + $newToken = $this->random->generate(32); |
|
| 774 | + $this->config->setUserValue($uid, 'login_token', $newToken, $this->timeFactory->getTime()); |
|
| 775 | + |
|
| 776 | + try { |
|
| 777 | + $sessionId = $this->session->getId(); |
|
| 778 | + $this->tokenProvider->renewSessionToken($oldSessionId, $sessionId); |
|
| 779 | + } catch (SessionNotAvailableException $ex) { |
|
| 780 | + return false; |
|
| 781 | + } catch (InvalidTokenException $ex) { |
|
| 782 | + \OC::$server->getLogger()->warning('Renewing session token failed', ['app' => 'core']); |
|
| 783 | + return false; |
|
| 784 | + } |
|
| 785 | + |
|
| 786 | + $this->setMagicInCookie($user->getUID(), $newToken); |
|
| 787 | + $token = $this->tokenProvider->getToken($sessionId); |
|
| 788 | + |
|
| 789 | + //login |
|
| 790 | + $this->setUser($user); |
|
| 791 | + $this->setLoginName($token->getLoginName()); |
|
| 792 | + $this->setToken($token->getId()); |
|
| 793 | + $this->lockdownManager->setToken($token); |
|
| 794 | + $user->updateLastLoginTimestamp(); |
|
| 795 | + $password = null; |
|
| 796 | + try { |
|
| 797 | + $password = $this->tokenProvider->getPassword($token, $sessionId); |
|
| 798 | + } catch (PasswordlessTokenException $ex) { |
|
| 799 | + // Ignore |
|
| 800 | + } |
|
| 801 | + $this->manager->emit('\OC\User', 'postRememberedLogin', [$user, $password]); |
|
| 802 | + return true; |
|
| 803 | + } |
|
| 804 | + |
|
| 805 | + /** |
|
| 806 | + * @param IUser $user |
|
| 807 | + */ |
|
| 808 | + public function createRememberMeToken(IUser $user) { |
|
| 809 | + $token = $this->random->generate(32); |
|
| 810 | + $this->config->setUserValue($user->getUID(), 'login_token', $token, $this->timeFactory->getTime()); |
|
| 811 | + $this->setMagicInCookie($user->getUID(), $token); |
|
| 812 | + } |
|
| 813 | + |
|
| 814 | + /** |
|
| 815 | + * logout the user from the session |
|
| 816 | + */ |
|
| 817 | + public function logout() { |
|
| 818 | + $this->manager->emit('\OC\User', 'logout'); |
|
| 819 | + $user = $this->getUser(); |
|
| 820 | + if (!is_null($user)) { |
|
| 821 | + try { |
|
| 822 | + $this->tokenProvider->invalidateToken($this->session->getId()); |
|
| 823 | + } catch (SessionNotAvailableException $ex) { |
|
| 824 | + |
|
| 825 | + } |
|
| 826 | + } |
|
| 827 | + $this->setUser(null); |
|
| 828 | + $this->setLoginName(null); |
|
| 829 | + $this->setToken(null); |
|
| 830 | + $this->unsetMagicInCookie(); |
|
| 831 | + $this->session->clear(); |
|
| 832 | + $this->manager->emit('\OC\User', 'postLogout'); |
|
| 833 | + } |
|
| 834 | + |
|
| 835 | + /** |
|
| 836 | + * Set cookie value to use in next page load |
|
| 837 | + * |
|
| 838 | + * @param string $username username to be set |
|
| 839 | + * @param string $token |
|
| 840 | + */ |
|
| 841 | + public function setMagicInCookie($username, $token) { |
|
| 842 | + $secureCookie = OC::$server->getRequest()->getServerProtocol() === 'https'; |
|
| 843 | + $webRoot = \OC::$WEBROOT; |
|
| 844 | + if ($webRoot === '') { |
|
| 845 | + $webRoot = '/'; |
|
| 846 | + } |
|
| 847 | + |
|
| 848 | + $expires = $this->timeFactory->getTime() + $this->config->getSystemValue('remember_login_cookie_lifetime', 60 * 60 * 24 * 15); |
|
| 849 | + setcookie('nc_username', $username, $expires, $webRoot, '', $secureCookie, true); |
|
| 850 | + setcookie('nc_token', $token, $expires, $webRoot, '', $secureCookie, true); |
|
| 851 | + try { |
|
| 852 | + setcookie('nc_session_id', $this->session->getId(), $expires, $webRoot, '', $secureCookie, true); |
|
| 853 | + } catch (SessionNotAvailableException $ex) { |
|
| 854 | + // ignore |
|
| 855 | + } |
|
| 856 | + } |
|
| 857 | + |
|
| 858 | + /** |
|
| 859 | + * Remove cookie for "remember username" |
|
| 860 | + */ |
|
| 861 | + public function unsetMagicInCookie() { |
|
| 862 | + //TODO: DI for cookies and IRequest |
|
| 863 | + $secureCookie = OC::$server->getRequest()->getServerProtocol() === 'https'; |
|
| 864 | + |
|
| 865 | + unset($_COOKIE['nc_username']); //TODO: DI |
|
| 866 | + unset($_COOKIE['nc_token']); |
|
| 867 | + unset($_COOKIE['nc_session_id']); |
|
| 868 | + setcookie('nc_username', '', $this->timeFactory->getTime() - 3600, OC::$WEBROOT, '', $secureCookie, true); |
|
| 869 | + setcookie('nc_token', '', $this->timeFactory->getTime() - 3600, OC::$WEBROOT, '', $secureCookie, true); |
|
| 870 | + setcookie('nc_session_id', '', $this->timeFactory->getTime() - 3600, OC::$WEBROOT, '', $secureCookie, true); |
|
| 871 | + // old cookies might be stored under /webroot/ instead of /webroot |
|
| 872 | + // and Firefox doesn't like it! |
|
| 873 | + setcookie('nc_username', '', $this->timeFactory->getTime() - 3600, OC::$WEBROOT . '/', '', $secureCookie, true); |
|
| 874 | + setcookie('nc_token', '', $this->timeFactory->getTime() - 3600, OC::$WEBROOT . '/', '', $secureCookie, true); |
|
| 875 | + setcookie('nc_session_id', '', $this->timeFactory->getTime() - 3600, OC::$WEBROOT . '/', '', $secureCookie, true); |
|
| 876 | + } |
|
| 877 | + |
|
| 878 | + /** |
|
| 879 | + * Update password of the browser session token if there is one |
|
| 880 | + * |
|
| 881 | + * @param string $password |
|
| 882 | + */ |
|
| 883 | + public function updateSessionTokenPassword($password) { |
|
| 884 | + try { |
|
| 885 | + $sessionId = $this->session->getId(); |
|
| 886 | + $token = $this->tokenProvider->getToken($sessionId); |
|
| 887 | + $this->tokenProvider->setPassword($token, $sessionId, $password); |
|
| 888 | + } catch (SessionNotAvailableException $ex) { |
|
| 889 | + // Nothing to do |
|
| 890 | + } catch (InvalidTokenException $ex) { |
|
| 891 | + // Nothing to do |
|
| 892 | + } |
|
| 893 | + } |
|
| 894 | 894 | |
| 895 | 895 | |
| 896 | 896 | } |
@@ -32,69 +32,69 @@ |
||
| 32 | 32 | use OCP\Security\ISecureRandom; |
| 33 | 33 | |
| 34 | 34 | class SettingsController extends Controller { |
| 35 | - /** @var IURLGenerator */ |
|
| 36 | - private $urlGenerator; |
|
| 37 | - /** @var ClientMapper */ |
|
| 38 | - private $clientMapper; |
|
| 39 | - /** @var ISecureRandom */ |
|
| 40 | - private $secureRandom; |
|
| 41 | - /** @var AccessTokenMapper */ |
|
| 42 | - private $accessTokenMapper; |
|
| 43 | - /** @var DefaultTokenMapper */ |
|
| 44 | - private $defaultTokenMapper; |
|
| 35 | + /** @var IURLGenerator */ |
|
| 36 | + private $urlGenerator; |
|
| 37 | + /** @var ClientMapper */ |
|
| 38 | + private $clientMapper; |
|
| 39 | + /** @var ISecureRandom */ |
|
| 40 | + private $secureRandom; |
|
| 41 | + /** @var AccessTokenMapper */ |
|
| 42 | + private $accessTokenMapper; |
|
| 43 | + /** @var DefaultTokenMapper */ |
|
| 44 | + private $defaultTokenMapper; |
|
| 45 | 45 | |
| 46 | - const validChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; |
|
| 46 | + const validChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * @param string $appName |
|
| 50 | - * @param IRequest $request |
|
| 51 | - * @param IURLGenerator $urlGenerator |
|
| 52 | - * @param ClientMapper $clientMapper |
|
| 53 | - * @param ISecureRandom $secureRandom |
|
| 54 | - * @param AccessTokenMapper $accessTokenMapper |
|
| 55 | - * @param DefaultTokenMapper $defaultTokenMapper |
|
| 56 | - */ |
|
| 57 | - public function __construct($appName, |
|
| 58 | - IRequest $request, |
|
| 59 | - IURLGenerator $urlGenerator, |
|
| 60 | - ClientMapper $clientMapper, |
|
| 61 | - ISecureRandom $secureRandom, |
|
| 62 | - AccessTokenMapper $accessTokenMapper, |
|
| 63 | - DefaultTokenMapper $defaultTokenMapper |
|
| 64 | - ) { |
|
| 65 | - parent::__construct($appName, $request); |
|
| 66 | - $this->urlGenerator = $urlGenerator; |
|
| 67 | - $this->secureRandom = $secureRandom; |
|
| 68 | - $this->clientMapper = $clientMapper; |
|
| 69 | - $this->accessTokenMapper = $accessTokenMapper; |
|
| 70 | - $this->defaultTokenMapper = $defaultTokenMapper; |
|
| 71 | - } |
|
| 48 | + /** |
|
| 49 | + * @param string $appName |
|
| 50 | + * @param IRequest $request |
|
| 51 | + * @param IURLGenerator $urlGenerator |
|
| 52 | + * @param ClientMapper $clientMapper |
|
| 53 | + * @param ISecureRandom $secureRandom |
|
| 54 | + * @param AccessTokenMapper $accessTokenMapper |
|
| 55 | + * @param DefaultTokenMapper $defaultTokenMapper |
|
| 56 | + */ |
|
| 57 | + public function __construct($appName, |
|
| 58 | + IRequest $request, |
|
| 59 | + IURLGenerator $urlGenerator, |
|
| 60 | + ClientMapper $clientMapper, |
|
| 61 | + ISecureRandom $secureRandom, |
|
| 62 | + AccessTokenMapper $accessTokenMapper, |
|
| 63 | + DefaultTokenMapper $defaultTokenMapper |
|
| 64 | + ) { |
|
| 65 | + parent::__construct($appName, $request); |
|
| 66 | + $this->urlGenerator = $urlGenerator; |
|
| 67 | + $this->secureRandom = $secureRandom; |
|
| 68 | + $this->clientMapper = $clientMapper; |
|
| 69 | + $this->accessTokenMapper = $accessTokenMapper; |
|
| 70 | + $this->defaultTokenMapper = $defaultTokenMapper; |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - /** |
|
| 74 | - * @param string $name |
|
| 75 | - * @param string $redirectUri |
|
| 76 | - * @return RedirectResponse |
|
| 77 | - */ |
|
| 78 | - public function addClient($name, |
|
| 79 | - $redirectUri) { |
|
| 80 | - $client = new Client(); |
|
| 81 | - $client->setName($name); |
|
| 82 | - $client->setRedirectUri($redirectUri); |
|
| 83 | - $client->setSecret($this->secureRandom->generate(64, self::validChars)); |
|
| 84 | - $client->setClientIdentifier($this->secureRandom->generate(64, self::validChars)); |
|
| 85 | - $this->clientMapper->insert($client); |
|
| 86 | - return new RedirectResponse($this->urlGenerator->getAbsoluteURL('/index.php/settings/admin/security')); |
|
| 87 | - } |
|
| 73 | + /** |
|
| 74 | + * @param string $name |
|
| 75 | + * @param string $redirectUri |
|
| 76 | + * @return RedirectResponse |
|
| 77 | + */ |
|
| 78 | + public function addClient($name, |
|
| 79 | + $redirectUri) { |
|
| 80 | + $client = new Client(); |
|
| 81 | + $client->setName($name); |
|
| 82 | + $client->setRedirectUri($redirectUri); |
|
| 83 | + $client->setSecret($this->secureRandom->generate(64, self::validChars)); |
|
| 84 | + $client->setClientIdentifier($this->secureRandom->generate(64, self::validChars)); |
|
| 85 | + $this->clientMapper->insert($client); |
|
| 86 | + return new RedirectResponse($this->urlGenerator->getAbsoluteURL('/index.php/settings/admin/security')); |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * @param int $id |
|
| 91 | - * @return RedirectResponse |
|
| 92 | - */ |
|
| 93 | - public function deleteClient($id) { |
|
| 94 | - $client = $this->clientMapper->getByUid($id); |
|
| 95 | - $this->accessTokenMapper->deleteByClientId($id); |
|
| 96 | - $this->defaultTokenMapper->deleteByName($client->getName()); |
|
| 97 | - $this->clientMapper->delete($client); |
|
| 98 | - return new RedirectResponse($this->urlGenerator->getAbsoluteURL('/index.php/settings/admin/security')); |
|
| 99 | - } |
|
| 89 | + /** |
|
| 90 | + * @param int $id |
|
| 91 | + * @return RedirectResponse |
|
| 92 | + */ |
|
| 93 | + public function deleteClient($id) { |
|
| 94 | + $client = $this->clientMapper->getByUid($id); |
|
| 95 | + $this->accessTokenMapper->deleteByClientId($id); |
|
| 96 | + $this->defaultTokenMapper->deleteByName($client->getName()); |
|
| 97 | + $this->clientMapper->delete($client); |
|
| 98 | + return new RedirectResponse($this->urlGenerator->getAbsoluteURL('/index.php/settings/admin/security')); |
|
| 99 | + } |
|
| 100 | 100 | } |
@@ -46,9 +46,9 @@ |
||
| 46 | 46 | </thead> |
| 47 | 47 | <tbody> |
| 48 | 48 | <?php |
| 49 | - $imageUrl = $urlGenerator->imagePath('core', 'actions/toggle.svg'); |
|
| 50 | - foreach ($clients as $client) { |
|
| 51 | - ?> |
|
| 49 | + $imageUrl = $urlGenerator->imagePath('core', 'actions/toggle.svg'); |
|
| 50 | + foreach ($clients as $client) { |
|
| 51 | + ?> |
|
| 52 | 52 | <tr> |
| 53 | 53 | <td><?php p($client->getName()); ?></td> |
| 54 | 54 | <td><?php p($client->getRedirectUri()); ?></td> |
@@ -29,51 +29,51 @@ |
||
| 29 | 29 | use OCP\IURLGenerator; |
| 30 | 30 | |
| 31 | 31 | class LoginRedirectorController extends Controller { |
| 32 | - /** @var IURLGenerator */ |
|
| 33 | - private $urlGenerator; |
|
| 34 | - /** @var ClientMapper */ |
|
| 35 | - private $clientMapper; |
|
| 36 | - /** @var ISession */ |
|
| 37 | - private $session; |
|
| 32 | + /** @var IURLGenerator */ |
|
| 33 | + private $urlGenerator; |
|
| 34 | + /** @var ClientMapper */ |
|
| 35 | + private $clientMapper; |
|
| 36 | + /** @var ISession */ |
|
| 37 | + private $session; |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * @param string $appName |
|
| 41 | - * @param IRequest $request |
|
| 42 | - * @param IURLGenerator $urlGenerator |
|
| 43 | - * @param ClientMapper $clientMapper |
|
| 44 | - * @param ISession $session |
|
| 45 | - */ |
|
| 46 | - public function __construct($appName, |
|
| 47 | - IRequest $request, |
|
| 48 | - IURLGenerator $urlGenerator, |
|
| 49 | - ClientMapper $clientMapper, |
|
| 50 | - ISession $session) { |
|
| 51 | - parent::__construct($appName, $request); |
|
| 52 | - $this->urlGenerator = $urlGenerator; |
|
| 53 | - $this->clientMapper = $clientMapper; |
|
| 54 | - $this->session = $session; |
|
| 55 | - } |
|
| 39 | + /** |
|
| 40 | + * @param string $appName |
|
| 41 | + * @param IRequest $request |
|
| 42 | + * @param IURLGenerator $urlGenerator |
|
| 43 | + * @param ClientMapper $clientMapper |
|
| 44 | + * @param ISession $session |
|
| 45 | + */ |
|
| 46 | + public function __construct($appName, |
|
| 47 | + IRequest $request, |
|
| 48 | + IURLGenerator $urlGenerator, |
|
| 49 | + ClientMapper $clientMapper, |
|
| 50 | + ISession $session) { |
|
| 51 | + parent::__construct($appName, $request); |
|
| 52 | + $this->urlGenerator = $urlGenerator; |
|
| 53 | + $this->clientMapper = $clientMapper; |
|
| 54 | + $this->session = $session; |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * @PublicPage |
|
| 59 | - * @NoCSRFRequired |
|
| 60 | - * @UseSession |
|
| 61 | - * |
|
| 62 | - * @param string $client_id |
|
| 63 | - * @param string $state |
|
| 64 | - * @return RedirectResponse |
|
| 65 | - */ |
|
| 66 | - public function authorize($client_id, |
|
| 67 | - $state) { |
|
| 68 | - $client = $this->clientMapper->getByIdentifier($client_id); |
|
| 69 | - $this->session->set('oauth.state', $state); |
|
| 57 | + /** |
|
| 58 | + * @PublicPage |
|
| 59 | + * @NoCSRFRequired |
|
| 60 | + * @UseSession |
|
| 61 | + * |
|
| 62 | + * @param string $client_id |
|
| 63 | + * @param string $state |
|
| 64 | + * @return RedirectResponse |
|
| 65 | + */ |
|
| 66 | + public function authorize($client_id, |
|
| 67 | + $state) { |
|
| 68 | + $client = $this->clientMapper->getByIdentifier($client_id); |
|
| 69 | + $this->session->set('oauth.state', $state); |
|
| 70 | 70 | |
| 71 | - $targetUrl = $this->urlGenerator->linkToRouteAbsolute( |
|
| 72 | - 'core.ClientFlowLogin.showAuthPickerPage', |
|
| 73 | - [ |
|
| 74 | - 'clientIdentifier' => $client->getClientIdentifier(), |
|
| 75 | - ] |
|
| 76 | - ); |
|
| 77 | - return new RedirectResponse($targetUrl); |
|
| 78 | - } |
|
| 71 | + $targetUrl = $this->urlGenerator->linkToRouteAbsolute( |
|
| 72 | + 'core.ClientFlowLogin.showAuthPickerPage', |
|
| 73 | + [ |
|
| 74 | + 'clientIdentifier' => $client->getClientIdentifier(), |
|
| 75 | + ] |
|
| 76 | + ); |
|
| 77 | + return new RedirectResponse($targetUrl); |
|
| 78 | + } |
|
| 79 | 79 | } |
@@ -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 | } |