Subversion Repositories oidplus

Rev

Rev 1270 | Rev 1273 | Go to most recent revision | 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 OIDplusPageRaRestApi
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 OIDplusPageAdminRestApi extends OIDplusPagePluginAdmin {
30
 
31
        /**
32
         * @param string $actionID
33
         * @param array $params
34
         * @return array
35
         * @throws OIDplusException
36
         */
37
        public function action(string $actionID, array $params): array {
38
                if ($actionID == 'blacklistJWT') {
39
                        if (!OIDplus::authUtils()->isAdminLoggedIn()) {
1266 daniel-mar 40
                                throw new OIDplusHtmlException(_L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login$admin')), null, 401);
1265 daniel-mar 41
                        }
42
 
43
                        if (!OIDplus::baseConfig()->getValue('JWT_ALLOW_REST_ADMIN', true)) {
44
                                throw new OIDplusException(_L('The administrator has disabled this feature. (Base configuration setting %1).','JWT_ALLOW_REST_ADMIN'));
45
                        }
46
 
47
                        $gen = OIDplusAuthContentStoreJWT::JWT_GENERATOR_REST;
48
                        $sub = 'admin';
49
 
50
                        OIDplusAuthContentStoreJWT::jwtBlacklist($gen, $sub);
51
 
52
                        return array("status" => 0);
53
                } else {
54
                        return parent::action($actionID, $params);
55
                }
56
        }
57
 
58
        /**
59
         * @param string $id
60
         * @param array $out
61
         * @param bool $handled
62
         * @return void
63
         * @throws OIDplusException
64
         */
65
        public function gui(string $id, array &$out, bool &$handled) {
1272 daniel-mar 66
                $tmp = explode('$',$id);
67
                $classname = $tmp[1] ?? '';
68
 
69
                $parts = explode('.',$tmp[0],2);
70
                if ($parts[0] != 'oidplus:rest_api_information_admin') return;
71
                $handled = true;
72
 
73
                if (str_starts_with($classname, 'endpoints:')) {
74
                        $plugin = OIDplus::getPluginByOid(explode(':',$classname)[1]);
75
                        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);
76
                        $out['title'] = _L('REST API').' - '.$plugin->getManifest()->getName() . ' ' . _L('Endpoints');
77
                        $out['icon'] = file_exists(__DIR__.'/img/endpoints_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
78
                        $out['text'] = '<p><a '.OIDplus::gui()->link('oidplus:rest_api_information_admin').'><img src="img/arrow_back.png" width="16" alt="'._L('Go back').'"> '._L('Go back').'</a></p>';
79
                        $out['text'] .= $plugin->restApiInfo('html');
80
                } else {
1265 daniel-mar 81
                        $out['title'] = _L('REST API');
82
                        $out['icon'] = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
83
 
84
                        if (!OIDplus::authUtils()->isAdminLoggedIn()) {
1266 daniel-mar 85
                                throw new OIDplusHtmlException(_L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login$admin')), $out['title'], 401);
1265 daniel-mar 86
                        }
87
 
88
                        if (!OIDplus::baseConfig()->getValue('JWT_ALLOW_REST_ADMIN', true)) {
89
                                throw new OIDplusException(_L('The administrator has disabled this feature. (Base configuration setting %1).','JWT_ALLOW_REST_ADMIN'), $out['title']);
90
                        }
91
 
1270 daniel-mar 92
                        if (is_null(OIDplus::getPluginByOid("1.3.6.1.4.1.37476.2.5.2.4.1.2"))) { // OIDplusPagePublicRestApi
93
                                throw new OIDplusException(_L('The administrator has disabled this feature. (Plugin %1).','OIDplusPagePublicRestApi'), $out['title']);
94
                        }
95
 
1265 daniel-mar 96
                        $gen = OIDplusAuthContentStoreJWT::JWT_GENERATOR_REST;
97
                        $sub = 'admin';
98
 
99
                        $authSimulation = new OIDplusAuthContentStoreJWT();
100
                        $authSimulation->adminLogin();
101
                        $authSimulation->setValue('oidplus_generator', $gen);
102
                        $token = $authSimulation->getJWTToken();
103
 
104
                        $out['text'] .= '<p>'._L('You can make automated calls to your OIDplus account by calling an REST API.').'</p>';
105
                        $out['text'] .= '<h2>'._L('Endpoints').'</h2>';
106
                        $endpoints = '';
107
                        foreach (OIDplus::getAllPlugins() as $plugin) {
108
                                if ($plugin instanceof INTF_OID_1_3_6_1_4_1_37476_2_5_2_3_9) {
1272 daniel-mar 109
                                        $link = 'oidplus:rest_api_information_admin$endpoints:'.$plugin->getManifest()->getOid();
110
                                        $endpoints .= '<li><a '.OIDplus::gui()->link($link).'>'.htmlentities($plugin->getManifest()->getName()).'</a></li>';
1265 daniel-mar 111
                                }
112
                        }
113
                        if ($endpoints) {
1272 daniel-mar 114
                                $out['text'] .= '<p>'._L('The following installed plugins are offering REST endpoints:').'</p>';
115
                                $out['text'] .= '<p><ul>'.$endpoints.'</ul></p>';
1265 daniel-mar 116
                        } else {
117
                                $out['text'] .= '<p>'._L('No installed plugin offers a REST functionality').'</p>';
118
                        }
119
                        $out['text'] .= '<h2>'._L('Authentication').'</h2>';
120
                        $out['text'] .= '<p>'._L('The authentication is done via the following HTTP header:').'</p>';
121
                        $out['text'] .= '<p><pre id="oidplus_auth_jwt">';
122
                        $out['text'] .= 'Authentication: Bearer '.htmlentities($token)."\n";
123
                        $out['text'] .= '</pre></p>';
124
                        $out['text'] .= '<p><input type="button" value="'._L('Copy to clipboard').'" onClick="copyToClipboard(oidplus_auth_jwt)"></p>';
125
                        $out['text'] .= '<p>'._L('Please keep this information confidential!').'</p>';
126
                        $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>';
127
 
128
                        $out['text'] .= '<h2>'._L('Blacklisted tokens').'</h2>';
129
                        $bl_time = OIDplusAuthContentStoreJWT::jwtGetBlacklistTime($gen, $sub);
130
                        if ($bl_time == 0) {
131
                                $out['text'] .= '<p>'._L('None of the previously generated JWT tokens have been blacklisted.').'</p>';
132
                        } else {
133
                                $out['text'] .= '<p>'._L('All tokens generated before %1 have been blacklisted.',date('d F Y, H:i:s',$bl_time+1)).'</p>';
134
                        }
135
                        $out['text'] .= '<button type="button" name="btn_blacklist_jwt" id="btn_blacklist_jwt" class="btn btn-danger btn-xs" onclick="OIDplusPageAdminRestApi.blacklistJWT()">'._L('Blacklist all previously generated tokens').'</button>';
136
                }
137
        }
138
 
139
        /**
140
         * @param array $json
141
         * @param string|null $ra_email
142
         * @param bool $nonjs
143
         * @param string $req_goto
144
         * @return bool
145
         * @throws OIDplusException
146
         */
147
        public function tree(array &$json, string $ra_email=null, bool $nonjs=false, string $req_goto=''): bool {
148
                if (!OIDplus::authUtils()->isAdminLoggedIn()) return false;
149
 
150
                if (file_exists(__DIR__.'/img/main_icon16.png')) {
151
                        $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png';
152
                } else {
153
                        $tree_icon = null; // default icon (folder)
154
                }
155
 
1272 daniel-mar 156
                if (file_exists(__DIR__.'/img/endpoints_icon16.png')) {
157
                        $tree_icon_endpoints = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/endpoints_icon16.png';
158
                } else {
159
                        $tree_icon_endpoints = null; // default icon (folder)
160
                }
161
 
162
                $submenu = array();
163
                foreach (OIDplus::getAllPlugins() as $plugin) {
164
                        if ($plugin instanceof INTF_OID_1_3_6_1_4_1_37476_2_5_2_3_9) {
165
                                $submenu[] = [
166
                                        'id' => 'oidplus:rest_api_information_admin$endpoints:'.$plugin->getManifest()->getOid(),
167
                                        'icon' => $tree_icon_endpoints,
168
                                        'text' => $plugin->getManifest()->getName() . ' ' . _L('Endpoints')
169
                                ];
170
                        }
171
                }
172
 
1265 daniel-mar 173
                $json[] = array(
174
                        'id' => 'oidplus:rest_api_information_admin',
175
                        'icon' => $tree_icon,
1272 daniel-mar 176
                        'text' => _L('REST API'),
177
                        'children' => $submenu
1265 daniel-mar 178
                );
179
 
180
                return true;
181
        }
182
 
183
        /**
184
         * @param string $request
185
         * @return array|false
186
         */
187
        public function tree_search(string $request) {
188
                return false;
189
        }
190
}