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

Commit 493bb9d

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

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

Diff for: inc/mosquittoauth.class.php

+30-1
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,37 @@ public function authenticate($input) {
5757
}
5858

5959
public function authorize($input) {
60+
$mqttUser = new PluginFlyvemdmMqttUser();
61+
if (!$mqttUser->getByUser($input['username'])) {
62+
return 403;
63+
}
64+
if ($mqttUser->getField('enabled') == '0') {
65+
return 403;
66+
}
6067

68+
$mqttUserId = $mqttUser->getID();
69+
$acc = (int) $input['acc'];
70+
$requestedTopic = explode('/', $input['topic']);
71+
$mqttAcl = new PluginFlyvemdmMqttAcl();
72+
$rows = $mqttAcl->find("`plugin_flyvemdm_mqttusers_id`='$mqttUserId'
73+
AND `access_level` & $acc");
74+
foreach ($rows as $row) {
75+
$topic = explode('/', $row['topic']);
76+
foreach ($topic as $index => $pathItem) {
77+
if ($pathItem === '#' && $index === count($topic) - 1) {
78+
return 200;
79+
}
80+
if ($pathItem === '+') {
81+
// This path item matches a joker
82+
continue;
83+
}
84+
if ($pathItem !== $requestedTopic[$index]) {
85+
// This topic does not match, try the next one
86+
break;
87+
}
88+
}
89+
}
6190

62-
return 404;
91+
return 403;
6392
}
6493
}

0 commit comments

Comments
 (0)