Subversion Repositories oidplus

Rev

Rev 984 | Rev 1086 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 984 Rev 1050
1
<?php
1
<?php
2
 
2
 
3
/*
3
/*
4
 * OIDplus 2.0
4
 * OIDplus 2.0
5
 * Copyright 2019 - 2021 Daniel Marschall, ViaThinkSoft
5
 * Copyright 2019 - 2021 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
if (!defined('INSIDE_OIDPLUS')) die();
23
namespace ViaThinkSoft\OIDplus;
24
 
24
 
25
class OIDplusPageRaAutomatedAJAXCalls extends OIDplusPagePluginRa {
25
class OIDplusPageRaAutomatedAJAXCalls extends OIDplusPagePluginRa {
26
 
26
 
27
        public function action($actionID, $params) {
27
        public function action($actionID, $params) {
28
                if ($actionID == 'blacklistJWT') {
28
                if ($actionID == 'blacklistJWT') {
29
                        if (!OIDplus::baseConfig()->getValue('JWT_ALLOW_AJAX_USER', true)) {
29
                        if (!OIDplus::baseConfig()->getValue('JWT_ALLOW_AJAX_USER', true)) {
30
                                throw new OIDplusException(_L('The administrator has disabled this feature. (Base configuration setting %1).','JWT_ALLOW_AJAX_USER'));
30
                                throw new OIDplusException(_L('The administrator has disabled this feature. (Base configuration setting %1).','JWT_ALLOW_AJAX_USER'));
31
                        }
31
                        }
32
 
32
 
33
                        _CheckParamExists($params, 'user');
33
                        _CheckParamExists($params, 'user');
34
                        $ra_email = $params['user'];
34
                        $ra_email = $params['user'];
35
 
35
 
36
                        if (!OIDplus::authUtils()->isRaLoggedIn($ra_email) && !OIDplus::authUtils()->isAdminLoggedIn()) {
36
                        if (!OIDplus::authUtils()->isRaLoggedIn($ra_email) && !OIDplus::authUtils()->isAdminLoggedIn()) {
37
                                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>'));
37
                                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>'));
38
                        }
38
                        }
39
 
39
 
40
                        $gen = OIDplusAuthContentStoreJWT::JWT_GENERATOR_AJAX;
40
                        $gen = OIDplusAuthContentStoreJWT::JWT_GENERATOR_AJAX;
41
                        $sub = $ra_email;
41
                        $sub = $ra_email;
42
 
42
 
43
                        OIDplusAuthContentStoreJWT::jwtBlacklist($gen, $sub);
43
                        OIDplusAuthContentStoreJWT::jwtBlacklist($gen, $sub);
44
 
44
 
45
                        return array("status" => 0);
45
                        return array("status" => 0);
46
                } else {
46
                } else {
47
                        throw new OIDplusException(_L('Unknown action ID'));
47
                        throw new OIDplusException(_L('Unknown action ID'));
48
                }
48
                }
49
        }
49
        }
50
 
50
 
51
        public function gui($id, &$out, &$handled) {
51
        public function gui($id, &$out, &$handled) {
52
                if (explode('$',$id)[0] == 'oidplus:automated_ajax_information_ra') {
52
                if (explode('$',$id)[0] == 'oidplus:automated_ajax_information_ra') {
53
                        $handled = true;
53
                        $handled = true;
54
 
54
 
55
                        $ra_email = explode('$',$id)[1];
55
                        $ra_email = explode('$',$id)[1];
56
 
56
 
57
                        $out['title'] = _L('Automated AJAX calls');
57
                        $out['title'] = _L('Automated AJAX calls');
58
                        $out['icon'] = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
58
                        $out['icon'] = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
59
 
59
 
60
                        if (!OIDplus::authUtils()->isRaLoggedIn($ra_email) && !OIDplus::authUtils()->isAdminLoggedIn()) {
60
                        if (!OIDplus::authUtils()->isRaLoggedIn($ra_email) && !OIDplus::authUtils()->isAdminLoggedIn()) {
61
                                $out['icon'] = 'img/error.png';
61
                                $out['icon'] = 'img/error.png';
62
                                $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>';
62
                                $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>';
63
                                return;
63
                                return;
64
                        }
64
                        }
65
 
65
 
66
                        if (!OIDplus::baseConfig()->getValue('JWT_ALLOW_AJAX_USER', true)) {
66
                        if (!OIDplus::baseConfig()->getValue('JWT_ALLOW_AJAX_USER', true)) {
67
                                $out['text'] = '<p>'._L('The administrator has disabled this feature. (Base configuration setting %1).','JWT_ALLOW_AJAX_USER').'</p>';
67
                                $out['text'] = '<p>'._L('The administrator has disabled this feature. (Base configuration setting %1).','JWT_ALLOW_AJAX_USER').'</p>';
68
                                return;
68
                                return;
69
                        }
69
                        }
70
 
70
 
71
                        $gen = OIDplusAuthContentStoreJWT::JWT_GENERATOR_AJAX;
71
                        $gen = OIDplusAuthContentStoreJWT::JWT_GENERATOR_AJAX;
72
                        $sub = $ra_email;
72
                        $sub = $ra_email;
73
 
73
 
74
                        $authSimulation = new OIDplusAuthContentStoreJWT();
74
                        $authSimulation = new OIDplusAuthContentStoreJWT();
75
                        $authSimulation->raLogin($ra_email);
75
                        $authSimulation->raLogin($ra_email);
76
                        $authSimulation->setValue('oidplus_generator', $gen);
76
                        $authSimulation->setValue('oidplus_generator', $gen);
77
                        $token = $authSimulation->getJWTToken();
77
                        $token = $authSimulation->getJWTToken();
78
 
78
 
79
                        $out['text'] .= '<p>'._L('You can make automated calls to your OIDplus account by calling the AJAX API.').'</p>';
79
                        $out['text'] .= '<p>'._L('You can make automated calls to your OIDplus account by calling the AJAX API.').'</p>';
80
                        $out['text'] .= '<p>'._L('The URL for the AJAX script is:').'</p>';
80
                        $out['text'] .= '<p>'._L('The URL for the AJAX script is:').'</p>';
81
                        $out['text'] .= '<p><b>'.OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL).'ajax.php</b></p>';
81
                        $out['text'] .= '<p><b>'.OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL).'ajax.php</b></p>';
82
                        $out['text'] .= '<p>'._L('You must at least provide following fields:').'</p>';
82
                        $out['text'] .= '<p>'._L('You must at least provide following fields:').'</p>';
83
                        $out['text'] .= '<p><pre>';
83
                        $out['text'] .= '<p><pre>';
84
                        $out['text'] .= 'OIDPLUS_AUTH_JWT = "'.htmlentities($token).'"'."\n";
84
                        $out['text'] .= 'OIDPLUS_AUTH_JWT = "'.htmlentities($token).'"'."\n";
85
                        $out['text'] .= '</pre></p>';
85
                        $out['text'] .= '</pre></p>';
86
                        $out['text'] .= '<p><input type="button" value="'._L('Copy to clipboard').'" onClick="copyToClipboard(oidplus_auth_jwt)"></p>';
86
                        $out['text'] .= '<p><input type="button" value="'._L('Copy to clipboard').'" onClick="copyToClipboard(oidplus_auth_jwt)"></p>';
87
                        $out['text'] .= '<p>'._L('Please keep this information confidential!').'</p>';
87
                        $out['text'] .= '<p>'._L('Please keep this information confidential!').'</p>';
88
                        $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>';
88
                        $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>';
89
                        $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>';
89
                        $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>';
90
 
90
 
91
                        $out['text'] .= '<h2>'._L('Blacklisted tokens').'</h2>';
91
                        $out['text'] .= '<h2>'._L('Blacklisted tokens').'</h2>';
92
                        $bl_time = OIDplusAuthContentStoreJWT::jwtGetBlacklistTime($gen, $sub);
92
                        $bl_time = OIDplusAuthContentStoreJWT::jwtGetBlacklistTime($gen, $sub);
93
                        if ($bl_time == 0) {
93
                        if ($bl_time == 0) {
94
                                $out['text'] .= '<p>'._L('None of the previously generated JWT tokens have been blacklisted.').'</p>';
94
                                $out['text'] .= '<p>'._L('None of the previously generated JWT tokens have been blacklisted.').'</p>';
95
                        } else {
95
                        } else {
96
                                $out['text'] .= '<p>'._L('All tokens generated before %1 have been blacklisted.',date('d F Y, H:i:s',$bl_time+1)).'</p>';
96
                                $out['text'] .= '<p>'._L('All tokens generated before %1 have been blacklisted.',date('d F Y, H:i:s',$bl_time+1)).'</p>';
97
                        }
97
                        }
98
                        $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>';
98
                        $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>';
99
 
99
 
100
                        $out['text'] .= '<h2>'._L('Example for adding OID 2.999.123 using JavaScript').'</h2>';
100
                        $out['text'] .= '<h2>'._L('Example for adding OID 2.999.123 using JavaScript').'</h2>';
101
                        $cont = file_get_contents(__DIR__.'/examples/example_js.html');
101
                        $cont = file_get_contents(__DIR__.'/examples/example_js.html');
102
                        $cont = str_replace('<url>', OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL).'ajax.php', $cont);
102
                        $cont = str_replace('<url>', OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL).'ajax.php', $cont);
103
                        $cont = str_replace('<token>', $token, $cont);
103
                        $cont = str_replace('<token>', $token, $cont);
104
                        $out['text'] .= '<pre id="example_js">'.htmlentities($cont).'</pre>';
104
                        $out['text'] .= '<pre id="example_js">'.htmlentities($cont).'</pre>';
105
                        $out['text'] .= '<p><input type="button" value="'._L('Copy to clipboard').'" onClick="copyToClipboard(example_js)"></p>';
105
                        $out['text'] .= '<p><input type="button" value="'._L('Copy to clipboard').'" onClick="copyToClipboard(example_js)"></p>';
106
 
106
 
107
                        $out['text'] .= '<h2>'._L('Example for adding OID 2.999.123 using PHP (located at a foreign server)').'</h2>';
107
                        $out['text'] .= '<h2>'._L('Example for adding OID 2.999.123 using PHP (located at a foreign server)').'</h2>';
108
                        $cont = file_get_contents(__DIR__.'/examples/example_php.phps');
108
                        $cont = file_get_contents(__DIR__.'/examples/example_php.phps');
109
                        $cont = str_replace('<url>', OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL).'ajax.php', $cont);
109
                        $cont = str_replace('<url>', OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL).'ajax.php', $cont);
110
                        $cont = str_replace('<token>', $token, $cont);
110
                        $cont = str_replace('<token>', $token, $cont);
111
                        $out['text'] .= '<pre id="example_php">'.preg_replace("@<br.*>@ismU","",highlight_string($cont,true)).'</pre>';
111
                        $out['text'] .= '<pre id="example_php">'.preg_replace("@<br.*>@ismU","",highlight_string($cont,true)).'</pre>';
112
                        $out['text'] .= '<p><input type="button" value="'._L('Copy to clipboard').'" onClick="copyToClipboard(example_php)"></p>';
112
                        $out['text'] .= '<p><input type="button" value="'._L('Copy to clipboard').'" onClick="copyToClipboard(example_php)"></p>';
113
 
113
 
114
                        $out['text'] .= '<h2>'._L('Example for adding OID 2.999.123 using Python').'</h2>';
114
                        $out['text'] .= '<h2>'._L('Example for adding OID 2.999.123 using Python').'</h2>';
115
                        $cont = file_get_contents(__DIR__.'/examples/example_python.py');
115
                        $cont = file_get_contents(__DIR__.'/examples/example_python.py');
116
                        $cont = str_replace('<url>', OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL).'ajax.php', $cont);
116
                        $cont = str_replace('<url>', OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL).'ajax.php', $cont);
117
                        $cont = str_replace('<token>', $token, $cont);
117
                        $cont = str_replace('<token>', $token, $cont);
118
                        $out['text'] .= '<pre id="example_python">'.htmlentities($cont).'</pre>';
118
                        $out['text'] .= '<pre id="example_python">'.htmlentities($cont).'</pre>';
119
                        $out['text'] .= '<p><input type="button" value="'._L('Copy to clipboard').'" onClick="copyToClipboard(example_python)"></p>';
119
                        $out['text'] .= '<p><input type="button" value="'._L('Copy to clipboard').'" onClick="copyToClipboard(example_python)"></p>';
120
 
120
 
121
                        $out['text'] .= '<h2>'._L('Example for adding OID 2.999.123 using VBScript').'</h2>';
121
                        $out['text'] .= '<h2>'._L('Example for adding OID 2.999.123 using VBScript').'</h2>';
122
                        $cont = file_get_contents(__DIR__.'/examples/example_vbs.vbs');
122
                        $cont = file_get_contents(__DIR__.'/examples/example_vbs.vbs');
123
                        $cont = str_replace('<url>', OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL).'ajax.php', $cont);
123
                        $cont = str_replace('<url>', OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL).'ajax.php', $cont);
124
                        $cont = str_replace('<token>', $token, $cont);
124
                        $cont = str_replace('<token>', $token, $cont);
125
                        $out['text'] .= '<pre id="example_vbs">'.htmlentities($cont).'</pre>';
125
                        $out['text'] .= '<pre id="example_vbs">'.htmlentities($cont).'</pre>';
126
                        $out['text'] .= '<p><input type="button" value="'._L('Copy to clipboard').'" onClick="copyToClipboard(example_vbs)"></p>';
126
                        $out['text'] .= '<p><input type="button" value="'._L('Copy to clipboard').'" onClick="copyToClipboard(example_vbs)"></p>';
127
                }
127
                }
128
        }
128
        }
129
 
129
 
130
        public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
130
        public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
131
                if (!$ra_email) return false;
131
                if (!$ra_email) return false;
132
                if (!OIDplus::authUtils()->isRaLoggedIn($ra_email) && !OIDplus::authUtils()->isAdminLoggedIn()) return false;
132
                if (!OIDplus::authUtils()->isRaLoggedIn($ra_email) && !OIDplus::authUtils()->isAdminLoggedIn()) return false;
133
 
133
 
134
                if (file_exists(__DIR__.'/img/main_icon16.png')) {
134
                if (file_exists(__DIR__.'/img/main_icon16.png')) {
135
                        $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png';
135
                        $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png';
136
                } else {
136
                } else {
137
                        $tree_icon = null; // default icon (folder)
137
                        $tree_icon = null; // default icon (folder)
138
                }
138
                }
139
 
139
 
140
                $json[] = array(
140
                $json[] = array(
141
                        'id' => 'oidplus:automated_ajax_information_ra$'.$ra_email,
141
                        'id' => 'oidplus:automated_ajax_information_ra$'.$ra_email,
142
                        'icon' => $tree_icon,
142
                        'icon' => $tree_icon,
143
                        'text' => _L('Automated AJAX calls')
143
                        'text' => _L('Automated AJAX calls')
144
                );
144
                );
145
 
145
 
146
                return true;
146
                return true;
147
        }
147
        }
148
 
148
 
149
        public function tree_search($request) {
149
        public function tree_search($request) {
150
                return false;
150
                return false;
151
        }
151
        }
152
}
152
}
153
 
153