Rev 1116 | Rev 1201 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1116 | Rev 1143 | ||
---|---|---|---|
1 | <?php |
1 | <?php |
2 | 2 | ||
3 | /* |
3 | /* |
4 | * OIDplus 2.0 |
4 | * OIDplus 2.0 |
5 | * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft |
5 | * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft |
6 | * |
6 | * |
7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | * you may not use this file except in compliance with the License. |
8 | * you may not use this file except in compliance with the License. |
9 | * You may obtain a copy of the License at |
9 | * You may obtain a copy of the License at |
10 | * |
10 | * |
11 | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | * http://www.apache.org/licenses/LICENSE-2.0 |
12 | * |
12 | * |
13 | * Unless required by applicable law or agreed to in writing, software |
13 | * Unless required by applicable law or agreed to in writing, software |
14 | * distributed under the License is distributed on an "AS IS" BASIS, |
14 | * distributed under the License is distributed on an "AS IS" BASIS, |
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | * See the License for the specific language governing permissions and |
16 | * See the License for the specific language governing permissions and |
17 | * limitations under the License. |
17 | * limitations under the License. |
18 | */ |
18 | */ |
19 | 19 | ||
20 | // ATTENTION: If you change something, please make sure that the changes |
20 | // ATTENTION: If you change something, please make sure that the changes |
21 | // are synchronous with OIDplusPageAdminAutomatedAJAXCalls |
21 | // are synchronous with OIDplusPageAdminAutomatedAJAXCalls |
22 | 22 | ||
23 | namespace ViaThinkSoft\OIDplus; |
23 | namespace ViaThinkSoft\OIDplus; |
24 | 24 | ||
25 | // phpcs:disable PSR1.Files.SideEffects |
25 | // phpcs:disable PSR1.Files.SideEffects |
26 | \defined('INSIDE_OIDPLUS') or die; |
26 | \defined('INSIDE_OIDPLUS') or die; |
27 | // phpcs:enable PSR1.Files.SideEffects |
27 | // phpcs:enable PSR1.Files.SideEffects |
28 | 28 | ||
29 | class OIDplusPageRaAutomatedAJAXCalls extends OIDplusPagePluginRa { |
29 | class OIDplusPageRaAutomatedAJAXCalls extends OIDplusPagePluginRa { |
30 | 30 | ||
31 | /** |
31 | /** |
32 | * @param string $actionID |
32 | * @param string $actionID |
33 | * @param array $params |
33 | * @param array $params |
34 | * @return int[] |
34 | * @return array |
35 | * @throws OIDplusException |
35 | * @throws OIDplusException |
36 | */ |
36 | */ |
37 | public function action(string $actionID, array $params): array { |
37 | public function action(string $actionID, array $params): array { |
38 | if ($actionID == 'blacklistJWT') { |
38 | if ($actionID == 'blacklistJWT') { |
39 | if (!OIDplus::baseConfig()->getValue('JWT_ALLOW_AJAX_USER', true)) { |
39 | if (!OIDplus::baseConfig()->getValue('JWT_ALLOW_AJAX_USER', true)) { |
40 | throw new OIDplusException(_L('The administrator has disabled this feature. (Base configuration setting %1).','JWT_ALLOW_AJAX_USER')); |
40 | throw new OIDplusException(_L('The administrator has disabled this feature. (Base configuration setting %1).','JWT_ALLOW_AJAX_USER')); |
41 | } |
41 | } |
42 | 42 | ||
43 | _CheckParamExists($params, 'user'); |
43 | _CheckParamExists($params, 'user'); |
44 | $ra_email = $params['user']; |
44 | $ra_email = $params['user']; |
45 | 45 | ||
46 | if (!OIDplus::authUtils()->isRaLoggedIn($ra_email) && !OIDplus::authUtils()->isAdminLoggedIn()) { |
46 | if (!OIDplus::authUtils()->isRaLoggedIn($ra_email) && !OIDplus::authUtils()->isAdminLoggedIn()) { |
47 | throw new OIDplusException(_L('You need to <a %1>log in</a> as the requested RA %2 or as admin.',OIDplus::gui()->link('oidplus:login$ra$'.$ra_email),'<b>'.htmlentities($ra_email).'</b>')); |
47 | throw new OIDplusException(_L('You need to <a %1>log in</a> as the requested RA %2 or as admin.',OIDplus::gui()->link('oidplus:login$ra$'.$ra_email),'<b>'.htmlentities($ra_email).'</b>')); |
48 | } |
48 | } |
49 | 49 | ||
50 | $gen = OIDplusAuthContentStoreJWT::JWT_GENERATOR_AJAX; |
50 | $gen = OIDplusAuthContentStoreJWT::JWT_GENERATOR_AJAX; |
51 | $sub = $ra_email; |
51 | $sub = $ra_email; |
52 | 52 | ||
53 | OIDplusAuthContentStoreJWT::jwtBlacklist($gen, $sub); |
53 | OIDplusAuthContentStoreJWT::jwtBlacklist($gen, $sub); |
54 | 54 | ||
55 | return array("status" => 0); |
55 | return array("status" => 0); |
56 | } else { |
56 | } else { |
57 | return parent::action($actionID, $params); |
57 | return parent::action($actionID, $params); |
58 | } |
58 | } |
59 | } |
59 | } |
60 | 60 | ||
61 | /** |
61 | /** |
62 | * @param string $id |
62 | * @param string $id |
63 | * @param array $out |
63 | * @param array $out |
64 | * @param bool $handled |
64 | * @param bool $handled |
65 | * @return void |
65 | * @return void |
66 | * @throws OIDplusException |
66 | * @throws OIDplusException |
67 | */ |
67 | */ |
68 | public function gui(string $id, array &$out, bool &$handled) { |
68 | public function gui(string $id, array &$out, bool &$handled) { |
69 | if (explode('$',$id)[0] == 'oidplus:automated_ajax_information_ra') { |
69 | if (explode('$',$id)[0] == 'oidplus:automated_ajax_information_ra') { |
70 | $handled = true; |
70 | $handled = true; |
71 | 71 | ||
72 | $ra_email = explode('$',$id)[1]; |
72 | $ra_email = explode('$',$id)[1]; |
73 | 73 | ||
74 | $out['title'] = _L('Automated AJAX calls'); |
74 | $out['title'] = _L('Automated AJAX calls'); |
75 | $out['icon'] = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : ''; |
75 | $out['icon'] = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : ''; |
76 | 76 | ||
77 | if (!OIDplus::authUtils()->isRaLoggedIn($ra_email) && !OIDplus::authUtils()->isAdminLoggedIn()) { |
77 | if (!OIDplus::authUtils()->isRaLoggedIn($ra_email) && !OIDplus::authUtils()->isAdminLoggedIn()) { |
78 | $out['icon'] = 'img/error.png'; |
78 | $out['icon'] = 'img/error.png'; |
79 | $out['text'] = '<p>'._L('You need to <a %1>log in</a> as the requested RA %2 or as admin.',OIDplus::gui()->link('oidplus:login$ra$'.$ra_email),'<b>'.htmlentities($ra_email).'</b>').'</p>'; |
79 | $out['text'] = '<p>'._L('You need to <a %1>log in</a> as the requested RA %2 or as admin.',OIDplus::gui()->link('oidplus:login$ra$'.$ra_email),'<b>'.htmlentities($ra_email).'</b>').'</p>'; |
80 | return; |
80 | return; |
81 | } |
81 | } |
82 | 82 | ||
83 | if (!OIDplus::baseConfig()->getValue('JWT_ALLOW_AJAX_USER', true)) { |
83 | if (!OIDplus::baseConfig()->getValue('JWT_ALLOW_AJAX_USER', true)) { |
84 | $out['text'] = '<p>'._L('The administrator has disabled this feature. (Base configuration setting %1).','JWT_ALLOW_AJAX_USER').'</p>'; |
84 | $out['text'] = '<p>'._L('The administrator has disabled this feature. (Base configuration setting %1).','JWT_ALLOW_AJAX_USER').'</p>'; |
85 | return; |
85 | return; |
86 | } |
86 | } |
87 | 87 | ||
88 | $gen = OIDplusAuthContentStoreJWT::JWT_GENERATOR_AJAX; |
88 | $gen = OIDplusAuthContentStoreJWT::JWT_GENERATOR_AJAX; |
89 | $sub = $ra_email; |
89 | $sub = $ra_email; |
90 | 90 | ||
91 | $authSimulation = new OIDplusAuthContentStoreJWT(); |
91 | $authSimulation = new OIDplusAuthContentStoreJWT(); |
92 | $authSimulation->raLogin($ra_email); |
92 | $authSimulation->raLogin($ra_email); |
93 | $authSimulation->setValue('oidplus_generator', $gen); |
93 | $authSimulation->setValue('oidplus_generator', $gen); |
94 | $token = $authSimulation->getJWTToken(); |
94 | $token = $authSimulation->getJWTToken(); |
95 | 95 | ||
96 | $out['text'] .= '<p>'._L('You can make automated calls to your OIDplus account by calling the AJAX API.').'</p>'; |
96 | $out['text'] .= '<p>'._L('You can make automated calls to your OIDplus account by calling the AJAX API.').'</p>'; |
97 | $out['text'] .= '<p>'._L('The URL for the AJAX script is:').'</p>'; |
97 | $out['text'] .= '<p>'._L('The URL for the AJAX script is:').'</p>'; |
98 | $out['text'] .= '<p><b>'.OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL).'ajax.php</b></p>'; |
98 | $out['text'] .= '<p><b>'.OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL).'ajax.php</b></p>'; |
99 | $out['text'] .= '<p>'._L('You must at least provide following fields:').'</p>'; |
99 | $out['text'] .= '<p>'._L('You must at least provide following fields:').'</p>'; |
100 | $out['text'] .= '<p><pre>'; |
100 | $out['text'] .= '<p><pre>'; |
101 | $out['text'] .= 'OIDPLUS_AUTH_JWT = "'.htmlentities($token).'"'."\n"; |
101 | $out['text'] .= 'OIDPLUS_AUTH_JWT = "'.htmlentities($token).'"'."\n"; |
102 | $out['text'] .= '</pre></p>'; |
102 | $out['text'] .= '</pre></p>'; |
103 | $out['text'] .= '<p><input type="button" value="'._L('Copy to clipboard').'" onClick="copyToClipboard(oidplus_auth_jwt)"></p>'; |
103 | $out['text'] .= '<p><input type="button" value="'._L('Copy to clipboard').'" onClick="copyToClipboard(oidplus_auth_jwt)"></p>'; |
104 | $out['text'] .= '<p>'._L('Please keep this information confidential!').'</p>'; |
104 | $out['text'] .= '<p>'._L('Please keep this information confidential!').'</p>'; |
105 | $out['text'] .= '<p>'._L('The JWT-token (secret!) will automatically perform a one-time-login to fulfill the request. The other fields are the normal fields which are called during the usual operation of OIDplus.').'</p>'; |
105 | $out['text'] .= '<p>'._L('The JWT-token (secret!) will automatically perform a one-time-login to fulfill the request. The other fields are the normal fields which are called during the usual operation of OIDplus.').'</p>'; |
106 | $out['text'] .= '<p>'._L('Currently, there is no documentation for the AJAX calls. However, you can look at the <b>script.js</b> files of the plugins to see the field names being used. You can also enable network analysis in your web browser debugger (F12) to see the request headers sent to the server during the operation of OIDplus.').'</p>'; |
106 | $out['text'] .= '<p>'._L('Currently, there is no documentation for the AJAX calls. However, you can look at the <b>script.js</b> files of the plugins to see the field names being used. You can also enable network analysis in your web browser debugger (F12) to see the request headers sent to the server during the operation of OIDplus.').'</p>'; |
107 | 107 | ||
108 | $out['text'] .= '<h2>'._L('Blacklisted tokens').'</h2>'; |
108 | $out['text'] .= '<h2>'._L('Blacklisted tokens').'</h2>'; |
109 | $bl_time = OIDplusAuthContentStoreJWT::jwtGetBlacklistTime($gen, $sub); |
109 | $bl_time = OIDplusAuthContentStoreJWT::jwtGetBlacklistTime($gen, $sub); |
110 | if ($bl_time == 0) { |
110 | if ($bl_time == 0) { |
111 | $out['text'] .= '<p>'._L('None of the previously generated JWT tokens have been blacklisted.').'</p>'; |
111 | $out['text'] .= '<p>'._L('None of the previously generated JWT tokens have been blacklisted.').'</p>'; |
112 | } else { |
112 | } else { |
113 | $out['text'] .= '<p>'._L('All tokens generated before %1 have been blacklisted.',date('d F Y, H:i:s',$bl_time+1)).'</p>'; |
113 | $out['text'] .= '<p>'._L('All tokens generated before %1 have been blacklisted.',date('d F Y, H:i:s',$bl_time+1)).'</p>'; |
114 | } |
114 | } |
115 | $out['text'] .= '<button type="button" name="btn_blacklist_jwt" id="btn_blacklist_jwt" class="btn btn-danger btn-xs" onclick="OIDplusPageRaAutomatedAJAXCalls.blacklistJWT('.js_escape($ra_email).')">'._L('Blacklist all previously generated tokens').'</button>'; |
115 | $out['text'] .= '<button type="button" name="btn_blacklist_jwt" id="btn_blacklist_jwt" class="btn btn-danger btn-xs" onclick="OIDplusPageRaAutomatedAJAXCalls.blacklistJWT('.js_escape($ra_email).')">'._L('Blacklist all previously generated tokens').'</button>'; |
116 | 116 | ||
117 | $out['text'] .= '<h2>'._L('Example for adding OID 2.999.123 using JavaScript').'</h2>'; |
117 | $out['text'] .= '<h2>'._L('Example for adding OID 2.999.123 using JavaScript').'</h2>'; |
118 | $cont = file_get_contents(__DIR__.'/examples/example_js.html'); |
118 | $cont = file_get_contents(__DIR__.'/examples/example_js.html'); |
119 | $cont = str_replace('<url>', OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL).'ajax.php', $cont); |
119 | $cont = str_replace('<url>', OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL).'ajax.php', $cont); |
120 | $cont = str_replace('<token>', $token, $cont); |
120 | $cont = str_replace('<token>', $token, $cont); |
121 | $out['text'] .= '<pre id="example_js">'.htmlentities($cont).'</pre>'; |
121 | $out['text'] .= '<pre id="example_js">'.htmlentities($cont).'</pre>'; |
122 | $out['text'] .= '<p><input type="button" value="'._L('Copy to clipboard').'" onClick="copyToClipboard(example_js)"></p>'; |
122 | $out['text'] .= '<p><input type="button" value="'._L('Copy to clipboard').'" onClick="copyToClipboard(example_js)"></p>'; |
123 | 123 | ||
124 | $out['text'] .= '<h2>'._L('Example for adding OID 2.999.123 using PHP (located at a foreign server)').'</h2>'; |
124 | $out['text'] .= '<h2>'._L('Example for adding OID 2.999.123 using PHP (located at a foreign server)').'</h2>'; |
125 | $cont = file_get_contents(__DIR__.'/examples/example_php.phps'); |
125 | $cont = file_get_contents(__DIR__.'/examples/example_php.phps'); |
126 | $cont = str_replace('<url>', OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL).'ajax.php', $cont); |
126 | $cont = str_replace('<url>', OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL).'ajax.php', $cont); |
127 | $cont = str_replace('<token>', $token, $cont); |
127 | $cont = str_replace('<token>', $token, $cont); |
128 | $out['text'] .= '<pre id="example_php">'.preg_replace("@<br.*>@ismU","",highlight_string($cont,true)).'</pre>'; |
128 | $out['text'] .= '<pre id="example_php">'.preg_replace("@<br.*>@ismU","",highlight_string($cont,true)).'</pre>'; |
129 | $out['text'] .= '<p><input type="button" value="'._L('Copy to clipboard').'" onClick="copyToClipboard(example_php)"></p>'; |
129 | $out['text'] .= '<p><input type="button" value="'._L('Copy to clipboard').'" onClick="copyToClipboard(example_php)"></p>'; |
130 | 130 | ||
131 | $out['text'] .= '<h2>'._L('Example for adding OID 2.999.123 using Python').'</h2>'; |
131 | $out['text'] .= '<h2>'._L('Example for adding OID 2.999.123 using Python').'</h2>'; |
132 | $cont = file_get_contents(__DIR__.'/examples/example_python.py'); |
132 | $cont = file_get_contents(__DIR__.'/examples/example_python.py'); |
133 | $cont = str_replace('<url>', OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL).'ajax.php', $cont); |
133 | $cont = str_replace('<url>', OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL).'ajax.php', $cont); |
134 | $cont = str_replace('<token>', $token, $cont); |
134 | $cont = str_replace('<token>', $token, $cont); |
135 | $out['text'] .= '<pre id="example_python">'.htmlentities($cont).'</pre>'; |
135 | $out['text'] .= '<pre id="example_python">'.htmlentities($cont).'</pre>'; |
136 | $out['text'] .= '<p><input type="button" value="'._L('Copy to clipboard').'" onClick="copyToClipboard(example_python)"></p>'; |
136 | $out['text'] .= '<p><input type="button" value="'._L('Copy to clipboard').'" onClick="copyToClipboard(example_python)"></p>'; |
137 | 137 | ||
138 | $out['text'] .= '<h2>'._L('Example for adding OID 2.999.123 using VBScript').'</h2>'; |
138 | $out['text'] .= '<h2>'._L('Example for adding OID 2.999.123 using VBScript').'</h2>'; |
139 | $cont = file_get_contents(__DIR__.'/examples/example_vbs.vbs'); |
139 | $cont = file_get_contents(__DIR__.'/examples/example_vbs.vbs'); |
140 | $cont = str_replace('<url>', OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL).'ajax.php', $cont); |
140 | $cont = str_replace('<url>', OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL).'ajax.php', $cont); |
141 | $cont = str_replace('<token>', $token, $cont); |
141 | $cont = str_replace('<token>', $token, $cont); |
142 | $out['text'] .= '<pre id="example_vbs">'.htmlentities($cont).'</pre>'; |
142 | $out['text'] .= '<pre id="example_vbs">'.htmlentities($cont).'</pre>'; |
143 | $out['text'] .= '<p><input type="button" value="'._L('Copy to clipboard').'" onClick="copyToClipboard(example_vbs)"></p>'; |
143 | $out['text'] .= '<p><input type="button" value="'._L('Copy to clipboard').'" onClick="copyToClipboard(example_vbs)"></p>'; |
144 | } |
144 | } |
145 | } |
145 | } |
146 | 146 | ||
147 | /** |
147 | /** |
148 | * @param array $json |
148 | * @param array $json |
149 | * @param string|null $ra_email |
149 | * @param string|null $ra_email |
150 | * @param bool $nonjs |
150 | * @param bool $nonjs |
151 | * @param string $req_goto |
151 | * @param string $req_goto |
152 | * @return bool |
152 | * @return bool |
153 | * @throws OIDplusException |
153 | * @throws OIDplusException |
154 | */ |
154 | */ |
155 | public function tree(array &$json, string $ra_email=null, bool $nonjs=false, string $req_goto=''): bool { |
155 | public function tree(array &$json, string $ra_email=null, bool $nonjs=false, string $req_goto=''): bool { |
156 | if (!$ra_email) return false; |
156 | if (!$ra_email) return false; |
157 | if (!OIDplus::authUtils()->isRaLoggedIn($ra_email) && !OIDplus::authUtils()->isAdminLoggedIn()) return false; |
157 | if (!OIDplus::authUtils()->isRaLoggedIn($ra_email) && !OIDplus::authUtils()->isAdminLoggedIn()) return false; |
158 | 158 | ||
159 | if (file_exists(__DIR__.'/img/main_icon16.png')) { |
159 | if (file_exists(__DIR__.'/img/main_icon16.png')) { |
160 | $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png'; |
160 | $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png'; |
161 | } else { |
161 | } else { |
162 | $tree_icon = null; // default icon (folder) |
162 | $tree_icon = null; // default icon (folder) |
163 | } |
163 | } |
164 | 164 | ||
165 | $json[] = array( |
165 | $json[] = array( |
166 | 'id' => 'oidplus:automated_ajax_information_ra$'.$ra_email, |
166 | 'id' => 'oidplus:automated_ajax_information_ra$'.$ra_email, |
167 | 'icon' => $tree_icon, |
167 | 'icon' => $tree_icon, |
168 | 'text' => _L('Automated AJAX calls') |
168 | 'text' => _L('Automated AJAX calls') |
169 | ); |
169 | ); |
170 | 170 | ||
171 | return true; |
171 | return true; |
172 | } |
172 | } |
173 | 173 | ||
174 | /** |
174 | /** |
175 | * @param string $request |
175 | * @param string $request |
176 | * @return array|false |
176 | * @return array|false |
177 | */ |
177 | */ |
178 | public function tree_search(string $request) { |
178 | public function tree_search(string $request) { |
179 | return false; |
179 | return false; |
180 | } |
180 | } |
181 | } |
181 | } |
182 | 182 |