Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing logout for app password scenario #30597

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 8 additions & 1 deletion lib/private/User/BasicAuthModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,20 @@

use OCP\Authentication\IAuthModule;
use OCP\IRequest;
use OCP\ISession;
use OCP\IUser;
use OCP\IUserManager;

class BasicAuthModule implements IAuthModule {

/** @var IUserManager */
private $manager;
/** @var ISession */
private $session;

public function __construct(IUserManager $manager) {
public function __construct(IUserManager $manager, ISession $session) {
$this->manager = $manager;
$this->session = $session;
}

/**
Expand All @@ -44,6 +48,9 @@ public function auth(IRequest $request) {
if (!isset($request->server['PHP_AUTH_USER'], $request->server['PHP_AUTH_PW'])) {
return null;
}
if ($this->session->exists('app_password')) {
return null;
}
$authUser = $request->server['PHP_AUTH_USER'];
$authPass = $request->server['PHP_AUTH_PW'];
if ($authUser === '' || $authPass === '') {
Expand Down
2 changes: 1 addition & 1 deletion lib/private/User/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,7 @@ protected function getAuthModules($includeBuiltIn) {
}

if ($includeBuiltIn) {
yield new BasicAuthModule($this->manager);
yield new BasicAuthModule($this->manager, $this->session);
}
}
}
35 changes: 33 additions & 2 deletions tests/lib/User/BasicAuthModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use OC\User\BasicAuthModule;
use OCP\IRequest;
use OCP\ISession;
use OCP\IUser;
use OCP\IUserManager;
use Test\TestCase;
Expand All @@ -37,11 +38,14 @@ class BasicAuthModuleTest extends TestCase {
private $request;
/** @var IUser | \PHPUnit_Framework_MockObject_MockObject */
private $user;
/** @var ISession | \PHPUnit_Framework_MockObject_MockObject */
private $session;

public function setUp() {
parent::setUp();
$this->manager = $this->createMock(IUserManager::class);
$this->request = $this->createMock(IRequest::class);
$this->session = $this->createMock(ISession::class);

$this->user = $this->createMock(IUser::class);
$this->user->expects($this->any())->method('getUID')->willReturn('user1');
Expand All @@ -61,6 +65,7 @@ public function setUp() {
['[email protected]', [$this->user]],
['user2', []]
]);

}

/**
Expand All @@ -69,7 +74,13 @@ public function setUp() {
* @param string $userId
*/
public function testAuth($expectedResult, $userId) {
$module = new BasicAuthModule($this->manager);

$this->session
->method('exists')
->with('app_password')
->willReturn(false);

$module = new BasicAuthModule($this->manager, $this->session);
$this->request->server = [
'PHP_AUTH_USER' => $userId,
'PHP_AUTH_PW' => '123456',
Expand All @@ -81,8 +92,28 @@ public function testAuth($expectedResult, $userId) {
$this->assertEquals($expectedResult ? $this->user : null, $module->auth($this->request));
}

public function testAppPassword() {

$this->session
->expects($this->once())
->method('exists')
->with('app_password')
->willReturn(true);

$this->manager
->expects($this->never())
->method('checkPassword');

$module = new BasicAuthModule($this->manager, $this->session);
$this->request->server = [
'PHP_AUTH_USER' => 'user',
'PHP_AUTH_PW' => 'app-pass-word',
];
$this->assertEquals(null, $module->auth($this->request));
}

public function testGetUserPassword() {
$module = new BasicAuthModule($this->manager);
$module = new BasicAuthModule($this->manager, $this->session);
$this->request->server = [
'PHP_AUTH_USER' => 'user1',
'PHP_AUTH_PW' => '123456',
Expand Down