Subversion Repositories oidplus

Rev

Rev 1265 | Rev 1270 | 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 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 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::baseConfig()->getValue('JWT_ALLOW_REST_USER', true)) {
40
                                throw new OIDplusException(_L('The administrator has disabled this feature. (Base configuration setting %1).','JWT_ALLOW_REST_USER'));
41
                        }
42
 
43
                        _CheckParamExists($params, 'user');
44
                        $ra_email = $params['user'];
45
 
46
                        if (!OIDplus::authUtils()->isRaLoggedIn($ra_email) && !OIDplus::authUtils()->isAdminLoggedIn()) {
1266 daniel-mar 47
                                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);
1265 daniel-mar 48
                        }
49
 
50
                        $gen = OIDplusAuthContentStoreJWT::JWT_GENERATOR_REST;
51
                        $sub = $ra_email;
52
 
53
                        OIDplusAuthContentStoreJWT::jwtBlacklist($gen, $sub);
54
 
55
                        return array("status" => 0);
56
                } else {
57
                        return parent::action($actionID, $params);
58
                }
59
        }
60
 
61
        /**
62
         * @param string $id
63
         * @param array $out
64
         * @param bool $handled
65
         * @return void
66
         * @throws OIDplusException
67
         */
68
        public function gui(string $id, array &$out, bool &$handled) {
69
                if (explode('$',$id)[0] == 'oidplus:rest_api_information_ra') {
70
                        $handled = true;
71
 
72
                        $ra_email = explode('$',$id)[1];
73
 
74
                        $out['title'] = _L('REST API');
75
                        $out['icon'] = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
76
 
77
                        if (!OIDplus::authUtils()->isRaLoggedIn($ra_email) && !OIDplus::authUtils()->isAdminLoggedIn()) {
1266 daniel-mar 78
                                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);
1265 daniel-mar 79
                        }
80
 
81
                        if (!OIDplus::baseConfig()->getValue('JWT_ALLOW_REST_USER', true)) {
82
                                throw new OIDplusException(_L('The administrator has disabled this feature. (Base configuration setting %1).','JWT_ALLOW_REST_USER'), $out['title']);
83
                        }
84
 
85
                        $gen = OIDplusAuthContentStoreJWT::JWT_GENERATOR_REST;
86
                        $sub = $ra_email;
87
 
88
                        $authSimulation = new OIDplusAuthContentStoreJWT();
89
                        $authSimulation->raLogin($ra_email);
90
                        $authSimulation->setValue('oidplus_generator', $gen);
91
                        $token = $authSimulation->getJWTToken();
92
 
93
                        $out['text'] .= '<p>'._L('You can make automated calls to your OIDplus account by calling an REST API.').'</p>';
94
                        $out['text'] .= '<h2>'._L('Endpoints').'</h2>';
95
                        $endpoints = '';
96
                        foreach (OIDplus::getAllPlugins() as $plugin) {
97
                                if ($plugin instanceof INTF_OID_1_3_6_1_4_1_37476_2_5_2_3_9) {
98
                                        $endpoints .= $plugin->restApiInfo('html');
99
                                }
100
                        }
101
                        if ($endpoints) {
102
                                $out['text'] .= '<p>'._L('The following endpoints are registered by the plugins in this system:').'</p>';
103
                                $out['text'] .= '<p>'.$endpoints.'</p>';
104
                        } else {
105
                                $out['text'] .= '<p>'._L('No installed plugin offers a REST functionality').'</p>';
106
                        }
107
                        $out['text'] .= '<h2>'._L('Authentication').'</h2>';
108
                        $out['text'] .= '<p>'._L('The authentication is done via the following HTTP header:').'</p>';
109
                        $out['text'] .= '<p><pre id="oidplus_auth_jwt">';
110
                        $out['text'] .= 'Authentication: Bearer '.htmlentities($token)."\n";
111
                        $out['text'] .= '</pre></p>';
112
                        $out['text'] .= '<p><input type="button" value="'._L('Copy to clipboard').'" onClick="copyToClipboard(oidplus_auth_jwt)"></p>';
113
                        $out['text'] .= '<p>'._L('Please keep this information confidential!').'</p>';
114
                        $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>';
115
 
116
                        $out['text'] .= '<h2>'._L('Blacklisted tokens').'</h2>';
117
                        $bl_time = OIDplusAuthContentStoreJWT::jwtGetBlacklistTime($gen, $sub);
118
                        if ($bl_time == 0) {
119
                                $out['text'] .= '<p>'._L('None of the previously generated JWT tokens have been blacklisted.').'</p>';
120
                        } else {
121
                                $out['text'] .= '<p>'._L('All tokens generated before %1 have been blacklisted.',date('d F Y, H:i:s',$bl_time+1)).'</p>';
122
                        }
123
                        $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>';
124
                }
125
        }
126
 
127
        /**
128
         * @param array $json
129
         * @param string|null $ra_email
130
         * @param bool $nonjs
131
         * @param string $req_goto
132
         * @return bool
133
         * @throws OIDplusException
134
         */
135
        public function tree(array &$json, string $ra_email=null, bool $nonjs=false, string $req_goto=''): bool {
136
                if (!$ra_email) return false;
137
                if (!OIDplus::authUtils()->isRaLoggedIn($ra_email) && !OIDplus::authUtils()->isAdminLoggedIn()) return false;
138
 
139
                if (file_exists(__DIR__.'/img/main_icon16.png')) {
140
                        $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png';
141
                } else {
142
                        $tree_icon = null; // default icon (folder)
143
                }
144
 
145
                $json[] = array(
146
                        'id' => 'oidplus:rest_api_information_ra$'.$ra_email,
147
                        'icon' => $tree_icon,
148
                        'text' => _L('REST API')
149
                );
150
 
151
                // TODO: Make "Endpoints" (with all installed plugins) and "Authentication" as menu entries!
152
 
153
                return true;
154
        }
155
 
156
        /**
157
         * @param string $request
158
         * @return array|false
159
         */
160
        public function tree_search(string $request) {
161
                return false;
162
        }
163
}