Skip to content

Commit

Permalink
Show warning in case the server cuts out auth header
Browse files Browse the repository at this point in the history
  • Loading branch information
VicDeo committed Sep 20, 2019
1 parent fc47f94 commit d931388
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
1 change: 1 addition & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']
]
Expand Down
15 changes: 15 additions & 0 deletions js/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
);
}
});
});
12 changes: 12 additions & 0 deletions lib/Controller/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
];
}
}
24 changes: 23 additions & 1 deletion tests/Unit/Controller/SettingsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
}

0 comments on commit d931388

Please sign in to comment.