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

Commit 5a19aae

Browse files
committed
feat(m2m): authorization backend
Signed-off-by: Thierry Bugier <[email protected]>
1 parent b340668 commit 5a19aae

File tree

3 files changed

+417
-27
lines changed

3 files changed

+417
-27
lines changed

front/mosquittoauth.php

+5
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@
4848
http_response_code($answer);
4949
die();
5050
}
51+
if (isset($_GET['superuser'])) {
52+
$answer = $flyvemdmM2mApi->isSuperuser($_POST);
53+
http_response_code($answer);
54+
die();
55+
}
5156
if (isset($_GET['authorize'])) {
5257
$answer = $flyvemdmM2mApi->authorize($_POST);
5358
http_response_code($answer);

inc/mosquittoauth.class.php

+46-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ public function authenticate($input) {
4848
if (!$mqttUser->getByUser($input['username'])) {
4949
return 404;
5050
}
51+
if ($mqttUser->getField('enabled') == '0') {
52+
return 404;
53+
}
5154
$input['password'] = Toolbox::stripslashes_deep($input['password']);
5255
if ($mqttUser->comparePasswords($input['password'])) {
5356
return 200;
@@ -57,8 +60,50 @@ public function authenticate($input) {
5760
}
5861

5962
public function authorize($input) {
63+
$mqttUser = new PluginFlyvemdmMqttUser();
64+
if (!$mqttUser->getByUser($input['username'])) {
65+
return 403;
66+
}
67+
if ($mqttUser->getField('enabled') == '0') {
68+
return 403;
69+
}
70+
71+
$mqttUserId = $mqttUser->getID();
72+
$acc = (int) $input['acc'];
73+
$requestedTopic = explode('/', $input['topic']);
74+
$mqttAcl = new PluginFlyvemdmMqttAcl();
75+
$rows = $mqttAcl->find("`plugin_flyvemdm_mqttusers_id`='$mqttUserId'
76+
AND `access_level` & $acc");
77+
foreach ($rows as $row) {
78+
$topic = explode('/', $row['topic']);
79+
$match = true;
80+
foreach ($topic as $index => $pathItem) {
81+
if ($pathItem === '+') {
82+
// This path item matches a joker
83+
continue;
84+
}
85+
if ($pathItem === '#' && $index === count($topic) - 1) {
86+
continue;
87+
}
88+
if (!isset($requestedTopic[$index])) {
89+
$match = false;
90+
break;
91+
}
92+
if ($pathItem !== $requestedTopic[$index]) {
93+
// This topic does not match, try the next one
94+
$match = false;
95+
break;
96+
}
97+
}
98+
if ($match) {
99+
return 200;
100+
}
101+
}
60102

103+
return 403;
104+
}
61105

62-
return 404;
106+
public function isSuperuser($input) {
107+
return 403;
63108
}
64109
}

0 commit comments

Comments
 (0)