Rev 1305 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1265 | daniel-mar | 1 | <?php |
2 | |||
3 | /* |
||
4 | * OIDplus 2.0 |
||
5 | * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft |
||
6 | * |
||
7 | * Licensed under the Apache License, Version 2.0 (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 |
||
10 | * |
||
11 | * http://www.apache.org/licenses/LICENSE-2.0 |
||
12 | * |
||
13 | * Unless required by applicable law or agreed to in writing, software |
||
14 | * distributed under the License is distributed on an "AS IS" BASIS, |
||
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
16 | * See the License for the specific language governing permissions and |
||
17 | * limitations under the License. |
||
18 | */ |
||
19 | |||
20 | // ATTENTION: If you change something, please make sure that the changes |
||
21 | // are synchronous with OIDplusPageAdminRestApi |
||
22 | |||
23 | namespace ViaThinkSoft\OIDplus; |
||
24 | |||
25 | // phpcs:disable PSR1.Files.SideEffects |
||
26 | \defined('INSIDE_OIDPLUS') or die; |
||
27 | // phpcs:enable PSR1.Files.SideEffects |
||
28 | |||
29 | class OIDplusPageRaRestApi extends OIDplusPagePluginRa { |
||
30 | |||
31 | /** |
||
32 | * @param array $params |
||
33 | * @return array |
||
34 | * @throws OIDplusException |
||
35 | */ |
||
1293 | daniel-mar | 36 | private function action_Blacklist(array $params): array { |
37 | if (!OIDplus::baseConfig()->getValue('JWT_ALLOW_REST_USER', true)) { |
||
38 | throw new OIDplusException(_L('The administrator has disabled this feature. (Base configuration setting %1).','JWT_ALLOW_REST_USER')); |
||
39 | } |
||
1265 | daniel-mar | 40 | |
1293 | daniel-mar | 41 | _CheckParamExists($params, 'user'); |
42 | $ra_email = $params['user']; |
||
1265 | daniel-mar | 43 | |
1293 | daniel-mar | 44 | if (!OIDplus::authUtils()->isRaLoggedIn($ra_email) && !OIDplus::authUtils()->isAdminLoggedIn()) { |
45 | throw new OIDplusHtmlException(_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>'), null, 401); |
||
46 | } |
||
1265 | daniel-mar | 47 | |
1293 | daniel-mar | 48 | $gen = OIDplusAuthContentStoreJWT::JWT_GENERATOR_REST; |
49 | $sub = $ra_email; |
||
1265 | daniel-mar | 50 | |
1293 | daniel-mar | 51 | OIDplusAuthContentStoreJWT::jwtBlacklist($gen, $sub); |
1265 | daniel-mar | 52 | |
1293 | daniel-mar | 53 | return array("status" => 0); |
54 | } |
||
55 | |||
56 | /** |
||
57 | * @param string $actionID |
||
58 | * @param array $params |
||
59 | * @return array |
||
60 | * @throws OIDplusException |
||
61 | */ |
||
62 | public function action(string $actionID, array $params): array { |
||
63 | if ($actionID == 'blacklistJWT') { |
||
64 | return $this->action_Blacklist($params); |
||
1265 | daniel-mar | 65 | } else { |
66 | return parent::action($actionID, $params); |
||
67 | } |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * @param string $id |
||
72 | * @param array $out |
||
73 | * @param bool $handled |
||
74 | * @return void |
||
75 | * @throws OIDplusException |
||
76 | */ |
||
77 | public function gui(string $id, array &$out, bool &$handled) { |
||
1278 | daniel-mar | 78 | $parts = explode('$',$id,3); |
79 | $ra_email = $parts[1] ?? ''; |
||
80 | $subpage = $parts[2] ?? ''; |
||
1265 | daniel-mar | 81 | |
1278 | daniel-mar | 82 | if (empty($ra_email)) return; |
1272 | daniel-mar | 83 | |
1278 | daniel-mar | 84 | if ($parts[0] == 'oidplus:rest_api_information_ra') { |
85 | $handled = true; |
||
1265 | daniel-mar | 86 | |
1278 | daniel-mar | 87 | if (str_starts_with($subpage, 'endpoints:')) { |
88 | // Note: This page can be accessed WITHOUT login! |
||
89 | $plugin = OIDplus::getPluginByOid(explode(':',$subpage)[1]); |
||
90 | if (!$plugin || !($plugin instanceof INTF_OID_1_3_6_1_4_1_37476_2_5_2_3_9)) throw new OIDplusException(_L("No endpoints for this plugin found"), null, 404); |
||
91 | $out['title'] = _L('REST API').' - '.$plugin->getManifest()->getName() . ' ' . _L('Endpoints'); |
||
92 | $out['icon'] = file_exists(__DIR__.'/img/endpoints_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/endpoints_icon.png' : ''; |
||
93 | $out['text'] = ''; |
||
1297 | daniel-mar | 94 | if (OIDplus::authUtils()->isRaLoggedIn($ra_email) || OIDplus::authUtils()->isAdminLoggedIn()) { |
95 | $out['text'] .= '<p><a '.OIDplus::gui()->link('oidplus:rest_api_information_ra$'.$ra_email).'><img src="img/arrow_back.png" width="16" alt="'._L('Go back').'"> '._L('Go back').'</a></p>'; |
||
1278 | daniel-mar | 96 | } |
97 | $out['text'] .= $plugin->restApiInfo('html'); |
||
98 | } else { |
||
99 | $out['title'] = _L('REST API'); |
||
100 | $out['icon'] = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : ''; |
||
1265 | daniel-mar | 101 | |
1278 | daniel-mar | 102 | if (!OIDplus::authUtils()->isRaLoggedIn($ra_email) && !OIDplus::authUtils()->isAdminLoggedIn()) { |
103 | throw new OIDplusHtmlException(_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>'), $out['title'], 401); |
||
104 | } |
||
1265 | daniel-mar | 105 | |
1278 | daniel-mar | 106 | if (!OIDplus::baseConfig()->getValue('JWT_ALLOW_REST_USER', true)) { |
107 | throw new OIDplusException(_L('The administrator has disabled this feature. (Base configuration setting %1).','JWT_ALLOW_REST_USER'), $out['title']); |
||
108 | } |
||
1265 | daniel-mar | 109 | |
1278 | daniel-mar | 110 | if (is_null(OIDplus::getPluginByOid("1.3.6.1.4.1.37476.2.5.2.4.1.2"))) { // OIDplusPagePublicRestApi |
111 | throw new OIDplusException(_L('The administrator has disabled this feature. (Plugin %1).','OIDplusPagePublicRestApi'), $out['title']); |
||
112 | } |
||
1270 | daniel-mar | 113 | |
1278 | daniel-mar | 114 | $gen = OIDplusAuthContentStoreJWT::JWT_GENERATOR_REST; |
115 | $sub = $ra_email; |
||
1265 | daniel-mar | 116 | |
1314 | daniel-mar | 117 | $token = OIDplusAuthContentStoreJWT::craftJWT([$sub], false, $gen); |
1265 | daniel-mar | 118 | |
1278 | daniel-mar | 119 | $out['text'] .= '<p>'._L('You can make automated calls to your OIDplus account by calling an REST API.').'</p>'; |
120 | $out['text'] .= '<h2>'._L('Endpoints').'</h2>'; |
||
121 | $endpoints = ''; |
||
122 | foreach (OIDplus::getAllPlugins() as $plugin) { |
||
123 | if ($plugin instanceof INTF_OID_1_3_6_1_4_1_37476_2_5_2_3_9) { |
||
124 | $link = 'oidplus:rest_api_information_ra$'.$ra_email.'$endpoints:'.$plugin->getManifest()->getOid(); |
||
125 | $endpoints .= '<li><a '.OIDplus::gui()->link($link).'>'.htmlentities($plugin->getManifest()->getName()).'</a></li>'; |
||
126 | } |
||
1265 | daniel-mar | 127 | } |
1278 | daniel-mar | 128 | if ($endpoints) { |
129 | $out['text'] .= '<p>'._L('The following installed plugins are offering REST endpoints:').'</p>'; |
||
130 | $out['text'] .= '<p><ul>'.$endpoints.'</ul></p>'; |
||
131 | } else { |
||
132 | $out['text'] .= '<p>'._L('No installed plugin offers a REST functionality').'</p>'; |
||
133 | } |
||
134 | $out['text'] .= '<h2>'._L('Authentication').'</h2>'; |
||
135 | $out['text'] .= '<p>'._L('The authentication is done via the following HTTP header:').'</p>'; |
||
136 | $out['text'] .= '<p><pre id="oidplus_auth_jwt">'; |
||
137 | $out['text'] .= 'Authentication: Bearer '.htmlentities($token)."\n"; |
||
138 | $out['text'] .= '</pre></p>'; |
||
139 | $out['text'] .= '<p><input type="button" value="'._L('Copy to clipboard').'" onClick="copyToClipboard(oidplus_auth_jwt)"></p>'; |
||
140 | $out['text'] .= '<p>'._L('Please keep this information confidential!').'</p>'; |
||
1265 | daniel-mar | 141 | |
1278 | daniel-mar | 142 | $out['text'] .= '<h2>'._L('Blacklisted tokens').'</h2>'; |
143 | $bl_time = OIDplusAuthContentStoreJWT::jwtGetBlacklistTime($gen, $sub); |
||
144 | if ($bl_time == 0) { |
||
145 | $out['text'] .= '<p>'._L('None of the previously generated JWT tokens have been blacklisted.').'</p>'; |
||
146 | } else { |
||
147 | $out['text'] .= '<p>'._L('All tokens generated before %1 have been blacklisted.',date('d F Y, H:i:s',$bl_time+1)).'</p>'; |
||
148 | } |
||
149 | $out['text'] .= '<button type="button" name="btn_blacklist_jwt" id="btn_blacklist_jwt" class="btn btn-danger btn-xs" onclick="OIDplusPageRaRestApi.blacklistJWT('.js_escape($ra_email).')">'._L('Blacklist all previously generated tokens').'</button>'; |
||
1265 | daniel-mar | 150 | } |
151 | } |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * @param array $json |
||
156 | * @param string|null $ra_email |
||
157 | * @param bool $nonjs |
||
158 | * @param string $req_goto |
||
159 | * @return bool |
||
160 | * @throws OIDplusException |
||
161 | */ |
||
162 | public function tree(array &$json, string $ra_email=null, bool $nonjs=false, string $req_goto=''): bool { |
||
163 | if (!$ra_email) return false; |
||
164 | if (!OIDplus::authUtils()->isRaLoggedIn($ra_email) && !OIDplus::authUtils()->isAdminLoggedIn()) return false; |
||
165 | |||
166 | if (file_exists(__DIR__.'/img/main_icon16.png')) { |
||
167 | $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png'; |
||
168 | } else { |
||
169 | $tree_icon = null; // default icon (folder) |
||
170 | } |
||
171 | |||
1272 | daniel-mar | 172 | if (file_exists(__DIR__.'/img/endpoints_icon16.png')) { |
173 | $tree_icon_endpoints = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/endpoints_icon16.png'; |
||
174 | } else { |
||
175 | $tree_icon_endpoints = null; // default icon (folder) |
||
176 | } |
||
177 | |||
178 | $submenu = array(); |
||
179 | foreach (OIDplus::getAllPlugins() as $plugin) { |
||
180 | if ($plugin instanceof INTF_OID_1_3_6_1_4_1_37476_2_5_2_3_9) { |
||
181 | $submenu[] = [ |
||
1278 | daniel-mar | 182 | 'id' => 'oidplus:rest_api_information_ra$'.$ra_email.'$endpoints:'.$plugin->getManifest()->getOid(), |
1272 | daniel-mar | 183 | 'icon' => $tree_icon_endpoints, |
184 | 'text' => $plugin->getManifest()->getName() . ' ' . _L('Endpoints') |
||
185 | ]; |
||
186 | } |
||
187 | } |
||
188 | |||
1265 | daniel-mar | 189 | $json[] = array( |
1278 | daniel-mar | 190 | 'id' => 'oidplus:rest_api_information_ra$'.$ra_email, |
1265 | daniel-mar | 191 | 'icon' => $tree_icon, |
1272 | daniel-mar | 192 | 'text' => _L('REST API'), |
193 | 'children' => $submenu |
||
1265 | daniel-mar | 194 | ); |
195 | |||
196 | return true; |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * @param string $request |
||
201 | * @return array|false |
||
202 | */ |
||
203 | public function tree_search(string $request) { |
||
204 | return false; |
||
205 | } |
||
206 | } |