11<?php
22
33use OpenConext \Component \EngineBlockMetadata \Entity \Assembler \JanusPushMetadataAssembler ;
4- use OpenConext \Component \EngineBlockMetadata \Entity \IdentityProvider ;
5- use OpenConext \Component \EngineBlockMetadata \Entity \ServiceProvider ;
64use OpenConext \Component \EngineBlockMetadata \MetadataRepository \DoctrineMetadataRepository ;
7- use OpenConext \Component \EngineBlockMetadata \Service \JanusPushMetadataSynchronizer ;
85
96class 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}
0 commit comments