Subversion Repositories oidplus

Rev

Rev 582 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
329 daniel-mar 1
<?php
2
 
3
/*
4
 * OIDplus 2.0
511 daniel-mar 5
 * Copyright 2019 - 2021 Daniel Marschall, ViaThinkSoft
329 daniel-mar 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 OIDplusPageRaAutomatedAJAXCalls
22
 
511 daniel-mar 23
if (!defined('INSIDE_OIDPLUS')) die();
24
 
329 daniel-mar 25
class OIDplusPageAdminAutomatedAJAXCalls extends OIDplusPagePluginAdmin {
26
 
574 daniel-mar 27
        public function action($actionID, $params) {
28
                if ($actionID == 'blacklistJWT') {
575 daniel-mar 29
                        if (!OIDplus::authUtils()->isAdminLoggedIn()) {
30
                                throw new OIDplusException(_L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login$admin')));
31
                        }
32
 
33
                        if (!OIDplus::baseConfig()->getValue('JWT_ALLOW_AJAX_ADMIN', true)) {
34
                                throw new OIDplusException(_L('The administrator has disabled this feature. (Base configuration setting %1).','JWT_ALLOW_AJAX_ADMIN'));
35
                        }
36
 
585 daniel-mar 37
                        $gen = OIDplusAuthContentStoreJWT::JWT_GENERATOR_AJAX;
574 daniel-mar 38
                        $sub = 'admin';
329 daniel-mar 39
 
585 daniel-mar 40
                        OIDplusAuthContentStoreJWT::jwtBlacklist($gen, $sub);
329 daniel-mar 41
 
574 daniel-mar 42
                        return array("status" => 0);
43
                } else {
44
                        throw new OIDplusException(_L('Unknown action ID'));
329 daniel-mar 45
                }
46
        }
47
 
48
        public function gui($id, &$out, &$handled) {
49
                if ($id === 'oidplus:automated_ajax_information_admin') {
50
                        $handled = true;
360 daniel-mar 51
                        $out['title'] = _L('Automated AJAX calls');
329 daniel-mar 52
                        $out['icon'] = file_exists(__DIR__.'/icon_big.png') ? OIDplus::webpath(__DIR__).'icon_big.png' : '';
53
 
549 daniel-mar 54
                        if (!OIDplus::authUtils()->isAdminLoggedIn()) {
329 daniel-mar 55
                                $out['icon'] = 'img/error_big.png';
559 daniel-mar 56
                                $out['text'] = '<p>'._L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login$admin')).'</p>';
329 daniel-mar 57
                                return;
58
                        }
59
 
574 daniel-mar 60
                        if (!OIDplus::baseConfig()->getValue('JWT_ALLOW_AJAX_ADMIN', true)) {
61
                                $out['text'] = '<p>'._L('The administrator has disabled this feature. (Base configuration setting %1).','JWT_ALLOW_AJAX_ADMIN').'</p>';
62
                                return;
63
                        }
64
 
585 daniel-mar 65
                        $gen = OIDplusAuthContentStoreJWT::JWT_GENERATOR_AJAX;
574 daniel-mar 66
                        $sub = 'admin';
67
 
566 daniel-mar 68
                        $authSimulation = new OIDplusAuthContentStoreJWT();
69
                        $authSimulation->adminLogin();
574 daniel-mar 70
                        $authSimulation->setValue('oidplus_generator', $gen);
585 daniel-mar 71
                        $token = $authSimulation->getJWTToken();
566 daniel-mar 72
 
360 daniel-mar 73
                        $out['text'] .= '<p>'._L('You can make automated calls to your OIDplus account by calling the AJAX API.').'</p>';
572 daniel-mar 74
                        $out['text'] .= '<p>'._L('The URL for the AJAX script is:').'</p>';
496 daniel-mar 75
                        $out['text'] .= '<p><b>'.OIDplus::webpath(null,false).'ajax.php</b></p>';
572 daniel-mar 76
                        $out['text'] .= '<p>'._L('You must at least provide following fields:').'</p>';
329 daniel-mar 77
                        $out['text'] .= '<p><pre>';
568 daniel-mar 78
                        $out['text'] .= 'OIDPLUS_AUTH_JWT = "'.htmlentities($token).'"'."\n";
329 daniel-mar 79
                        $out['text'] .= '</pre></p>';
360 daniel-mar 80
                        $out['text'] .= '<p>'._L('Please keep this information confidential!').'</p>';
566 daniel-mar 81
                        $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>';
360 daniel-mar 82
                        $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>';
424 daniel-mar 83
 
574 daniel-mar 84
                        $out['text'] .= '<h2>'._L('Blacklisted tokens').'</h2>';
585 daniel-mar 85
                        $bl_time = OIDplusAuthContentStoreJWT::jwtGetBlacklistTime($gen, $sub);
576 daniel-mar 86
                        if ($bl_time == 0) {
574 daniel-mar 87
                                $out['text'] .= '<p>'._L('None of the previously generated JWT tokens have been blacklisted.').'</p>';
88
                        } else {
576 daniel-mar 89
                                $out['text'] .= '<p>'._L('All tokens generated before %1 have been blacklisted.',date('d F Y, H:i:s',$bl_time+1)).'</p>';
574 daniel-mar 90
                        }
91
                        $out['text'] .= '<button type="button" name="btn_blacklist_jwt" id="btn_blacklist_jwt" class="btn btn-danger btn-xs" onclick="OIDplusPageAdminAutomatedAJAXCalls.blacklistJWT()">'._L('Blacklist all previously generated tokens').'</button>';
92
 
360 daniel-mar 93
                        $out['text'] .= '<h2>'._L('Example for adding OID 2.999.123 using JavaScript').'</h2>';
424 daniel-mar 94
                        $cont = file_get_contents(__DIR__.'/examples/example_js.html');
496 daniel-mar 95
                        $cont = str_replace('<url>', OIDplus::webpath(null,false).'ajax.php', $cont);
568 daniel-mar 96
                        $cont = str_replace('<token>', $token, $cont);
424 daniel-mar 97
                        $out['text'] .= '<pre>'.htmlentities($cont).'</pre>';
425 daniel-mar 98
 
360 daniel-mar 99
                        $out['text'] .= '<h2>'._L('Example for adding OID 2.999.123 using PHP (located at a foreign server)').'</h2>';
424 daniel-mar 100
                        $cont = file_get_contents(__DIR__.'/examples/example_php.phps');
496 daniel-mar 101
                        $cont = str_replace('<url>', OIDplus::webpath(null,false).'ajax.php', $cont);
568 daniel-mar 102
                        $cont = str_replace('<token>', $token, $cont);
424 daniel-mar 103
                        $out['text'] .= '<pre>'.preg_replace("@<br.*>@ismU","",highlight_string($cont,true)).'</pre>';
425 daniel-mar 104
 
582 daniel-mar 105
                        $out['text'] .= '<h2>'._L('Example for adding OID 2.999.123 using Python').'</h2>';
106
                        $cont = file_get_contents(__DIR__.'/examples/example_python.py');
107
                        $cont = str_replace('<url>', OIDplus::webpath(null,false).'ajax.php', $cont);
108
                        $cont = str_replace('<token>', $token, $cont);
109
                        $out['text'] .= '<pre>'.htmlentities($cont).'</pre>';
110
 
425 daniel-mar 111
                        $out['text'] .= '<h2>'._L('Example for adding OID 2.999.123 using VBScript').'</h2>';
112
                        $cont = file_get_contents(__DIR__.'/examples/example_vbs.vbs');
496 daniel-mar 113
                        $cont = str_replace('<url>', OIDplus::webpath(null,false).'ajax.php', $cont);
568 daniel-mar 114
                        $cont = str_replace('<token>', $token, $cont);
425 daniel-mar 115
                        $out['text'] .= '<pre>'.htmlentities($cont).'</pre>';
329 daniel-mar 116
                }
117
        }
118
 
119
        public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
549 daniel-mar 120
                if (!OIDplus::authUtils()->isAdminLoggedIn()) return false;
329 daniel-mar 121
 
122
                if (file_exists(__DIR__.'/treeicon.png')) {
123
                        $tree_icon = OIDplus::webpath(__DIR__).'treeicon.png';
124
                } else {
125
                        $tree_icon = null; // default icon (folder)
126
                }
127
 
128
                $json[] = array(
129
                        'id' => 'oidplus:automated_ajax_information_admin',
130
                        'icon' => $tree_icon,
360 daniel-mar 131
                        'text' => _L('Automated AJAX calls')
329 daniel-mar 132
                );
133
 
134
                return true;
135
        }
136
 
137
        public function tree_search($request) {
138
                return false;
139
        }
426 daniel-mar 140
}