Skip to content

Commit 7d8290e

Browse files
author
Boy Baukema
committed
Merge pull request #226 from OpenConext/bugfix/push-metadata
Bugfixes for push metadata
2 parents d37607b + a29e3ec commit 7d8290e

File tree

15 files changed

+182
-3757
lines changed

15 files changed

+182
-3757
lines changed
Lines changed: 63 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,15 @@
11
<?php
22

33
use OpenConext\Component\EngineBlockMetadata\Entity\Assembler\JanusPushMetadataAssembler;
4-
use OpenConext\Component\EngineBlockMetadata\Entity\IdentityProvider;
5-
use OpenConext\Component\EngineBlockMetadata\Entity\ServiceProvider;
64
use OpenConext\Component\EngineBlockMetadata\MetadataRepository\DoctrineMetadataRepository;
7-
use OpenConext\Component\EngineBlockMetadata\Service\JanusPushMetadataSynchronizer;
85

96
class Api_Controller_Connections extends EngineBlock_Controller_Abstract
107
{
118
public function indexAction()
129
{
1310
$this->setNoRender();
1411

15-
$configuration = EngineBlock_ApplicationSingleton::getInstance()->getConfigurationValue('engineApi');
16-
17-
if (!$configuration) {
18-
throw new EngineBlock_Exception('API access disabled');
19-
}
20-
21-
if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW'])) {
22-
header('WWW-Authenticate: Basic realm="EngineBlock API"');
23-
header('HTTP/1.1 401 Unauthorized');
24-
echo json_encode('Unauthenticated');
25-
exit;
26-
}
27-
28-
if ($_SERVER['PHP_AUTH_USER'] !== $configuration->user) {
29-
header('WWW-Authenticate: Basic realm="EngineBlock API"');
30-
header('HTTP/1.1 401 Unauthorized');
31-
echo json_encode('Invalid credentials');
32-
exit;
33-
}
34-
35-
if ($_SERVER['PHP_AUTH_PW'] !== $configuration->password) {
36-
header('WWW-Authenticate: Basic realm="EngineBlock API"');
37-
header('HTTP/1.1 401 Unauthorized');
38-
echo json_encode('Invalid credentials');
39-
exit;
40-
}
41-
42-
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
43-
header('HTTP/1.1 400 Bad Request');
44-
echo json_encode('Not a POST request');
12+
if (!$this->requireApiAuth()) {
4513
return;
4614
}
4715

@@ -58,15 +26,19 @@ public function indexAction()
5826
$connections = json_decode($body);
5927

6028
if (!$connections) {
61-
header('HTTP/1.1 400 Bad Request');
62-
echo json_encode('Unable to decode body as JSON');
63-
exit;
29+
$this->_getResponse()->setStatus(400, 'Bad Request');
30+
$this->_getResponse()->setBody(
31+
json_encode('Unable to decode body as JSON')
32+
);
33+
return;
6434
}
6535

6636
if (!is_object($connections) || !isset($connections->connections) && !is_object($connections->connections)) {
67-
header('HTTP/1.1 400 Bad Request');
68-
echo json_encode('Unrecognized structure for JSON');
69-
exit;
37+
$this->_getResponse()->setStatus(400, 'Bad Request');
38+
$this->_getResponse()->setBody(
39+
json_encode('Unrecognized structure for JSON')
40+
);
41+
return;
7042
}
7143

7244
$assembler = new JanusPushMetadataAssembler();
@@ -78,6 +50,57 @@ public function indexAction()
7850
);
7951
$result = $doctrineRepository->synchronize($roles);
8052

81-
echo json_encode($result);
53+
$this->_getResponse()->setBody(json_encode($result));
54+
}
55+
56+
public function testAction()
57+
{
58+
if (!$this->requireApiAuth()) {
59+
return;
60+
}
61+
}
62+
63+
private function requireApiAuth()
64+
{
65+
$configuration = EngineBlock_ApplicationSingleton::getInstance()->getConfigurationValue('engineApi');
66+
67+
if (!$configuration) {
68+
throw new EngineBlock_Exception('API access disabled');
69+
}
70+
71+
if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW'])) {
72+
$this->setNoRender();
73+
$this->_getResponse()->setHeader(
74+
'WWW-Authenticate',
75+
'Basic realm="EngineBlock API'
76+
);
77+
$this->_getResponse()->setStatus(401, 'Unauthorized');
78+
$this->_getResponse()->setBody(json_encode('Unauthenticated'));
79+
return false;
80+
}
81+
82+
if ($_SERVER['PHP_AUTH_USER'] !== $configuration->user) {
83+
$this->setNoRender();
84+
$this->_getResponse()->setHeader(
85+
'WWW-Authenticate',
86+
'Basic realm="EngineBlock API'
87+
);
88+
$this->_getResponse()->setStatus(401, 'Unauthorized');
89+
$this->_getResponse()->setBody(json_encode('Invalid credentials'));
90+
return false;
91+
}
92+
93+
if ($_SERVER['PHP_AUTH_PW'] !== $configuration->password) {
94+
$this->setNoRender();
95+
$this->_getResponse()->setHeader(
96+
'WWW-Authenticate',
97+
'Basic realm="EngineBlock API'
98+
);
99+
$this->_getResponse()->setStatus(401, 'Unauthorized');
100+
$this->_getResponse()->setBody(json_encode('Invalid credentials'));
101+
return false;
102+
}
103+
104+
return true;
82105
}
83106
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<div style="text-align: center; margin-bottom: 1rem">
2+
<button class="submit c-button" style="padding: 1em">PUSH</button>
3+
</div>
4+
5+
<label for="connections-json" style="font-size: large; font-weight: bolder; display: block">JSON:</label>
6+
<textarea id="connections-json" rows="50" cols="120"></textarea>
7+
8+
<div style="text-align: center; margin-top: 1rem">
9+
<button class="submit c-button" style="padding: 1em">PUSH</button>
10+
</div>
11+
12+
<script>
13+
window.setTimeout(
14+
function() {
15+
$('button').on('click', function() {
16+
$.post('index', $('textarea').val());
17+
});
18+
},
19+
3000
20+
);
21+
</script>

application/modules/Api/View/Index/Index.phtml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ $layout->footerText = $this->t('footer');
1515
<div class="main">
1616
<h1>Welcome to the EngineBlock API index page!</h1>
1717
<p>
18-
We currently only support 1 action, pushing metadata configuration.
19-
To do this POST a valid JSON document with 'connections' to /connections,
20-
using the engineApi.user and engineApi.password in HTTP Basic authentication.
18+
We currently only support 1 action, pushing metadata configuration. <br>
19+
To do this POST a valid JSON document with 'connections' to /connections, <br>
20+
using the engineApi.user and engineApi.password in HTTP Basic authentication. <br><br>
21+
22+
To test this go here: <a href="/api/connections/test">/api/connections/test</a>
2123
</p>
2224
</div>
2325
</div>

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
"monolog/monolog": "~1.13",
1818
"mrclay/minify": "~2.2",
1919
"openconext/engineblock-fixtures": "~0.4",
20-
"openconext/engineblock-metadata": "~1.0",
20+
"openconext/engineblock-metadata": "~1.2.8",
2121
"openconext/stoker-metadata": "~0.1",
2222
"openid/php-openid": "dev-master#a287b2d85e753c84b3b883ed8ee3ffe8692c8477 as 2.2.2",
2323
"pimple/pimple": "~2.1",
24-
"simplesamlphp/saml2": "~0.6",
24+
"simplesamlphp/saml2": "1.7.1 as 0.8.1",
2525
"simplesamlphp/simplesamlphp": "~1.13",
2626
"sybio/image-workshop": "~2.0.7",
2727
"zendframework/zendframework1":"~1.12"

0 commit comments

Comments
 (0)