Subversion Repositories oidplus

Rev

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

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