diff --git a/appinfo/routes.php b/appinfo/routes.php index b41c3229..47c11ece 100755 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -33,6 +33,7 @@ # Routes for admin settings ['name' => 'settings#addClient', 'url' => '/clients', 'verb' => 'POST'], ['name' => 'settings#deleteClient', 'url' => '/clients/{id}/delete', 'verb' => 'POST'], + ['name' => 'settings#test', 'url' => '/test', 'verb' => 'POST'], # Routes for personal settings ['name' => 'settings#revokeAuthorization', 'url' => '/clients/{id}/revoke', 'verb' => 'POST'] ] diff --git a/js/settings.js b/js/settings.js index cabe9084..c7f9e8d1 100644 --- a/js/settings.js +++ b/js/settings.js @@ -27,4 +27,19 @@ $(document).ready(function () { } }, false); } + + var testToken = Math.random().toString(); + $.ajax({ + type: 'POST', + url: OC.generateUrl('apps/oauth2/test'), + headers: { + 'Authorization': 'Bearer ' + testToken + } + }).done(function(data){ + if (data.authHeaderFound !== true) { + OC.Notification.show( + 'Oauth2 will not work properly as your webserver does not pass Authorization header to PHP.' + ); + } + }); }); diff --git a/lib/Controller/SettingsController.php b/lib/Controller/SettingsController.php index f3a7a9ed..d66dace8 100644 --- a/lib/Controller/SettingsController.php +++ b/lib/Controller/SettingsController.php @@ -26,6 +26,7 @@ use OCA\OAuth2\Db\RefreshTokenMapper; use OCA\OAuth2\Utilities; use OCP\AppFramework\Controller; +use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\Http\RedirectResponse; use OCP\ILogger; use OCP\IRequest; @@ -193,4 +194,15 @@ public function revokeAuthorization($id) { ['sectionid' => 'security'] ) . '#oauth2'); } + + /** + * Checks if the server configured properly + * + * @return string[] + */ + public function test() { + return [ + 'authHeaderFound' => $this->request->getHeader('Authorization') !== null + ]; + } } diff --git a/tests/Unit/Controller/SettingsControllerTest.php b/tests/Unit/Controller/SettingsControllerTest.php index b8f47bcd..8b83cb67 100755 --- a/tests/Unit/Controller/SettingsControllerTest.php +++ b/tests/Unit/Controller/SettingsControllerTest.php @@ -42,6 +42,9 @@ class SettingsControllerTest extends TestCase { /** @var SettingsController $controller */ private $controller; + /** @var IRequest | \PHPUnit\Framework\MockObject\MockObject */ + private $request; + /** @var ClientMapper $clientMapper */ private $clientMapper; @@ -77,6 +80,7 @@ public function setUp() { $this->appName = $container->query('AppName'); + $this->request = $this->getMockBuilder(IRequest::class)->getMock(); $this->clientMapper = $container->query('OCA\OAuth2\Db\ClientMapper'); $this->clientMapper->deleteAll(); $this->authorizationCodeMapper = $container->query('OCA\OAuth2\Db\AuthorizationCodeMapper'); @@ -120,7 +124,7 @@ public function setUp() { $this->controller = new SettingsController( $this->appName, - $this->getMockBuilder(IRequest::class)->getMock(), + $this->request, $this->clientMapper, $this->authorizationCodeMapper, $this->accessTokenMapper, @@ -249,4 +253,22 @@ public function testRevokeAuthorization() { $this->assertEquals(0, \count($this->accessTokenMapper->findAll())); $this->assertEquals(0, \count($this->refreshTokenMapper->findAll())); } + + public function healthDataProvider() { + return [ + ['someToken', ['authHeaderFound' => true]], + [null, ['authHeaderFound' => false]] + ]; + } + + /** + * @dataProvider healthDataProvider + * @param string $authHeader + * @param array $expectedResult + */ + public function testTest($authHeader, $expectedResult) { + $this->request->method('getHeader')->willReturn($authHeader); + $result = $this->controller->test(); + $this->assertEquals($result, $expectedResult); + } }