Skip to content
This repository was archived by the owner on Aug 9, 2021. It is now read-only.

Commit b340668

Browse files
committed
refactor(m2mauth): add abstract class for mutualized code
Signed-off-by: Thierry Bugier <[email protected]>
1 parent 9c6f00e commit b340668

File tree

5 files changed

+97
-14
lines changed

5 files changed

+97
-14
lines changed

front/mosquitto-authenticate.php renamed to front/mosquittoauth.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,26 @@
3232
// Workaround CSRF checks
3333
$postData = $_POST;
3434
unset($_POST);
35+
3536
include '../../../inc/includes.php';
3637
$plugin = new Plugin();
3738
if (!$plugin->isActivated('flyvemdm')) {
3839
http_response_code(404);
3940
}
41+
42+
// Restore POST data
4043
$_POST = $postData;
4144
$flyvemdmM2mApi = new PluginFlyvemdmMosquittoAuth();
42-
$answer = $flyvemdmM2mApi->authenticate($_POST);
4345

44-
http_response_code($answer);
46+
if (isset($_GET['authenticate'])) {
47+
$answer = $flyvemdmM2mApi->authenticate($_POST);
48+
http_response_code($answer);
49+
die();
50+
}
51+
if (isset($_GET['authorize'])) {
52+
$answer = $flyvemdmM2mApi->authorize($_POST);
53+
http_response_code($answer);
54+
die();
55+
}
56+
57+
http_response_code(404);

inc/m2mauth.class.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* LICENSE
4+
*
5+
* Copyright © 2016-2018 Teclib'
6+
* Copyright © 2010-2018 by the FusionInventory Development Team.
7+
*
8+
* This file is part of Flyve MDM Plugin for GLPI.
9+
*
10+
* Flyve MDM Plugin for GLPI is a subproject of Flyve MDM. Flyve MDM is a mobile
11+
* device management software.
12+
*
13+
* Flyve MDM Plugin for GLPI is free software: you can redistribute it and/or
14+
* modify it under the terms of the GNU Affero General Public License as published
15+
* by the Free Software Foundation, either version 3 of the License, or
16+
* (at your option) any later version.
17+
* Flyve MDM Plugin for GLPI is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
* You should have received a copy of the GNU Affero General Public License
22+
* along with Flyve MDM Plugin for GLPI. If not, see http://www.gnu.org/licenses/.
23+
* ------------------------------------------------------------------------------
24+
* @author Thierry Bugier
25+
* @copyright Copyright © 2018 Teclib
26+
* @license AGPLv3+ http://www.gnu.org/licenses/agpl.txt
27+
* @link https://github.com/flyve-mdm/glpi-plugin
28+
* @link https://flyve-mdm.com/
29+
* ------------------------------------------------------------------------------
30+
*/
31+
32+
if (!defined('GLPI_ROOT')) {
33+
die("Sorry. You can't access this file directly");
34+
}
35+
36+
abstract class PluginFlyvemdmM2mAuth implements PluginFlyvemdmM2mAuthInterface {
37+
38+
/**
39+
* Checks the remote IP address matches the configured M2M server
40+
*/
41+
protected function checkRemote() {
42+
$remoteIp = Toolbox::getRemoteIpAddress();
43+
$config = Config::getConfigurationValues('flyvemdm', ['mqtt_broker_internal_address']);
44+
45+
// Try assuming the internal address is an IP Address
46+
if ($config['mqtt_broker_internal_address'] == $remoteIp) {
47+
return true;
48+
}
49+
50+
foreach (gethostbynamel($config['mqtt_broker_internal_address']) as $validIp) {
51+
if ($remoteIp == $validIp) {
52+
return true;
53+
}
54+
}
55+
56+
return false;
57+
}
58+
}

inc/m2mauthinterface.class.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,6 @@ interface PluginFlyvemdmM2mAuthInterface {
4343
* @return integer HTTP response code
4444
*/
4545
public function authenticate($input);
46+
47+
public function authorize($input);
4648
}

inc/mosquittoauth.class.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,17 @@
3333
die("Sorry. You can't access this file directly");
3434
}
3535

36-
class PluginFlyvemdmMosquittoAuth implements PluginFlyvemdmM2mAuthInterface {
36+
class PluginFlyvemdmMosquittoAuth extends PluginFlyvemdmM2mAuth {
3737
public function authenticate($input) {
38+
if (!$this->checkRemote()) {
39+
return 403;
40+
}
41+
3842
if (!isset($input['username']) || !isset($input['password'])) {
3943
// No credentials or credentials incomplete
4044
return 404;
4145
}
4246

43-
$remoteIp = Toolbox::getRemoteIpAddress();
44-
$config = Config::getConfigurationValues('flyvemdm', ['mqtt_broker_internal_address']);
45-
if ($config['mqtt_broker_internal_address'] != $remoteIp) {
46-
return 403;
47-
}
48-
4947
$mqttUser = new PluginFlyvemdmMqttUser();
5048
if (!$mqttUser->getByUser($input['username'])) {
5149
return 404;
@@ -55,6 +53,12 @@ public function authenticate($input) {
5553
return 200;
5654
}
5755

56+
return 404;
57+
}
58+
59+
public function authorize($input) {
60+
61+
5862
return 404;
5963
}
6064
}

tests/suite-unit/PluginFlyvemdmMosquittoAuth.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,19 @@ public function beforeTestMethod($method) {
4444
'user' => 'john',
4545
'password' => 'doe',
4646
]);
47+
$this->boolean($this->mqttUser->isNewItem())->isFalse('Failed to create a MQTT user');
4748
break;
4849
}
4950
}
5051

5152
public function afterTestMethod($method) {
5253
switch ($method) {
5354
case 'testAuthenticate':
54-
$this->mqttUser->delete($this->mqttUser->fields, 1);
55+
$this->mqttUser->delete([
56+
'id' => $this->mqttUser->getID(),
57+
],
58+
1
59+
);
5560
break;
5661
}
5762
}
@@ -63,7 +68,7 @@ public function providerAuthenticate() {
6368
'username' => 'foo',
6469
'password' => 'bar',
6570
],
66-
'repoteIp' => '127.0.0.1',
71+
'remoteIp' => '127.0.0.1',
6772
'expected' => [
6873
'httpCode' => '404',
6974
]
@@ -73,7 +78,7 @@ public function providerAuthenticate() {
7378
'username' => 'john',
7479
'password' => 'doe',
7580
],
76-
'repoteIp' => '127.0.0.1',
81+
'remoteIp' => '127.0.0.1',
7782
'expected' => [
7883
'httpCode' => '200',
7984
]
@@ -83,7 +88,7 @@ public function providerAuthenticate() {
8388
'username' => 'john',
8489
'password' => 'bar',
8590
],
86-
'repoteIp' => '127.0.0.1',
91+
'remoteIp' => '127.0.0.1',
8792
'expected' => [
8893
'httpCode' => '404',
8994
]
@@ -93,7 +98,7 @@ public function providerAuthenticate() {
9398
'username' => 'john',
9499
'password' => 'doe',
95100
],
96-
'repoteIp' => '10.0.0.1',
101+
'remoteIp' => '10.0.0.1',
97102
'expected' => [
98103
'httpCode' => '403',
99104
]
@@ -103,6 +108,7 @@ public function providerAuthenticate() {
103108

104109
/**
105110
* @dataProvider providerAuthenticate
111+
* @engine inline
106112
*/
107113
public function testAuthenticate($input, $remoteIp, $expected) {
108114
$backupServer = $_SERVER;

0 commit comments

Comments
 (0)