|
| 1 | +const express = require('express'); |
| 2 | +const bodyParser = require('body-parser'); |
| 3 | +const mqttWildcard = require('mqtt-wildcard'); |
| 4 | +const app = express(); |
| 5 | +app.use(bodyParser()); |
| 6 | +const port = 8080; |
| 7 | + |
| 8 | +const users = [ |
| 9 | + { |
| 10 | + login: 'testUser', |
| 11 | + password: 'testPassword', |
| 12 | + isSuper: false, |
| 13 | + acls: [ |
| 14 | + { |
| 15 | + topic: 'users/testUser/#', |
| 16 | + permissions: [ 'read', 'write', 'subscribe' ] // Can be "read", "write" or "subscribe" |
| 17 | + }, |
| 18 | + { |
| 19 | + topic: 'global', |
| 20 | + permissions: [ 'write', 'read', 'subscribe' ] |
| 21 | + } |
| 22 | + ] |
| 23 | + }, |
| 24 | + { |
| 25 | + login: 'testUser2', |
| 26 | + password: 'testPassword2', |
| 27 | + isSuper: false, |
| 28 | + acls: [ |
| 29 | + { |
| 30 | + topic: 'users/testUser2/#', |
| 31 | + permissions: [ 'read', 'write', 'subscribe' ] // Can be "read", "write" or "subscribe" |
| 32 | + }, |
| 33 | + { |
| 34 | + topic: 'global', |
| 35 | + permissions: [ 'write', 'read', 'subscribe' ] |
| 36 | + } |
| 37 | + ] |
| 38 | + } |
| 39 | +]; |
| 40 | + |
| 41 | + |
| 42 | +// Define POST route "/user" |
| 43 | +// This route will be used to check the user login and password |
| 44 | +app.post( |
| 45 | + '/user', |
| 46 | + (req, res) => { |
| 47 | + // Mosquitto sends us the username and the password |
| 48 | + const { username, password } = req.body; |
| 49 | + |
| 50 | + // We try to find the user |
| 51 | + const userFound = users.find(user => user.login === username && user.password === password); |
| 52 | + |
| 53 | + // We send a 200 if the authentication succeed or 401 else |
| 54 | + if (userFound) { |
| 55 | + return res.status(200).send(); |
| 56 | + } |
| 57 | + else { |
| 58 | + console.warn(`⛔️ User ${username} doesn't exist or password is incorrect`); |
| 59 | + return res.status(401).send(); |
| 60 | + } |
| 61 | + } |
| 62 | +); |
| 63 | + |
| 64 | + |
| 65 | +// Define POST route "/superUser" |
| 66 | +// This route will be used to check if the user is a super user or not |
| 67 | +app.post( |
| 68 | + '/superUser', |
| 69 | + (req, res) => { |
| 70 | + // Mosquitto sends us the username |
| 71 | + const { username } = req.body; |
| 72 | + |
| 73 | + // We try to find the user and check if he's a super user |
| 74 | + const userFound = users.find(user => user.login === username); |
| 75 | + |
| 76 | + // We send a 200 if he is a super user or 401 else |
| 77 | + if (userFound && userFound.isSuper) { |
| 78 | + return res.status(200).send(); |
| 79 | + } |
| 80 | + else { |
| 81 | + return res.status(401).send(); |
| 82 | + } |
| 83 | + } |
| 84 | +); |
| 85 | + |
| 86 | + |
| 87 | +// Define POST route "/acls" |
| 88 | +// This route will be used to check the topic ACL |
| 89 | +app.post( |
| 90 | + '/acls', |
| 91 | + (req, res) => { |
| 92 | + const { username, topic, clientId, acc } = req.body; |
| 93 | + |
| 94 | + // "acc" represents the type of access required by the client to this topic |
| 95 | + // - 1: read |
| 96 | + // - 2: write |
| 97 | + // - 3: read and write |
| 98 | + // - 4: subscribe |
| 99 | + |
| 100 | + const accToPermissions = { |
| 101 | + 1: [ 'read' ], |
| 102 | + 2: [ 'write' ], |
| 103 | + 3: [ 'read', 'write' ], |
| 104 | + 4: [ 'subscribe' ] |
| 105 | + } |
| 106 | + |
| 107 | + const allowed = users.find(user => { |
| 108 | + if (user.login !== username) { |
| 109 | + return false; |
| 110 | + } |
| 111 | + |
| 112 | + const aclValidated = user.acls.find( |
| 113 | + acl => mqttWildcard(topic, acl.topic) !== null |
| 114 | + && accToPermissions[acc].every(v => acl.permissions.includes(v)) |
| 115 | + ); |
| 116 | + return aclValidated; |
| 117 | + }); |
| 118 | + |
| 119 | + if (allowed) { |
| 120 | + return res.status(200).send(); |
| 121 | + } |
| 122 | + else { |
| 123 | + console.warn(`⛔️ Error when checking ACL for user ${username} on topic ${topic} with permission "${acc}"`); |
| 124 | + return res.status(401).send(); |
| 125 | + } |
| 126 | + } |
| 127 | +); |
| 128 | + |
| 129 | + |
| 130 | + |
| 131 | +// Start Express server |
| 132 | +const server = app.listen(port); |
| 133 | + |
| 134 | +// You'll see this log directly on your Stackhero's console |
| 135 | +console.log('🎉 The app has just start!'); |
| 136 | + |
| 137 | +// Handle termination signal |
| 138 | +// When you'll push your code to Stackhero, we'll send a termination signal (SIGTERM). |
| 139 | +// The goal is to let you close cleanly connections from Express, connections to databases etc... |
| 140 | +process.on('SIGTERM', () => { |
| 141 | + // You'll see this log directly on your Stackhero's console |
| 142 | + console.info('😯 SIGTERM signal received.'); |
| 143 | + |
| 144 | + // Close the server and all connections |
| 145 | + server.close( |
| 146 | + err => { |
| 147 | + if (err) { |
| 148 | + // You'll see this log directly on your Stackhero's console |
| 149 | + console.error(err); |
| 150 | + process.exit(1); |
| 151 | + } |
| 152 | + else { |
| 153 | + // You'll see this log directly on your Stackhero's console |
| 154 | + console.log('👋 Exit the app with status code 0'); |
| 155 | + process.exit(0); |
| 156 | + } |
| 157 | + } |
| 158 | + ); |
| 159 | +}); |
0 commit comments