Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
loginController now emitting preLogin hook signal before failed login…
… attempts
  • Loading branch information
karakayasemi committed Aug 9, 2017
commit 5e74265c92a58d6c1ba6f52b900a23ae56413166
17 changes: 6 additions & 11 deletions core/Controller/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,17 +197,11 @@ public function showLoginForm($user, $redirect_url, $remember_login) {
public function tryLogin($user, $password, $redirect_url) {
$originalUser = $user;
// TODO: Add all the insane error handling
/* @var $loginResult IUser */
$loginResult = $this->userManager->checkPassword($user, $password);
if ($loginResult === false) {
$users = $this->userManager->getByEmail($user);
// we only allow login by email if unique
if (count($users) === 1) {
$user = $users[0]->getUID();
$loginResult = $this->userManager->checkPassword($user, $password);
}
$emailUsers = $this->userManager->getByEmail($user);
if (count($emailUsers) === 1) {
$user = $emailUsers[0]->getUID();
}
if ($loginResult === false) {
if ($this->userSession->login($user, $password) !== true) {
$this->session->set('loginMessages', [
['invalidpassword'], []
]);
Expand All @@ -222,9 +216,10 @@ public function tryLogin($user, $password, $redirect_url) {
}
return new RedirectResponse($this->urlGenerator->linkToRoute('core.login.showLoginForm', $args));
}
/* @var $loginResult IUser */
$loginResult = $this->userManager->get($user);
// TODO: remove password checks from above and let the user session handle failures
// requires https://github.com/owncloud/core/pull/24616
$this->userSession->login($user, $password);
$this->userSession->createSessionToken($this->request, $loginResult->getUID(), $user, $password);

// User has successfully logged in, now remove the password reset link, when it is available
Expand Down
36 changes: 21 additions & 15 deletions tests/Core/Controller/LoginControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,8 @@ public function testLoginWithInvalidCredentials() {
$password = 'secret';
$loginPageUrl = 'some url';

$this->userManager->expects($this->once())
->method('checkPassword')
$this->userSession->expects($this->once())
->method('login')
->will($this->returnValue(false));
$this->urlGenerator->expects($this->once())
->method('linkToRoute')
Expand All @@ -328,12 +328,14 @@ public function testLoginWithValidCredentials() {
$password = 'secret';
$indexPageUrl = 'some url';

$this->userManager->expects($this->once())
->method('checkPassword')
->will($this->returnValue($user));
$this->userSession->expects($this->once())
->method('login')
->with($user, $password);
->with($user, $password)
->will($this->returnValue(true));
$this->userManager->expects($this->once())
->method('get')
->with($user)
->will($this->returnValue($user));
$this->userSession->expects($this->once())
->method('createSessionToken')
->with($this->request, $user->getUID(), $user, $password);
Expand Down Expand Up @@ -374,9 +376,13 @@ public function testLoginWithValidCredentialsAndRedirectUrl() {
$originalUrl = 'another%20url';
$redirectUrl = 'http://localhost/another url';

$this->userManager->expects($this->once())
->method('checkPassword')
$this->userSession->expects($this->once())
->method('login')
->with('Jane', $password)
->will($this->returnValue(true));
$this->userManager->expects($this->once())
->method('get')
->with('Jane')
->will($this->returnValue($user));
$this->userSession->expects($this->once())
->method('createSessionToken')
Expand All @@ -403,8 +409,11 @@ public function testLoginWithTwoFactorEnforced() {
$password = 'secret';
$challengeUrl = 'challenge/url';

$this->userSession->expects($this->once())
->method('login')
->will($this->returnValue(true));
$this->userManager->expects($this->once())
->method('checkPassword')
->method('get')
->will($this->returnValue($user));
$this->userSession->expects($this->once())
->method('login')
Expand Down Expand Up @@ -435,12 +444,9 @@ public function testToNotLeakLoginName() {
->method('getUID')
->will($this->returnValue('john'));

$this->userManager->expects($this->exactly(2))
->method('checkPassword')
->withConsecutive(
['[email protected]', 'just wrong'],
['john', 'just wrong']
)
$this->userSession->expects($this->once())
->method('login')
->with('john', 'just wrong')
->willReturn(false);

$this->userManager->expects($this->once())
Expand Down