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

fix(Session): avoid password confirmation on SSO #43942

Merged
merged 2 commits into from
Jun 7, 2024
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
4 changes: 2 additions & 2 deletions apps/settings/lib/Controller/AuthSettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ public function update($id, array $scope, string $name) {
$currentName = $token->getName();

if ($scope !== $token->getScopeAsArray()) {
$token->setScope(['filesystem' => $scope['filesystem']]);
$this->publishActivity($scope['filesystem'] ? Provider::APP_TOKEN_FILESYSTEM_GRANTED : Provider::APP_TOKEN_FILESYSTEM_REVOKED, $token->getId(), ['name' => $currentName]);
$token->setScope([IToken::SCOPE_FILESYSTEM => $scope[IToken::SCOPE_FILESYSTEM]]);
$this->publishActivity($scope[IToken::SCOPE_FILESYSTEM] ? Provider::APP_TOKEN_FILESYSTEM_GRANTED : Provider::APP_TOKEN_FILESYSTEM_REVOKED, $token->getId(), ['name' => $currentName]);
}

if (mb_strlen($name) > 128) {
Expand Down
20 changes: 10 additions & 10 deletions apps/settings/tests/Controller/AuthSettingsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ public function testUpdateRename(string $name, string $newName): void {

$token->expects($this->once())
->method('getScopeAsArray')
->willReturn(['filesystem' => true]);
->willReturn([IToken::SCOPE_FILESYSTEM => true]);

$token->expects($this->once())
->method('setName')
Expand All @@ -277,7 +277,7 @@ public function testUpdateRename(string $name, string $newName): void {
->method('updateToken')
->with($this->equalTo($token));

$this->assertSame([], $this->controller->update($tokenId, ['filesystem' => true], $newName));
$this->assertSame([], $this->controller->update($tokenId, [IToken::SCOPE_FILESYSTEM => true], $newName));
}

public function dataUpdateFilesystemScope(): array {
Expand Down Expand Up @@ -310,17 +310,17 @@ public function testUpdateFilesystemScope(bool $filesystem, bool $newFilesystem)

$token->expects($this->once())
->method('getScopeAsArray')
->willReturn(['filesystem' => $filesystem]);
->willReturn([IToken::SCOPE_FILESYSTEM => $filesystem]);

$token->expects($this->once())
->method('setScope')
->with($this->equalTo(['filesystem' => $newFilesystem]));
->with($this->equalTo([IToken::SCOPE_FILESYSTEM => $newFilesystem]));

$this->tokenProvider->expects($this->once())
->method('updateToken')
->with($this->equalTo($token));

$this->assertSame([], $this->controller->update($tokenId, ['filesystem' => $newFilesystem], 'App password'));
$this->assertSame([], $this->controller->update($tokenId, [IToken::SCOPE_FILESYSTEM => $newFilesystem], 'App password'));
}

public function testUpdateNoChange(): void {
Expand All @@ -339,7 +339,7 @@ public function testUpdateNoChange(): void {

$token->expects($this->once())
->method('getScopeAsArray')
->willReturn(['filesystem' => true]);
->willReturn([IToken::SCOPE_FILESYSTEM => true]);

$token->expects($this->never())
->method('setName');
Expand All @@ -351,7 +351,7 @@ public function testUpdateNoChange(): void {
->method('updateToken')
->with($this->equalTo($token));

$this->assertSame([], $this->controller->update($tokenId, ['filesystem' => true], 'App password'));
$this->assertSame([], $this->controller->update($tokenId, [IToken::SCOPE_FILESYSTEM => true], 'App password'));
}

public function testUpdateExpired() {
Expand All @@ -371,7 +371,7 @@ public function testUpdateExpired() {
->method('updateToken')
->with($this->equalTo($token));

$this->assertSame([], $this->controller->update($tokenId, ['filesystem' => true], 'App password'));
$this->assertSame([], $this->controller->update($tokenId, [IToken::SCOPE_FILESYSTEM => true], 'App password'));
}

public function testUpdateTokenWrongUser() {
Expand All @@ -389,7 +389,7 @@ public function testUpdateTokenWrongUser() {
$this->tokenProvider->expects($this->never())
->method('updateToken');

$response = $this->controller->update($tokenId, ['filesystem' => true], 'App password');
$response = $this->controller->update($tokenId, [IToken::SCOPE_FILESYSTEM => true], 'App password');
$this->assertSame([], $response->getData());
$this->assertSame(\OCP\AppFramework\Http::STATUS_NOT_FOUND, $response->getStatus());
}
Expand All @@ -403,7 +403,7 @@ public function testUpdateTokenNonExisting() {
$this->tokenProvider->expects($this->never())
->method('updateToken');

$response = $this->controller->update(42, ['filesystem' => true], 'App password');
$response = $this->controller->update(42, [IToken::SCOPE_FILESYSTEM => true], 'App password');
$this->assertSame([], $response->getData());
$this->assertSame(\OCP\AppFramework\Http::STATUS_NOT_FOUND, $response->getStatus());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use OCA\Settings\Settings\Personal\Security\Authtokens;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;
use OCP\Authentication\Token\IToken;
use OCP\ISession;
use OCP\IUserSession;
use PHPUnit\Framework\MockObject\MockObject;
Expand Down Expand Up @@ -108,7 +109,7 @@ public function testGetForm() {
'type' => 0,
'canDelete' => false,
'current' => true,
'scope' => ['filesystem' => true],
'scope' => [IToken::SCOPE_FILESYSTEM => true],
'canRename' => false,
],
[
Expand All @@ -117,7 +118,7 @@ public function testGetForm() {
'lastActivity' => 0,
'type' => 0,
'canDelete' => true,
'scope' => ['filesystem' => true],
'scope' => [IToken::SCOPE_FILESYSTEM => true],
'canRename' => true,
],
]
Expand Down
5 changes: 4 additions & 1 deletion core/Controller/OCJSController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace OC\Core\Controller;

use bantu\IniGetWrapper\IniGetWrapper;
use OC\Authentication\Token\IProvider;
use OC\CapabilitiesManager;
use OC\Template\JSConfigHelper;
use OCP\App\IAppManager;
Expand Down Expand Up @@ -42,6 +43,7 @@ public function __construct(
IURLGenerator $urlGenerator,
CapabilitiesManager $capabilitiesManager,
IInitialStateService $initialStateService,
IProvider $tokenProvider,
) {
parent::__construct($appName, $request);

Expand All @@ -56,7 +58,8 @@ public function __construct(
$iniWrapper,
$urlGenerator,
$capabilitiesManager,
$initialStateService
$initialStateService,
$tokenProvider
);
}

Expand Down
3 changes: 2 additions & 1 deletion lib/private/AppFramework/DependencyInjection/DIContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,8 @@ public function __construct(string $appName, array $urlParams = [], ?ServerConta
$c->get(IControllerMethodReflector::class),
$c->get(ISession::class),
$c->get(IUserSession::class),
$c->get(ITimeFactory::class)
$c->get(ITimeFactory::class),
$c->get(\OC\Authentication\Token\IProvider::class),
)
);
$dispatcher->registerMiddleware(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@

use OC\AppFramework\Middleware\Security\Exceptions\NotConfirmedException;
use OC\AppFramework\Utility\ControllerMethodReflector;
use OC\Authentication\Token\IProvider;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\Attribute\PasswordConfirmationRequired;
use OCP\AppFramework\Middleware;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Authentication\Exceptions\ExpiredTokenException;
use OCP\Authentication\Exceptions\InvalidTokenException;
use OCP\Authentication\Exceptions\WipeTokenException;
use OCP\Authentication\Token\IToken;
use OCP\ISession;
use OCP\IUserSession;
use OCP\Session\Exceptions\SessionNotAvailableException;
use OCP\User\Backend\IPasswordConfirmationBackend;
use ReflectionMethod;

Expand All @@ -27,6 +33,7 @@ class PasswordConfirmationMiddleware extends Middleware {
private $timeFactory;
/** @var array */
private $excludedUserBackEnds = ['user_saml' => true, 'user_globalsiteselector' => true];
private IProvider $tokenProvider;

/**
* PasswordConfirmationMiddleware constructor.
Expand All @@ -39,11 +46,14 @@ class PasswordConfirmationMiddleware extends Middleware {
public function __construct(ControllerMethodReflector $reflector,
ISession $session,
IUserSession $userSession,
ITimeFactory $timeFactory) {
ITimeFactory $timeFactory,
IProvider $tokenProvider,
) {
$this->reflector = $reflector;
$this->session = $session;
$this->userSession = $userSession;
$this->timeFactory = $timeFactory;
$this->tokenProvider = $tokenProvider;
}

/**
Expand All @@ -68,8 +78,21 @@ public function beforeController($controller, $methodName) {
$backendClassName = $user->getBackendClassName();
}

try {
$sessionId = $this->session->getId();
$token = $this->tokenProvider->getToken($sessionId);
} catch (SessionNotAvailableException|InvalidTokenException|WipeTokenException|ExpiredTokenException) {
// States we do not deal with here.
return;
}
$scope = $token->getScopeAsArray();
if (isset($scope[IToken::SCOPE_SKIP_PASSWORD_VALIDATION]) && $scope[IToken::SCOPE_SKIP_PASSWORD_VALIDATION] === true) {
// Users logging in from SSO backends cannot confirm their password by design
return;
}

$lastConfirm = (int) $this->session->get('last-password-confirm');
// we can't check the password against a SAML backend, so skip password confirmation in this case
// TODO: confirm excludedUserBackEnds can go away and remove it
if (!isset($this->excludedUserBackEnds[$backendClassName]) && $lastConfirm < ($this->timeFactory->getTime() - (30 * 60 + 15))) { // allow 15 seconds delay
throw new NotConfirmedException();
}
Expand Down
3 changes: 2 additions & 1 deletion lib/private/Authentication/Token/PublicKeyToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace OC\Authentication\Token;

use OCP\AppFramework\Db\Entity;
use OCP\Authentication\Token\IToken;

/**
* @method void setId(int $id)
Expand Down Expand Up @@ -162,7 +163,7 @@ public function getScopeAsArray(): array {
$scope = json_decode($this->getScope(), true);
if (!$scope) {
return [
'filesystem' => true
IToken::SCOPE_FILESYSTEM => true
];
}
return $scope;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ public function renewSessionToken(string $oldSessionId, string $sessionId): OCPI
OCPIToken::TEMPORARY_TOKEN,
$token->getRemember()
);
$newToken->setScope($token->getScopeAsArray());
$this->cacheToken($newToken);

$this->cacheInvalidHash($token->getToken());
Expand Down
4 changes: 2 additions & 2 deletions lib/private/Lockdown/LockdownManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
namespace OC\Lockdown;

use OC\Authentication\Token\IToken;
use OCP\Authentication\Token\IToken;
use OCP\ISession;
use OCP\Lockdown\ILockdownManager;

Expand Down Expand Up @@ -60,6 +60,6 @@ public function setToken(IToken $token) {

public function canAccessFilesystem() {
$scope = $this->getScopeAsArray();
return !$scope || $scope['filesystem'];
return !$scope || $scope[IToken::SCOPE_FILESYSTEM];
}
}
74 changes: 38 additions & 36 deletions lib/private/Template/JSConfigHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@
namespace OC\Template;

use bantu\IniGetWrapper\IniGetWrapper;
use OC\Authentication\Token\IProvider;
use OC\CapabilitiesManager;
use OC\Share\Share;
use OCP\App\AppPathNotFoundException;
use OCP\App\IAppManager;
use OCP\Authentication\Exceptions\ExpiredTokenException;
use OCP\Authentication\Exceptions\InvalidTokenException;
use OCP\Authentication\Exceptions\WipeTokenException;
use OCP\Authentication\Token\IToken;
use OCP\Constants;
use OCP\Defaults;
use OCP\Files\FileInfo;
Expand All @@ -23,48 +28,30 @@
use OCP\ISession;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\Session\Exceptions\SessionNotAvailableException;
use OCP\Share\IManager as IShareManager;
use OCP\User\Backend\IPasswordConfirmationBackend;
use OCP\Util;

class JSConfigHelper {
protected IL10N $l;
protected Defaults $defaults;
protected IAppManager $appManager;
protected ISession $session;
protected ?IUser $currentUser;
protected IConfig $config;
protected IGroupManager $groupManager;
protected IniGetWrapper $iniWrapper;
protected IURLGenerator $urlGenerator;
protected CapabilitiesManager $capabilitiesManager;
protected IInitialStateService $initialStateService;

/** @var array user back-ends excluded from password verification */
private $excludedUserBackEnds = ['user_saml' => true, 'user_globalsiteselector' => true];

public function __construct(IL10N $l,
Defaults $defaults,
IAppManager $appManager,
ISession $session,
?IUser $currentUser,
IConfig $config,
IGroupManager $groupManager,
IniGetWrapper $iniWrapper,
IURLGenerator $urlGenerator,
CapabilitiesManager $capabilitiesManager,
IInitialStateService $initialStateService) {
$this->l = $l;
$this->defaults = $defaults;
$this->appManager = $appManager;
$this->session = $session;
$this->currentUser = $currentUser;
$this->config = $config;
$this->groupManager = $groupManager;
$this->iniWrapper = $iniWrapper;
$this->urlGenerator = $urlGenerator;
$this->capabilitiesManager = $capabilitiesManager;
$this->initialStateService = $initialStateService;
public function __construct(
protected IL10N $l,
protected Defaults $defaults,
protected IAppManager $appManager,
protected ISession $session,
protected ?IUser $currentUser,
protected IConfig $config,
protected IGroupManager $groupManager,
protected IniGetWrapper $iniWrapper,
protected IURLGenerator $urlGenerator,
protected CapabilitiesManager $capabilitiesManager,
protected IInitialStateService $initialStateService,
protected IProvider $tokenProvider,
) {
}

public function getConfig(): string {
Expand Down Expand Up @@ -130,9 +117,13 @@ public function getConfig(): string {
}

if ($this->currentUser instanceof IUser) {
$lastConfirmTimestamp = $this->session->get('last-password-confirm');
if (!is_int($lastConfirmTimestamp)) {
$lastConfirmTimestamp = 0;
if ($this->canUserValidatePassword()) {
$lastConfirmTimestamp = $this->session->get('last-password-confirm');
if (!is_int($lastConfirmTimestamp)) {
$lastConfirmTimestamp = 0;
}
} else {
$lastConfirmTimestamp = PHP_INT_MAX;
}
} else {
$lastConfirmTimestamp = 0;
Expand Down Expand Up @@ -287,4 +278,15 @@ public function getConfig(): string {

return $result;
}

protected function canUserValidatePassword(): bool {
try {
$token = $this->tokenProvider->getToken($this->session->getId());
} catch (ExpiredTokenException|WipeTokenException|InvalidTokenException|SessionNotAvailableException) {
// actually we do not know, so we fall back to this statement
return true;
}
$scope = $token->getScopeAsArray();
return !isset($scope[IToken::SCOPE_SKIP_PASSWORD_VALIDATION]) || $scope[IToken::SCOPE_SKIP_PASSWORD_VALIDATION] === false;
}
}
4 changes: 3 additions & 1 deletion lib/private/TemplateLayout.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace OC;

use bantu\IniGetWrapper\IniGetWrapper;
use OC\Authentication\Token\IProvider;
use OC\Search\SearchQuery;
use OC\Template\CSSResourceLocator;
use OC\Template\JSConfigHelper;
Expand Down Expand Up @@ -224,7 +225,8 @@ public function __construct($renderAs, $appId = '') {
\OC::$server->get(IniGetWrapper::class),
\OC::$server->getURLGenerator(),
\OC::$server->get(CapabilitiesManager::class),
\OCP\Server::get(IInitialStateService::class)
\OCP\Server::get(IInitialStateService::class),
\OCP\Server::get(IProvider::class),
);
$config = $jsConfigHelper->getConfig();
if (\OC::$server->getContentSecurityPolicyNonceManager()->browserSupportsCspV3()) {
Expand Down
Loading
Loading