|
72 | 72 | use Monitor; |
73 | 73 | use NetworkPort; |
74 | 74 | use NetworkPortType; |
| 75 | +use OAuthClient; |
75 | 76 | use Peripheral; |
76 | 77 | use Plugin; |
77 | 78 | use Printer; |
@@ -140,6 +141,8 @@ class Conf extends CommonGLPI |
140 | 141 |
|
141 | 142 | public const STALE_AGENT_ACTION_TRASHBIN = 2; |
142 | 143 |
|
| 144 | + public const NO_AUTH = 'none'; |
| 145 | + |
143 | 146 | public const CLIENT_CREDENTIALS = 'client_credentials'; |
144 | 147 |
|
145 | 148 | public const BASIC_AUTH = 'basic_auth'; |
@@ -396,18 +399,50 @@ public function showConfigForm() |
396 | 399 | echo "</tr>"; |
397 | 400 | echo "<tr class='tab_bg_1'>"; |
398 | 401 | echo "<td>"; |
399 | | - echo "<i class='ti ti-cloud-lock me-2'></i>"; |
400 | | - echo "<label for='auth'>" . __s('Authorization header') . "</label>"; |
| 402 | + $auth_rand = mt_rand(); |
| 403 | + echo "<i class='ti ti-shield me-2'></i>"; |
| 404 | + echo "<label for='dropdown_auth_required{$auth_rand}'>" . __s('Authorization header') . "</label>"; |
| 405 | + echo "<span class='required'>*</span>"; |
401 | 406 | echo "</td>"; |
402 | | - echo "<td>"; |
| 407 | + echo "<td colspan='3'>"; |
403 | 408 | Dropdown::showFromArray('auth_required', [ |
404 | | - 'none' => __('None'), |
| 409 | + '' => Dropdown::EMPTY_VALUE, |
405 | 410 | self::CLIENT_CREDENTIALS => __s('OAuth - Client credentials'), |
406 | 411 | self::BASIC_AUTH => __s('Basic Authentication'), |
| 412 | + self::NO_AUTH => __('None (not recommended)'), |
407 | 413 | ], [ |
408 | | - 'value' => $config['auth_required'] ?? 'none', |
| 414 | + 'value' => $config['auth_required'] ?? '', |
| 415 | + 'required' => true, |
| 416 | + 'rand' => $auth_rand, |
409 | 417 | ]); |
| 418 | + echo "<div id='oauth_client_hint_row' class='mt-1'>"; |
| 419 | + echo "<div class='alert alert-info d-inline-flex align-items-center mb-0 py-2' role='note'>"; |
| 420 | + echo "<i class='ti ti-info-circle flex-shrink-0 me-1'></i>"; |
| 421 | + echo "<span>"; |
| 422 | + echo __s('Using OAuth Client Credentials requires a registered OAuth client with the "inventory" scope.'); |
| 423 | + echo '<br>'; |
| 424 | + echo sprintf( |
| 425 | + __s('The generated client ID and secret must then be set in the GLPI Agent configuration using the %s and %s parameters.'), |
| 426 | + '<code>oauth-client-id</code>', |
| 427 | + '<code>oauth-client-secret</code>' |
| 428 | + ); |
| 429 | + echo ' <a href="' . htmlescape(OAuthClient::getFormURL()) . '">'; |
| 430 | + echo __s('Add an OAuth client'); |
| 431 | + echo ' <i class="ti ti-external-link ms-1"></i>'; |
| 432 | + echo '</a>'; |
| 433 | + echo "</span>"; |
| 434 | + echo "</div>"; |
| 435 | + echo "</div>"; |
| 436 | + echo "<div id='no_auth_warning_row' class='mt-1'>"; |
| 437 | + echo "<div class='alert alert-warning d-inline-flex align-items-center mb-0 py-2' role='alert'>"; |
| 438 | + echo "<i class='ti ti-alert-triangle flex-shrink-0 me-1'></i>"; |
| 439 | + echo "<span>"; |
| 440 | + echo __s('Not using any authentication on inventory is not recommended and poses a security risk. Any agent will be able to send inventory data without verification.'); |
| 441 | + echo "</span>"; |
| 442 | + echo "</div>"; |
| 443 | + echo "</div>"; |
410 | 444 | echo "</td></tr>"; |
| 445 | + |
411 | 446 | echo "<tr class='tab_bg_1' id='basic_auth_login_row'>"; |
412 | 447 | echo "<td>"; |
413 | 448 | echo "<i class='ti ti-abc me-2'></i>"; |
@@ -437,13 +472,14 @@ public function showConfigForm() |
437 | 472 | echo "</tr>"; |
438 | 473 | echo Html::scriptBlock(" |
439 | 474 | function toggleDisplayLoginInputs(select) { |
440 | | - let displayedInputs = false; |
441 | 475 | const selectedValue = $(select).val(); |
442 | | - if (selectedValue == '" . self::BASIC_AUTH . "') { |
443 | | - displayedInputs = true; |
444 | | - } |
445 | | - $('#basic_auth_login_row').toggle(displayedInputs); |
446 | | - $('#basic_auth_password_row').toggle(displayedInputs); |
| 476 | + const isBasicAuth = selectedValue == '" . self::BASIC_AUTH . "'; |
| 477 | + const isOAuth = selectedValue == '" . self::CLIENT_CREDENTIALS . "'; |
| 478 | + const isNoAuth = selectedValue == '" . self::NO_AUTH . "'; |
| 479 | + $('#basic_auth_login_row').toggle(isBasicAuth); |
| 480 | + $('#basic_auth_password_row').toggle(isBasicAuth); |
| 481 | + $('#oauth_client_hint_row').toggle(isOAuth); |
| 482 | + $('#no_auth_warning_row').toggle(isNoAuth); |
447 | 483 | } |
448 | 484 |
|
449 | 485 | const selectAuthHeader = $(`select[name='auth_required']`); |
@@ -1146,6 +1182,24 @@ public function saveConf(array $values) |
1146 | 1182 | $values['stale_agents_status_condition'] = ['all']; |
1147 | 1183 | } |
1148 | 1184 |
|
| 1185 | + $enabled_inventory = (int) ($values['enabled_inventory'] ?? $defaults['enabled_inventory']) === 1; |
| 1186 | + if ($enabled_inventory) { |
| 1187 | + $allowed_auth_required = [ |
| 1188 | + self::CLIENT_CREDENTIALS, |
| 1189 | + self::BASIC_AUTH, |
| 1190 | + self::NO_AUTH, |
| 1191 | + ]; |
| 1192 | + $auth_required = $values['auth_required'] ?? null; |
| 1193 | + if (!is_string($auth_required) || !in_array($auth_required, $allowed_auth_required, true)) { |
| 1194 | + Session::addMessageAfterRedirect( |
| 1195 | + __s('Inventory is enabled. Please select a valid authorization header method.'), |
| 1196 | + false, |
| 1197 | + ERROR |
| 1198 | + ); |
| 1199 | + return false; |
| 1200 | + } |
| 1201 | + } |
| 1202 | + |
1149 | 1203 | if (isset($values['auth_required']) && $values['auth_required'] === Conf::BASIC_AUTH) { |
1150 | 1204 | if ( |
1151 | 1205 | !empty($values['basic_auth_password']) |
@@ -1306,7 +1360,7 @@ public static function getDefaults(): array |
1306 | 1360 | 'stale_agents_status' => 0, |
1307 | 1361 | 'stale_agents_status_condition' => exportArrayToDB(['all']), |
1308 | 1362 | 'import_env' => 0, |
1309 | | - 'auth_required' => 'none', |
| 1363 | + 'auth_required' => '', |
1310 | 1364 | 'basic_auth_login' => '', |
1311 | 1365 | 'basic_auth_password' => '', |
1312 | 1366 | ]; |
|
0 commit comments