do not hard-require the token provider

The provider might need DB access and therefore depenedency
resolution fails on the setup page where we cannot inject
the db implementation.

Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
This commit is contained in:
Christoph Wurst 2017-01-02 11:57:05 +01:00 committed by Roeland Jago Douma
parent 6f74ecd94a
commit 21d3fe5883
No known key found for this signature in database
GPG Key ID: F941078878347C0C
3 changed files with 25 additions and 9 deletions

View File

@ -40,21 +40,21 @@ class Store implements IStore {
/** @var ISession */ /** @var ISession */
private $session; private $session;
/** @var IProvider */
private $tokenProvider;
/** @var ILogger */ /** @var ILogger */
private $logger; private $logger;
/** @var IProvider|null */
private $tokenProvider;
/** /**
* @param ISession $session * @param ISession $session
* @param IProvider $tokenProvider
* @param ILogger $logger * @param ILogger $logger
* @param IProvider $tokenProvider
*/ */
public function __construct(ISession $session, IProvider $tokenProvider, ILogger $logger) { public function __construct(ISession $session, ILogger $logger, IProvider $tokenProvider = null) {
$this->session = $session; $this->session = $session;
$this->tokenProvider = $tokenProvider;
$this->logger = $logger; $this->logger = $logger;
$this->tokenProvider = $tokenProvider;
Util::connectHook('OC_User', 'post_login', $this, 'authenticate'); Util::connectHook('OC_User', 'post_login', $this, 'authenticate');
} }
@ -84,6 +84,10 @@ class Store implements IStore {
* @throws CredentialsUnavailableException * @throws CredentialsUnavailableException
*/ */
public function getLoginCredentials() { public function getLoginCredentials() {
if (is_null($this->tokenProvider)) {
throw new CredentialsUnavailableException();
}
$trySession = false; $trySession = false;
try { try {
$sessionId = $this->session->getId(); $sessionId = $this->session->getId();

View File

@ -250,9 +250,13 @@ class Server extends ServerContainer implements IServerContainer {
}); });
$this->registerService(Store::class, function(Server $c) { $this->registerService(Store::class, function(Server $c) {
$session = $c->getSession(); $session = $c->getSession();
$tokenProvider = $c->query('OC\Authentication\Token\DefaultTokenProvider'); if (\OC::$server->getSystemConfig()->getValue('installed', false)) {
$tokenProvider = $c->query('OC\Authentication\Token\IProvider');
} else {
$tokenProvider = null;
}
$logger = $c->getLogger(); $logger = $c->getLogger();
return new Store($session, $tokenProvider, $logger); return new Store($session, $logger, $tokenProvider);
}); });
$this->registerAlias(IStore::class, Store::class); $this->registerAlias(IStore::class, Store::class);
$this->registerService('OC\Authentication\Token\DefaultTokenMapper', function (Server $c) { $this->registerService('OC\Authentication\Token\DefaultTokenMapper', function (Server $c) {

View File

@ -58,7 +58,7 @@ class StoreTest extends TestCase {
$this->tokenProvider = $this->createMock(IProvider::class); $this->tokenProvider = $this->createMock(IProvider::class);
$this->logger = $this->createMock(ILogger::class); $this->logger = $this->createMock(ILogger::class);
$this->store = new Store($this->session, $this->tokenProvider, $this->logger); $this->store = new Store($this->session, $this->logger, $this->tokenProvider);
} }
public function testAuthenticate() { public function testAuthenticate() {
@ -81,6 +81,14 @@ class StoreTest extends TestCase {
$this->store->setSession($session); $this->store->setSession($session);
} }
public function testGetLoginCredentialsNoTokenProvider() {
$this->store = new Store($this->session, $this->logger, null);
$this->expectException(CredentialsUnavailableException::class);
$this->store->getLoginCredentials();
}
public function testGetLoginCredentials() { public function testGetLoginCredentials() {
$uid = 'uid'; $uid = 'uid';
$user = 'user123'; $user = 'user123';