From 0b02a6d123b2014705998c94ddf3d460948d3eac Mon Sep 17 00:00:00 2001 From: redmatrix Date: Tue, 10 May 2016 17:26:44 -0700 Subject: initial sabre upgrade (needs lots of work - to wit: authentication, redo the browser interface, and rework event export/import) --- .../Sabre/DAV/Auth/Backend/AbstractBasicTest.php | 69 ++++++------ .../Sabre/DAV/Auth/Backend/AbstractDigestTest.php | 124 ++++++++++----------- .../tests/Sabre/DAV/Auth/Backend/ApacheTest.php | 53 ++++++--- .../dav/tests/Sabre/DAV/Auth/Backend/FileTest.php | 2 +- .../dav/tests/Sabre/DAV/Auth/Backend/Mock.php | 82 +++++++++++--- .../tests/Sabre/DAV/Auth/Backend/PDOMySQLTest.php | 18 +-- .../sabre/dav/tests/Sabre/DAV/Auth/PluginTest.php | 94 ++++++++++++---- 7 files changed, 279 insertions(+), 163 deletions(-) (limited to 'vendor/sabre/dav/tests/Sabre/DAV/Auth') diff --git a/vendor/sabre/dav/tests/Sabre/DAV/Auth/Backend/AbstractBasicTest.php b/vendor/sabre/dav/tests/Sabre/DAV/Auth/Backend/AbstractBasicTest.php index 36d23c5c0..7d7a59898 100644 --- a/vendor/sabre/dav/tests/Sabre/DAV/Auth/Backend/AbstractBasicTest.php +++ b/vendor/sabre/dav/tests/Sabre/DAV/Auth/Backend/AbstractBasicTest.php @@ -5,67 +5,68 @@ namespace Sabre\DAV\Auth\Backend; use Sabre\DAV; use Sabre\HTTP; -require_once 'Sabre/HTTP/ResponseMock.php'; - class AbstractBasicTest extends \PHPUnit_Framework_TestCase { - /** - * @expectedException Sabre\DAV\Exception\NotAuthenticated - */ - public function testAuthenticateNoHeaders() { + function testCheckNoHeaders() { - $response = new HTTP\ResponseMock(); - $server = new DAV\Server(); - $server->httpResponse = $response; + $request = new HTTP\Request(); + $response = new HTTP\Response(); $backend = new AbstractBasicMock(); - $backend->authenticate($server,'myRealm'); - } + $this->assertFalse( + $backend->check($request, $response)[0] + ); - /** - * @expectedException Sabre\DAV\Exception\NotAuthenticated - */ - public function testAuthenticateUnknownUser() { + } - $response = new HTTP\ResponseMock(); - $tree = new DAV\ObjectTree(new DAV\SimpleCollection('bla')); - $server = new DAV\Server($tree); - $server->httpResponse = $response; + function testCheckUnknownUser() { - $request = new HTTP\Request(array( + $request = HTTP\Sapi::createFromServerArray(array( 'PHP_AUTH_USER' => 'username', 'PHP_AUTH_PW' => 'wrongpassword', )); - $server->httpRequest = $request; + $response = new HTTP\Response(); $backend = new AbstractBasicMock(); - $backend->authenticate($server,'myRealm'); - } + $this->assertFalse( + $backend->check($request, $response)[0] + ); - public function testAuthenticate() { + } - $response = new HTTP\ResponseMock(); - $tree = new DAV\ObjectTree(new DAV\SimpleCollection('bla')); - $server = new DAV\Server($tree); - $server->httpResponse = $response; + function testCheckSuccess() { - $request = new HTTP\Request(array( + $request = HTTP\Sapi::createFromServerArray(array( 'PHP_AUTH_USER' => 'username', 'PHP_AUTH_PW' => 'password', )); - $server->httpRequest = $request; + $response = new HTTP\Response(); $backend = new AbstractBasicMock(); - $this->assertTrue($backend->authenticate($server,'myRealm')); + $this->assertEquals( + [true, 'principals/username'], + $backend->check($request, $response) + ); - $result = $backend->getCurrentUser(); + } - $this->assertEquals('username', $result); + function testRequireAuth() { - } + $request = new HTTP\Request(); + $response = new HTTP\Response(); + $backend = new AbstractBasicMock(); + $backend->setRealm('writing unittests on a saturday night'); + $backend->challenge($request, $response); + + $this->assertEquals( + 'Basic realm="writing unittests on a saturday night"', + $response->getHeader('WWW-Authenticate') + ); + + } } diff --git a/vendor/sabre/dav/tests/Sabre/DAV/Auth/Backend/AbstractDigestTest.php b/vendor/sabre/dav/tests/Sabre/DAV/Auth/Backend/AbstractDigestTest.php index 495690c4e..8ef416c37 100644 --- a/vendor/sabre/dav/tests/Sabre/DAV/Auth/Backend/AbstractDigestTest.php +++ b/vendor/sabre/dav/tests/Sabre/DAV/Auth/Backend/AbstractDigestTest.php @@ -5,130 +5,120 @@ namespace Sabre\DAV\Auth\Backend; use Sabre\DAV; use Sabre\HTTP; -require_once 'Sabre/HTTP/ResponseMock.php'; - class AbstractDigestTest extends \PHPUnit_Framework_TestCase { - /** - * @expectedException Sabre\DAV\Exception\NotAuthenticated - */ - public function testAuthenticateNoHeaders() { + function testCheckNoHeaders() { - $response = new HTTP\ResponseMock(); - $server = new DAV\Server(); - $server->httpResponse = $response; + $request = new HTTP\Request(); + $response = new HTTP\Response(); $backend = new AbstractDigestMock(); - $backend->authenticate($server,'myRealm'); + $this->assertFalse( + $backend->check($request, $response)[0] + ); } - /** - * @expectedException Sabre\DAV\Exception - */ - public function testAuthenticateBadGetUserInfoResponse() { - - $response = new HTTP\ResponseMock(); - $server = new DAV\Server(); - $server->httpResponse = $response; + function testCheckBadGetUserInfoResponse() { $header = 'username=null, realm=myRealm, nonce=12345, uri=/, response=HASH, opaque=1, qop=auth, nc=1, cnonce=1'; - $request = new HTTP\Request(array( + $request = HTTP\Sapi::createFromServerArray([ 'PHP_AUTH_DIGEST' => $header, - )); - $server->httpRequest = $request; + ]); + $response = new HTTP\Response(); $backend = new AbstractDigestMock(); - $backend->authenticate($server,'myRealm'); + $this->assertFalse( + $backend->check($request, $response)[0] + ); } /** * @expectedException Sabre\DAV\Exception */ - public function testAuthenticateBadGetUserInfoResponse2() { - - $response = new HTTP\ResponseMock(); - $server = new DAV\Server(); - $server->httpResponse = $response; + function testCheckBadGetUserInfoResponse2() { $header = 'username=array, realm=myRealm, nonce=12345, uri=/, response=HASH, opaque=1, qop=auth, nc=1, cnonce=1'; - $request = new HTTP\Request(array( + $request = HTTP\Sapi::createFromServerArray([ 'PHP_AUTH_DIGEST' => $header, - )); - $server->httpRequest = $request; + ]); + + $response = new HTTP\Response(); $backend = new AbstractDigestMock(); - $backend->authenticate($server,'myRealm'); + $backend->check($request, $response); } - /** - * @expectedException Sabre\DAV\Exception\NotAuthenticated - */ - public function testAuthenticateUnknownUser() { - - $response = new HTTP\ResponseMock(); - $server = new DAV\Server(); - $server->httpResponse = $response; + function testCheckUnknownUser() { $header = 'username=false, realm=myRealm, nonce=12345, uri=/, response=HASH, opaque=1, qop=auth, nc=1, cnonce=1'; - $request = new HTTP\Request(array( + $request = HTTP\Sapi::createFromServerArray([ 'PHP_AUTH_DIGEST' => $header, - )); - $server->httpRequest = $request; + ]); + + $response = new HTTP\Response(); $backend = new AbstractDigestMock(); - $backend->authenticate($server,'myRealm'); + $this->assertFalse( + $backend->check($request, $response)[0] + ); } - /** - * @expectedException Sabre\DAV\Exception\NotAuthenticated - */ - public function testAuthenticateBadPassword() { - - $response = new HTTP\ResponseMock(); - $server = new DAV\Server(); - $server->httpResponse = $response; + function testCheckBadPassword() { $header = 'username=user, realm=myRealm, nonce=12345, uri=/, response=HASH, opaque=1, qop=auth, nc=1, cnonce=1'; - $request = new HTTP\Request(array( + $request = HTTP\Sapi::createFromServerArray([ 'PHP_AUTH_DIGEST' => $header, 'REQUEST_METHOD' => 'PUT', - )); - $server->httpRequest = $request; + ]); + + $response = new HTTP\Response(); $backend = new AbstractDigestMock(); - $backend->authenticate($server,'myRealm'); + $this->assertFalse( + $backend->check($request, $response)[0] + ); } - public function testAuthenticate() { - - $response = new HTTP\ResponseMock(); - $server = new DAV\Server(); - $server->httpResponse = $response; + function testCheck() { $digestHash = md5('HELLO:12345:1:1:auth:' . md5('GET:/')); $header = 'username=user, realm=myRealm, nonce=12345, uri=/, response='.$digestHash.', opaque=1, qop=auth, nc=1, cnonce=1'; - $request = new HTTP\Request(array( + $request = HTTP\Sapi::createFromServerArray(array( 'REQUEST_METHOD' => 'GET', 'PHP_AUTH_DIGEST' => $header, 'REQUEST_URI' => '/', )); - $server->httpRequest = $request; + + $response = new HTTP\Response(); $backend = new AbstractDigestMock(); - $this->assertTrue($backend->authenticate($server,'myRealm')); + $this->assertEquals( + [true, 'principals/user'], + $backend->check($request, $response) + ); - $result = $backend->getCurrentUser(); + } - $this->assertEquals('user', $result); - $this->assertEquals('HELLO', $backend->getDigestHash('myRealm', $result)); + function testRequireAuth() { - } + $request = new HTTP\Request(); + $response = new HTTP\Response(); + $backend = new AbstractDigestMock(); + $backend->setRealm('writing unittests on a saturday night'); + $backend->challenge($request, $response); + + $this->assertStringStartsWith( + 'Digest realm="writing unittests on a saturday night"', + $response->getHeader('WWW-Authenticate') + ); + + } } diff --git a/vendor/sabre/dav/tests/Sabre/DAV/Auth/Backend/ApacheTest.php b/vendor/sabre/dav/tests/Sabre/DAV/Auth/Backend/ApacheTest.php index b1ed555d4..697b593db 100644 --- a/vendor/sabre/dav/tests/Sabre/DAV/Auth/Backend/ApacheTest.php +++ b/vendor/sabre/dav/tests/Sabre/DAV/Auth/Backend/ApacheTest.php @@ -10,36 +10,63 @@ class ApacheTest extends \PHPUnit_Framework_TestCase { function testConstruct() { $backend = new Apache(); + $this->assertInstanceOf('Sabre\DAV\Auth\Backend\Apache', $backend); } - /** - * @expectedException Sabre\DAV\Exception - */ function testNoHeader() { - $server = new DAV\Server(); + $request = new HTTP\Request(); + $response = new HTTP\Response(); $backend = new Apache(); - $backend->authenticate($server,'Realm'); + + $this->assertFalse( + $backend->check($request, $response)[0] + ); } function testRemoteUser() { + $request = HTTP\Sapi::createFromServerArray([ + 'REMOTE_USER' => 'username', + ]); + $response = new HTTP\Response(); $backend = new Apache(); - $server = new DAV\Server(); - $request = new HTTP\Request(array( - 'REMOTE_USER' => 'username', - )); - $server->httpRequest = $request; + $this->assertEquals( + [true, 'principals/username'], + $backend->check($request, $response) + ); - $this->assertTrue($backend->authenticate($server, 'Realm')); + } + + function testRedirectRemoteUser() { - $userInfo = 'username'; + $request = HTTP\Sapi::createFromServerArray([ + 'REDIRECT_REMOTE_USER' => 'username', + ]); + $response = new HTTP\Response(); + $backend = new Apache(); - $this->assertEquals($userInfo, $backend->getCurrentUser()); + $this->assertEquals( + [true, 'principals/username'], + $backend->check($request, $response) + ); } + function testRequireAuth() { + + $request = new HTTP\Request(); + $response = new HTTP\Response(); + + $backend = new Apache(); + $backend->challenge($request, $response); + + $this->assertNull( + $response->getHeader('WWW-Authenticate') + ); + + } } diff --git a/vendor/sabre/dav/tests/Sabre/DAV/Auth/Backend/FileTest.php b/vendor/sabre/dav/tests/Sabre/DAV/Auth/Backend/FileTest.php index 72f150ab6..d2e5fe49b 100644 --- a/vendor/sabre/dav/tests/Sabre/DAV/Auth/Backend/FileTest.php +++ b/vendor/sabre/dav/tests/Sabre/DAV/Auth/Backend/FileTest.php @@ -35,7 +35,7 @@ class FileTest extends \PHPUnit_Framework_TestCase { $file->loadFile(SABRE_TEMPDIR . '/backend'); $this->assertFalse($file->getDigestHash('realm','blabla')); - $this->assertEquals(md5('user:realm:password'), $file->getDigesthash('realm','user')); + $this->assertEquals(md5('user:realm:password'), $file->getDigestHash('realm','user')); } diff --git a/vendor/sabre/dav/tests/Sabre/DAV/Auth/Backend/Mock.php b/vendor/sabre/dav/tests/Sabre/DAV/Auth/Backend/Mock.php index fdad8a605..a782cb74d 100644 --- a/vendor/sabre/dav/tests/Sabre/DAV/Auth/Backend/Mock.php +++ b/vendor/sabre/dav/tests/Sabre/DAV/Auth/Backend/Mock.php @@ -2,35 +2,85 @@ namespace Sabre\DAV\Auth\Backend; -use Sabre\DAV; +use + Sabre\DAV, + Sabre\HTTP\RequestInterface, + Sabre\HTTP\ResponseInterface; class Mock implements BackendInterface { - protected $currentUser; + public $fail = false; - public $defaultUser = 'admin'; + public $invalidCheckResponse = false; - /** - * @param Sabre\DAV\Server $server - * @param string $realm - * @throws Sabre\DAV\Exception\NotAuthenticated - */ - function authenticate(DAV\Server $server, $realm) { + public $principal = 'principals/admin'; - if ($realm=='failme') throw new DAV\Exception\NotAuthenticated('deliberate fail'); - $this->currentUser = $this->defaultUser; + function setPrincipal($principal) { + + $this->principal = $principal; } - function setCurrentUser($user) { + /** + * When this method is called, the backend must check if authentication was + * successful. + * + * The returned value must be one of the following + * + * [true, "principals/username"] + * [false, "reason for failure"] + * + * If authentication was successful, it's expected that the authentication + * backend returns a so-called principal url. + * + * Examples of a principal url: + * + * principals/admin + * principals/user1 + * principals/users/joe + * principals/uid/123457 + * + * If you don't use WebDAV ACL (RFC3744) we recommend that you simply + * return a string such as: + * + * principals/users/[username] + * + * @param RequestInterface $request + * @param ResponseInterface $response + * @return array + */ + function check(RequestInterface $request, ResponseInterface $response) { - $this->currentUser = $user; + if ($this->invalidCheckResponse) { + return 'incorrect!'; + } + if ($this->fail) { + return [false, "fail!"]; + } + return [true, $this->principal]; } - function getCurrentUser() { - - return $this->currentUser; + /** + * This method is called when a user could not be authenticated, and + * authentication was required for the current request. + * + * This gives you the oppurtunity to set authentication headers. The 401 + * status code will already be set. + * + * In this case of Basic Auth, this would for example mean that the + * following header needs to be set: + * + * $response->addHeader('WWW-Authenticate', 'Basic realm=SabreDAV'); + * + * Keep in mind that in the case of multiple authentication backends, other + * WWW-Authenticate headers may already have been set, and you'll want to + * append your own WWW-Authenticate header instead of overwriting the + * existing one. + * + * @return void + */ + function challenge(RequestInterface $request, ResponseInterface $response) { } diff --git a/vendor/sabre/dav/tests/Sabre/DAV/Auth/Backend/PDOMySQLTest.php b/vendor/sabre/dav/tests/Sabre/DAV/Auth/Backend/PDOMySQLTest.php index ede432de2..8de2be667 100644 --- a/vendor/sabre/dav/tests/Sabre/DAV/Auth/Backend/PDOMySQLTest.php +++ b/vendor/sabre/dav/tests/Sabre/DAV/Auth/Backend/PDOMySQLTest.php @@ -12,15 +12,17 @@ class PDOMySQLTest extends AbstractPDOTest { $pdo = \Sabre\TestUtil::getMySQLDB(); if (!$pdo) $this->markTestSkipped('Could not connect to MySQL database'); $pdo->query("DROP TABLE IF EXISTS users"); - $pdo->query(" + $pdo->query(<<query("INSERT INTO users (username,digesta1,email,displayname) VALUES ('user','hash','user@example.org','User')"); diff --git a/vendor/sabre/dav/tests/Sabre/DAV/Auth/PluginTest.php b/vendor/sabre/dav/tests/Sabre/DAV/Auth/PluginTest.php index 2096a04d7..0ac9e0613 100644 --- a/vendor/sabre/dav/tests/Sabre/DAV/Auth/PluginTest.php +++ b/vendor/sabre/dav/tests/Sabre/DAV/Auth/PluginTest.php @@ -12,10 +12,11 @@ class PluginTest extends \PHPUnit_Framework_TestCase { function testInit() { $fakeServer = new DAV\Server( new DAV\SimpleCollection('bla')); - $plugin = new Plugin(new Backend\Mock(),'realm'); + $plugin = new Plugin(new Backend\Mock()); $this->assertTrue($plugin instanceof Plugin); $fakeServer->addPlugin($plugin); $this->assertEquals($plugin, $fakeServer->getPlugin('auth')); + $this->assertInternalType('array', $plugin->getPluginInfo()); } @@ -25,14 +26,14 @@ class PluginTest extends \PHPUnit_Framework_TestCase { function testAuthenticate() { $fakeServer = new DAV\Server( new DAV\SimpleCollection('bla')); - $plugin = new Plugin(new Backend\Mock(),'realm'); + $plugin = new Plugin(new Backend\Mock()); $fakeServer->addPlugin($plugin); - $fakeServer->broadCastEvent('beforeMethod',array('GET','/')); + $this->assertTrue( + $fakeServer->emit('beforeMethod', [new HTTP\Request(), new HTTP\Response()]) + ); } - - /** * @depends testInit * @expectedException Sabre\DAV\Exception\NotAuthenticated @@ -40,42 +41,87 @@ class PluginTest extends \PHPUnit_Framework_TestCase { function testAuthenticateFail() { $fakeServer = new DAV\Server( new DAV\SimpleCollection('bla')); - $plugin = new Plugin(new Backend\Mock(),'failme'); + $backend = new Backend\Mock(); + $backend->fail = true; + + $plugin = new Plugin($backend); $fakeServer->addPlugin($plugin); - $fakeServer->broadCastEvent('beforeMethod',array('GET','/')); + $fakeServer->emit('beforeMethod', [new HTTP\Request(), new HTTP\Response()]); } - function testReportPassThrough() { + /** + * @depends testAuthenticate + */ + function testMultipleBackend() { - $fakeServer = new DAV\Server(new DAV\SimpleCollection('bla')); - $plugin = new Plugin(new Backend\Mock(),'realm'); - $fakeServer->addPlugin($plugin); + $fakeServer = new DAV\Server( new DAV\SimpleCollection('bla')); + $backend1 = new Backend\Mock(); + $backend2 = new Backend\Mock(); + $backend2->fail = true; - $request = new HTTP\Request(array( - 'REQUEST_METHOD' => 'REPORT', - 'HTTP_CONTENT_TYPE' => 'application/xml', - 'REQUEST_URI' => '/', - )); - $request->setBody(''); + $plugin = new Plugin(); + $plugin->addBackend($backend1); + $plugin->addBackend($backend2); - $fakeServer->httpRequest = $request; - $fakeServer->httpResponse = new HTTP\ResponseMock(); - $fakeServer->exec(); + $fakeServer->addPlugin($plugin); + $fakeServer->emit('beforeMethod', [new HTTP\Request(), new HTTP\Response()]); - $this->assertEquals('HTTP/1.1 403 Forbidden', $fakeServer->httpResponse->status); + $this->assertEquals('principals/admin', $plugin->getCurrentPrincipal()); } /** * @depends testInit + * @expectedException Sabre\DAV\Exception + */ + function testNoAuthBackend() { + + $fakeServer = new DAV\Server( new DAV\SimpleCollection('bla')); + + $plugin = new Plugin(); + $fakeServer->addPlugin($plugin); + $fakeServer->emit('beforeMethod', [new HTTP\Request(), new HTTP\Response()]); + + } + /** + * @depends testInit + * @expectedException Sabre\DAV\Exception + */ + function testInvalidCheckResponse() { + + $fakeServer = new DAV\Server( new DAV\SimpleCollection('bla')); + $backend = new Backend\Mock(); + $backend->invalidCheckResponse = true; + + $plugin = new Plugin($backend); + $fakeServer->addPlugin($plugin); + $fakeServer->emit('beforeMethod', [new HTTP\Request(), new HTTP\Response()]); + + } + + /** + * @depends testAuthenticate + */ + function testGetCurrentPrincipal() { + + $fakeServer = new DAV\Server( new DAV\SimpleCollection('bla')); + $plugin = new Plugin(new Backend\Mock()); + $fakeServer->addPlugin($plugin); + $fakeServer->emit('beforeMethod', [new HTTP\Request(), new HTTP\Response()]); + $this->assertEquals('principals/admin', $plugin->getCurrentPrincipal()); + + } + + /** + * @depends testAuthenticate */ - function testGetCurrentUserPrincipal() { + function testGetCurrentUser() { $fakeServer = new DAV\Server( new DAV\SimpleCollection('bla')); - $plugin = new Plugin(new Backend\Mock(),'realm'); + $plugin = new Plugin(new Backend\Mock()); $fakeServer->addPlugin($plugin); - $fakeServer->broadCastEvent('beforeMethod',array('GET','/')); + $fakeServer->emit('beforeMethod', [new HTTP\Request(), new HTTP\Response()]); $this->assertEquals('admin', $plugin->getCurrentUser()); } -- cgit v1.2.3