auth.js 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  1. 'use strict'
  2. const { test } = require('tap')
  3. const eos = require('end-of-stream')
  4. const Faketimers = require('@sinonjs/fake-timers')
  5. const Client = require('../lib/client')
  6. const { setup, connect, noError, subscribe, subscribeMultiple } = require('./helper')
  7. const aedes = require('../')
  8. test('authenticate successfully a client with username and password', function (t) {
  9. t.plan(4)
  10. const s = noError(setup())
  11. t.teardown(s.broker.close.bind(s.broker))
  12. s.broker.authenticate = function (client, username, password, cb) {
  13. t.type(client, Client, 'client is there')
  14. t.equal(username, 'my username', 'username is there')
  15. t.same(password, Buffer.from('my pass'), 'password is there')
  16. cb(null, true)
  17. }
  18. s.inStream.write({
  19. cmd: 'connect',
  20. protocolId: 'MQTT',
  21. protocolVersion: 4,
  22. clean: true,
  23. clientId: 'my-client',
  24. username: 'my username',
  25. password: 'my pass',
  26. keepalive: 0
  27. })
  28. s.outStream.on('data', function (packet) {
  29. t.same(packet, {
  30. cmd: 'connack',
  31. returnCode: 0,
  32. length: 2,
  33. qos: 0,
  34. retain: false,
  35. dup: false,
  36. topic: null,
  37. payload: null,
  38. sessionPresent: false
  39. }, 'successful connack')
  40. })
  41. })
  42. test('authenticate unsuccessfully a client with username and password', function (t) {
  43. t.plan(6)
  44. const s = setup()
  45. t.teardown(s.broker.close.bind(s.broker))
  46. s.broker.authenticate = function (client, username, password, cb) {
  47. t.type(client, Client, 'client is there')
  48. t.equal(username, 'my username', 'username is there')
  49. t.same(password, Buffer.from('my pass'), 'password is there')
  50. cb(null, false)
  51. }
  52. s.broker.on('clientError', function (client, err) {
  53. t.equal(err.errorCode, 5)
  54. })
  55. s.broker.on('clientReady', function (client) {
  56. t.fail('client should not ready')
  57. })
  58. s.outStream.on('data', function (packet) {
  59. t.same(packet, {
  60. cmd: 'connack',
  61. returnCode: 5,
  62. length: 2,
  63. qos: 0,
  64. retain: false,
  65. dup: false,
  66. topic: null,
  67. payload: null,
  68. sessionPresent: false
  69. }, 'unsuccessful connack, unauthorized')
  70. })
  71. eos(s.outStream, function () {
  72. t.equal(s.broker.connectedClients, 0, 'no connected clients')
  73. })
  74. s.inStream.write({
  75. cmd: 'connect',
  76. protocolId: 'MQTT',
  77. protocolVersion: 4,
  78. clean: true,
  79. clientId: 'my-client',
  80. username: 'my username',
  81. password: 'my pass',
  82. keepalive: 0
  83. })
  84. })
  85. test('authenticate errors', function (t) {
  86. t.plan(7)
  87. const s = setup()
  88. t.teardown(s.broker.close.bind(s.broker))
  89. s.broker.authenticate = function (client, username, password, cb) {
  90. t.type(client, Client, 'client is there')
  91. t.equal(username, 'my username', 'username is there')
  92. t.same(password, Buffer.from('my pass'), 'password is there')
  93. cb(new Error('this should happen!'))
  94. }
  95. s.broker.on('clientError', function (client, err) {
  96. t.equal(err.message, 'this should happen!')
  97. t.equal(err.errorCode, 5)
  98. })
  99. s.broker.on('clientReady', function (client) {
  100. t.fail('client should not ready')
  101. })
  102. s.outStream.on('data', function (packet) {
  103. t.same(packet, {
  104. cmd: 'connack',
  105. returnCode: 5,
  106. length: 2,
  107. qos: 0,
  108. retain: false,
  109. dup: false,
  110. topic: null,
  111. payload: null,
  112. sessionPresent: false
  113. }, 'unsuccessful connack, unauthorized')
  114. })
  115. eos(s.outStream, function () {
  116. t.equal(s.broker.connectedClients, 0, 'no connected clients')
  117. })
  118. s.inStream.write({
  119. cmd: 'connect',
  120. protocolId: 'MQTT',
  121. protocolVersion: 4,
  122. clean: true,
  123. clientId: 'my-client',
  124. username: 'my username',
  125. password: 'my pass',
  126. keepalive: 0
  127. })
  128. })
  129. test('authentication error when return code 1 (unacceptable protocol version) is passed', function (t) {
  130. t.plan(7)
  131. const s = setup()
  132. t.teardown(s.broker.close.bind(s.broker))
  133. s.broker.authenticate = function (client, username, password, cb) {
  134. t.type(client, Client, 'client is there')
  135. t.equal(username, 'my username', 'username is there')
  136. t.same(password, Buffer.from('my pass'), 'password is there')
  137. const error = new Error('Auth error')
  138. error.returnCode = 1
  139. cb(error, null)
  140. }
  141. s.broker.on('clientError', function (client, err) {
  142. t.equal(err.message, 'Auth error')
  143. t.equal(err.errorCode, 5)
  144. })
  145. s.broker.on('clientReady', function (client) {
  146. t.fail('client should not ready')
  147. })
  148. s.outStream.on('data', function (packet) {
  149. t.same(packet, {
  150. cmd: 'connack',
  151. returnCode: 5,
  152. length: 2,
  153. qos: 0,
  154. retain: false,
  155. dup: false,
  156. topic: null,
  157. payload: null,
  158. sessionPresent: false
  159. }, 'unsuccessful connack, unauthorized')
  160. })
  161. eos(s.outStream, function () {
  162. t.equal(s.broker.connectedClients, 0, 'no connected clients')
  163. })
  164. s.inStream.write({
  165. cmd: 'connect',
  166. protocolId: 'MQTT',
  167. protocolVersion: 4,
  168. clean: true,
  169. clientId: 'my-client',
  170. username: 'my username',
  171. password: 'my pass',
  172. keepalive: 0
  173. })
  174. })
  175. test('authentication error when return code 2 (identifier rejected) is passed', function (t) {
  176. t.plan(7)
  177. const s = setup()
  178. t.teardown(s.broker.close.bind(s.broker))
  179. s.broker.authenticate = function (client, username, password, cb) {
  180. t.type(client, Client, 'client is there')
  181. t.equal(username, 'my username', 'username is there')
  182. t.same(password, Buffer.from('my pass'), 'password is there')
  183. const error = new Error('Auth error')
  184. error.returnCode = 2
  185. cb(error, null)
  186. }
  187. s.broker.on('clientError', function (client, err) {
  188. t.equal(err.message, 'Auth error')
  189. t.equal(err.errorCode, 2)
  190. })
  191. s.broker.on('clientReady', function (client) {
  192. t.fail('client should not ready')
  193. })
  194. s.outStream.on('data', function (packet) {
  195. t.same(packet, {
  196. cmd: 'connack',
  197. returnCode: 2,
  198. length: 2,
  199. qos: 0,
  200. retain: false,
  201. dup: false,
  202. topic: null,
  203. payload: null,
  204. sessionPresent: false
  205. }, 'unsuccessful connack, identifier rejected')
  206. })
  207. eos(s.outStream, function () {
  208. t.equal(s.broker.connectedClients, 0, 'no connected clients')
  209. })
  210. s.inStream.write({
  211. cmd: 'connect',
  212. protocolId: 'MQTT',
  213. protocolVersion: 4,
  214. clean: true,
  215. clientId: 'my-client',
  216. username: 'my username',
  217. password: 'my pass',
  218. keepalive: 0
  219. })
  220. })
  221. test('authentication error when return code 3 (Server unavailable) is passed', function (t) {
  222. t.plan(7)
  223. const s = setup()
  224. t.teardown(s.broker.close.bind(s.broker))
  225. s.broker.authenticate = function (client, username, password, cb) {
  226. t.type(client, Client, 'client is there')
  227. t.equal(username, 'my username', 'username is there')
  228. t.same(password, Buffer.from('my pass'), 'password is there')
  229. const error = new Error('Auth error')
  230. error.returnCode = 3
  231. cb(error, null)
  232. }
  233. s.broker.on('clientError', function (client, err) {
  234. t.equal(err.message, 'Auth error')
  235. t.equal(err.errorCode, 3)
  236. })
  237. s.broker.on('clientReady', function (client) {
  238. t.fail('client should not ready')
  239. })
  240. s.outStream.on('data', function (packet) {
  241. t.same(packet, {
  242. cmd: 'connack',
  243. returnCode: 3,
  244. length: 2,
  245. qos: 0,
  246. retain: false,
  247. dup: false,
  248. topic: null,
  249. payload: null,
  250. sessionPresent: false
  251. }, 'unsuccessful connack, Server unavailable')
  252. })
  253. eos(s.outStream, function () {
  254. t.equal(s.broker.connectedClients, 0, 'no connected clients')
  255. })
  256. s.inStream.write({
  257. cmd: 'connect',
  258. protocolId: 'MQTT',
  259. protocolVersion: 4,
  260. clean: true,
  261. clientId: 'my-client',
  262. username: 'my username',
  263. password: 'my pass',
  264. keepalive: 0
  265. })
  266. })
  267. test('authentication error when return code 4 (bad user or password) is passed', function (t) {
  268. t.plan(7)
  269. const s = setup()
  270. t.teardown(s.broker.close.bind(s.broker))
  271. s.broker.authenticate = function (client, username, password, cb) {
  272. t.type(client, Client, 'client is there')
  273. t.equal(username, 'my username', 'username is there')
  274. t.same(password, Buffer.from('my pass'), 'password is there')
  275. const error = new Error('Auth error')
  276. error.returnCode = 4
  277. cb(error, null)
  278. }
  279. s.broker.on('clientError', function (client, err) {
  280. t.equal(err.message, 'Auth error')
  281. t.equal(err.errorCode, 4)
  282. })
  283. s.broker.on('clientReady', function (client) {
  284. t.fail('client should not ready')
  285. })
  286. s.outStream.on('data', function (packet) {
  287. t.same(packet, {
  288. cmd: 'connack',
  289. returnCode: 4,
  290. length: 2,
  291. qos: 0,
  292. retain: false,
  293. dup: false,
  294. topic: null,
  295. payload: null,
  296. sessionPresent: false
  297. }, 'unsuccessful connack, bad username or password')
  298. })
  299. eos(s.outStream, function () {
  300. t.equal(s.broker.connectedClients, 0, 'no connected clients')
  301. })
  302. s.inStream.write({
  303. cmd: 'connect',
  304. protocolId: 'MQTT',
  305. protocolVersion: 4,
  306. clean: true,
  307. clientId: 'my-client',
  308. username: 'my username',
  309. password: 'my pass',
  310. keepalive: 0
  311. })
  312. })
  313. test('authentication error when non numeric return code is passed', function (t) {
  314. t.plan(7)
  315. const s = setup()
  316. t.teardown(s.broker.close.bind(s.broker))
  317. s.broker.authenticate = function (client, username, password, cb) {
  318. t.type(client, Client, 'client is there')
  319. t.equal(username, 'my username', 'username is there')
  320. t.same(password, Buffer.from('my pass'), 'password is there')
  321. const error = new Error('Non numeric error codes')
  322. error.returnCode = 'return Code'
  323. cb(error, null)
  324. }
  325. s.broker.on('clientError', function (client, err) {
  326. t.equal(err.message, 'Non numeric error codes')
  327. t.equal(err.errorCode, 5)
  328. })
  329. s.broker.on('clientReady', function (client) {
  330. t.fail('client should not ready')
  331. })
  332. s.outStream.on('data', function (packet) {
  333. t.same(packet, {
  334. cmd: 'connack',
  335. returnCode: 5,
  336. length: 2,
  337. qos: 0,
  338. retain: false,
  339. dup: false,
  340. topic: null,
  341. payload: null,
  342. sessionPresent: false
  343. }, 'unsuccessful connack, unauthorized')
  344. })
  345. eos(s.outStream, function () {
  346. t.equal(s.broker.connectedClients, 0, 'no connected clients')
  347. })
  348. s.inStream.write({
  349. cmd: 'connect',
  350. protocolId: 'MQTT',
  351. protocolVersion: 4,
  352. clean: true,
  353. clientId: 'my-client',
  354. username: 'my username',
  355. password: 'my pass',
  356. keepalive: 0
  357. })
  358. })
  359. test('authorize publish', function (t) {
  360. t.plan(4)
  361. const s = connect(setup(), { clientId: 'my-client-xyz' })
  362. t.teardown(s.broker.close.bind(s.broker))
  363. const expected = {
  364. cmd: 'publish',
  365. topic: 'hello',
  366. payload: Buffer.from('world'),
  367. qos: 0,
  368. retain: false,
  369. length: 12,
  370. dup: false
  371. }
  372. s.broker.authorizePublish = function (client, packet, cb) {
  373. t.ok(client, 'client exists')
  374. t.same(packet, expected, 'packet matches')
  375. cb()
  376. }
  377. s.broker.mq.on('hello', function (packet, cb) {
  378. t.notOk(Object.prototype.hasOwnProperty.call(packet, 'messageId'), 'should not contain messageId in QoS 0')
  379. expected.brokerId = s.broker.id
  380. expected.brokerCounter = s.broker.counter
  381. expected.clientId = 'my-client-xyz'
  382. delete expected.length
  383. t.same(packet, expected, 'packet matches')
  384. cb()
  385. })
  386. s.inStream.write({
  387. cmd: 'publish',
  388. topic: 'hello',
  389. payload: 'world'
  390. })
  391. })
  392. test('authorize waits for authenticate', function (t) {
  393. t.plan(6)
  394. const s = setup(aedes({ clientId: 'my-client-xyz-2' }))
  395. t.teardown(s.broker.close.bind(s.broker))
  396. s.broker.authenticate = function (client, username, password, cb) {
  397. t.type(client, Client, 'client is there')
  398. process.nextTick(function () {
  399. t.equal(username, 'my username', 'username is there')
  400. t.same(password, Buffer.from('my pass'), 'password is there')
  401. client.authenticated = true
  402. cb(null, true)
  403. })
  404. }
  405. s.broker.authorizePublish = function (client, packet, cb) {
  406. t.ok(client.authenticated, 'client authenticated')
  407. cb()
  408. }
  409. const expected = {
  410. cmd: 'publish',
  411. topic: 'hello',
  412. payload: Buffer.from('world'),
  413. qos: 0,
  414. retain: false,
  415. length: 12,
  416. dup: false,
  417. clientId: 'my-client'
  418. }
  419. s.broker.mq.on('hello', function (packet, cb) {
  420. t.notOk(Object.prototype.hasOwnProperty.call(packet, 'messageId'), 'should not contain messageId in QoS 0')
  421. expected.brokerId = s.broker.id
  422. expected.brokerCounter = s.broker.counter
  423. delete expected.length
  424. t.same(packet, expected, 'packet matches')
  425. cb()
  426. })
  427. s.inStream.write({
  428. cmd: 'connect',
  429. protocolId: 'MQTT',
  430. protocolVersion: 4,
  431. clean: true,
  432. clientId: 'my-client',
  433. username: 'my username',
  434. password: 'my pass',
  435. keepalive: 0
  436. })
  437. s.inStream.write({
  438. cmd: 'publish',
  439. topic: 'hello',
  440. payload: 'world'
  441. })
  442. })
  443. test('authorize publish from configOptions', function (t) {
  444. t.plan(4)
  445. const s = connect(setup(aedes({
  446. clientId: 'my-client-xyz-3',
  447. authorizePublish: function (client, packet, cb) {
  448. t.ok(client, 'client exists')
  449. t.same(packet, expected, 'packet matches')
  450. cb()
  451. }
  452. })), { clientId: 'my-client-xyz-3' })
  453. t.teardown(s.broker.close.bind(s.broker))
  454. const expected = {
  455. cmd: 'publish',
  456. topic: 'hello',
  457. payload: Buffer.from('world'),
  458. qos: 0,
  459. retain: false,
  460. length: 12,
  461. dup: false
  462. }
  463. s.broker.mq.on('hello', function (packet, cb) {
  464. t.notOk(Object.prototype.hasOwnProperty.call(packet, 'messageId'), 'should not contain messageId in QoS 0')
  465. expected.brokerId = s.broker.id
  466. expected.brokerCounter = s.broker.counter
  467. expected.clientId = 'my-client-xyz-3'
  468. delete expected.length
  469. t.same(packet, expected, 'packet matches')
  470. cb()
  471. })
  472. s.inStream.write({
  473. cmd: 'publish',
  474. topic: 'hello',
  475. payload: 'world'
  476. })
  477. })
  478. test('do not authorize publish', function (t) {
  479. t.plan(3)
  480. const s = connect(setup())
  481. t.teardown(s.broker.close.bind(s.broker))
  482. const expected = {
  483. cmd: 'publish',
  484. topic: 'hello',
  485. payload: Buffer.from('world'),
  486. qos: 0,
  487. retain: false,
  488. length: 12,
  489. dup: false
  490. }
  491. s.broker.authorizePublish = function (client, packet, cb) {
  492. t.ok(client, 'client exists')
  493. t.same(packet, expected, 'packet matches')
  494. cb(new Error('auth negated'))
  495. }
  496. eos(s.conn, function () {
  497. t.equal(s.broker.connectedClients, 0, 'no connected clients')
  498. })
  499. s.inStream.write({
  500. cmd: 'publish',
  501. topic: 'hello',
  502. payload: 'world'
  503. })
  504. })
  505. test('modify qos out of range in authorize publish ', function (t) {
  506. t.plan(2)
  507. const s = connect(setup(), { clientId: 'my-client-xyz-4' })
  508. t.teardown(s.broker.close.bind(s.broker))
  509. const expected = {
  510. cmd: 'publish',
  511. topic: 'foo',
  512. payload: Buffer.from('bar'),
  513. qos: 0,
  514. retain: false,
  515. length: 12,
  516. dup: false,
  517. clientId: 'my-client-xyz-4'
  518. }
  519. s.broker.authorizePublish = function (client, packet, cb) {
  520. if (packet.topic === 'hello') { packet.qos = 10 }
  521. cb()
  522. }
  523. s.outStream.on('data', function (packet) {
  524. t.fail('should no data sent')
  525. })
  526. s.broker.mq.on('hello', function (packet, cb) {
  527. t.fail('should not publish')
  528. })
  529. s.broker.mq.on('foo', function (packet, cb) {
  530. t.notOk(Object.prototype.hasOwnProperty.call(packet, 'messageId'), 'should not contain messageId in QoS 0')
  531. expected.brokerId = s.broker.id
  532. expected.brokerCounter = s.broker.counter
  533. delete expected.length
  534. t.same(packet, expected, 'packet matches')
  535. cb()
  536. })
  537. s.inStream.write({
  538. cmd: 'publish',
  539. topic: 'hello',
  540. payload: 'world'
  541. })
  542. s.inStream.write({
  543. cmd: 'publish',
  544. topic: 'foo',
  545. payload: 'bar'
  546. })
  547. })
  548. test('authorize subscribe', function (t) {
  549. t.plan(5)
  550. const s = connect(setup())
  551. t.teardown(s.broker.close.bind(s.broker))
  552. s.broker.authorizeSubscribe = function (client, sub, cb) {
  553. t.ok(client, 'client exists')
  554. t.same(sub, {
  555. topic: 'hello',
  556. qos: 0
  557. }, 'topic matches')
  558. cb(null, sub)
  559. }
  560. subscribe(t, s, 'hello', 0)
  561. })
  562. test('authorize subscribe multiple same topics with same qos', function (t) {
  563. t.plan(4)
  564. const s = connect(setup())
  565. t.teardown(s.broker.close.bind(s.broker))
  566. s.broker.authorizeSubscribe = function (client, sub, cb) {
  567. t.same(sub, {
  568. topic: 'hello',
  569. qos: 0
  570. }, 'topic matches')
  571. cb(null, sub)
  572. }
  573. subscribeMultiple(t, s, [{ topic: 'hello', qos: 0 }, { topic: 'hello', qos: 0 }], [0])
  574. })
  575. test('authorize subscribe multiple same topics with different qos', function (t) {
  576. t.plan(4)
  577. const s = connect(setup())
  578. t.teardown(s.broker.close.bind(s.broker))
  579. s.broker.authorizeSubscribe = function (client, sub, cb) {
  580. t.same(sub, {
  581. topic: 'hello',
  582. qos: 1
  583. }, 'topic matches')
  584. cb(null, sub)
  585. }
  586. subscribeMultiple(t, s, [{ topic: 'hello', qos: 0 }, { topic: 'hello', qos: 1 }], [1])
  587. })
  588. test('authorize subscribe multiple different topics', function (t) {
  589. t.plan(7)
  590. const s = connect(setup())
  591. t.teardown(s.broker.close.bind(s.broker))
  592. s.broker.authorizeSubscribe = function (client, sub, cb) {
  593. t.ok(client, 'client exists')
  594. if (sub.topic === 'hello') {
  595. t.same(sub, {
  596. topic: 'hello',
  597. qos: 0
  598. }, 'topic matches')
  599. } else if (sub.topic === 'foo') {
  600. t.same(sub, {
  601. topic: 'foo',
  602. qos: 0
  603. }, 'topic matches')
  604. }
  605. cb(null, sub)
  606. }
  607. subscribeMultiple(t, s, [{ topic: 'hello', qos: 0 }, { topic: 'foo', qos: 0 }], [0, 0])
  608. })
  609. test('authorize subscribe from config options', function (t) {
  610. t.plan(5)
  611. const s = connect(setup(aedes({
  612. authorizeSubscribe: function (client, sub, cb) {
  613. t.ok(client, 'client exists')
  614. t.same(sub, {
  615. topic: 'hello',
  616. qos: 0
  617. }, 'topic matches')
  618. cb(null, sub)
  619. }
  620. })))
  621. t.teardown(s.broker.close.bind(s.broker))
  622. subscribe(t, s, 'hello', 0)
  623. })
  624. test('negate subscription', function (t) {
  625. t.plan(5)
  626. const s = connect(setup())
  627. t.teardown(s.broker.close.bind(s.broker))
  628. s.broker.authorizeSubscribe = function (client, sub, cb) {
  629. t.ok(client, 'client exists')
  630. t.same(sub, {
  631. topic: 'hello',
  632. qos: 0
  633. }, 'topic matches')
  634. cb(null, null)
  635. }
  636. s.inStream.write({
  637. cmd: 'subscribe',
  638. messageId: 24,
  639. subscriptions: [{
  640. topic: 'hello',
  641. qos: 0
  642. }]
  643. })
  644. s.outStream.once('data', function (packet) {
  645. t.equal(packet.cmd, 'suback')
  646. t.same(packet.granted, [128])
  647. t.equal(packet.messageId, 24)
  648. })
  649. })
  650. test('negate multiple subscriptions', function (t) {
  651. t.plan(6)
  652. const s = connect(setup())
  653. t.teardown(s.broker.close.bind(s.broker))
  654. s.broker.authorizeSubscribe = function (client, sub, cb) {
  655. t.ok(client, 'client exists')
  656. cb(null, null)
  657. }
  658. const expectedSubs = [{
  659. topic: 'hello',
  660. qos: 128
  661. }, {
  662. topic: 'world',
  663. qos: 128
  664. }]
  665. s.broker.once('subscribe', function (subs, client) {
  666. t.same(subs, expectedSubs)
  667. })
  668. s.inStream.write({
  669. cmd: 'subscribe',
  670. messageId: 24,
  671. subscriptions: [{
  672. topic: 'hello',
  673. qos: 0
  674. }, {
  675. topic: 'world',
  676. qos: 0
  677. }]
  678. })
  679. s.outStream.once('data', function (packet) {
  680. t.equal(packet.cmd, 'suback')
  681. t.same(packet.granted, [128, 128])
  682. t.equal(packet.messageId, 24)
  683. })
  684. })
  685. test('negate subscription with correct persistence', function (t) {
  686. t.plan(6)
  687. // rh, rap, nl are undefined because mqtt.parser is set to MQTT 3.1.1 and will thus erase these props from s.inStream.write
  688. const expected = [{
  689. topic: 'hello',
  690. qos: 0,
  691. rh: undefined,
  692. rap: undefined,
  693. nl: undefined
  694. }, {
  695. topic: 'world',
  696. qos: 0,
  697. rh: undefined,
  698. rap: undefined,
  699. nl: undefined
  700. }]
  701. const broker = aedes()
  702. t.teardown(broker.close.bind(broker))
  703. broker.authorizeSubscribe = function (client, sub, cb) {
  704. t.ok(client, 'client exists')
  705. if (sub.topic === 'hello') {
  706. sub = null
  707. }
  708. cb(null, sub)
  709. }
  710. const s = connect(setup(broker), { clean: false, clientId: 'abcde' })
  711. s.outStream.once('data', function (packet) {
  712. t.equal(packet.cmd, 'suback')
  713. t.same(packet.granted, [128, 0])
  714. broker.persistence.subscriptionsByClient(broker.clients.abcde, function (_, subs, client) {
  715. t.same(subs, expected)
  716. })
  717. t.equal(packet.messageId, 24)
  718. })
  719. s.inStream.write({
  720. cmd: 'subscribe',
  721. messageId: 24,
  722. subscriptions: [{
  723. topic: 'hello',
  724. qos: 0,
  725. rh: 0,
  726. rap: true,
  727. nl: false
  728. }, {
  729. topic: 'world',
  730. qos: 0,
  731. rh: 0,
  732. rap: true,
  733. nl: false
  734. }]
  735. })
  736. })
  737. test('negate multiple subscriptions random times', function (t) {
  738. t.plan(5)
  739. const clock = Faketimers.createClock()
  740. const s = connect(setup())
  741. t.teardown(function () {
  742. clock.reset()
  743. s.broker.close()
  744. })
  745. s.broker.authorizeSubscribe = function (client, sub, cb) {
  746. t.ok(client, 'client exists')
  747. if (sub.topic === 'hello') {
  748. clock.setTimeout(function () {
  749. cb(null, sub)
  750. }, 100)
  751. } else {
  752. cb(null, null)
  753. clock.tick(100)
  754. }
  755. }
  756. s.inStream.write({
  757. cmd: 'subscribe',
  758. messageId: 24,
  759. subscriptions: [{
  760. topic: 'hello',
  761. qos: 0
  762. }, {
  763. topic: 'world',
  764. qos: 0
  765. }]
  766. })
  767. s.outStream.once('data', function (packet) {
  768. t.equal(packet.cmd, 'suback')
  769. t.same(packet.granted, [0, 128])
  770. t.equal(packet.messageId, 24)
  771. })
  772. })
  773. test('failed authentication does not disconnect other client with same clientId', function (t) {
  774. t.plan(3)
  775. const broker = aedes()
  776. t.teardown(broker.close.bind(broker))
  777. const s = setup(broker)
  778. const s0 = setup(broker)
  779. broker.authenticate = function (client, username, password, cb) {
  780. cb(null, password.toString() === 'right')
  781. }
  782. s0.inStream.write({
  783. cmd: 'connect',
  784. protocolId: 'MQTT',
  785. protocolVersion: 4,
  786. clean: true,
  787. clientId: 'my-client',
  788. username: 'my username',
  789. password: 'right',
  790. keepalive: 0
  791. })
  792. s0.outStream.on('data', function (packet) {
  793. t.same(packet, {
  794. cmd: 'connack',
  795. returnCode: 0,
  796. length: 2,
  797. qos: 0,
  798. retain: false,
  799. dup: false,
  800. topic: null,
  801. payload: null,
  802. sessionPresent: false
  803. }, 'successful connack')
  804. s.inStream.write({
  805. cmd: 'connect',
  806. protocolId: 'MQTT',
  807. protocolVersion: 4,
  808. clean: true,
  809. clientId: 'my-client',
  810. username: 'my username',
  811. password: 'wrong',
  812. keepalive: 0
  813. })
  814. })
  815. const removeEos = eos(s0.outStream, function () {
  816. t.fail('ended before time')
  817. })
  818. s.outStream.on('data', function (packet) {
  819. t.same(packet, {
  820. cmd: 'connack',
  821. returnCode: 5,
  822. length: 2,
  823. qos: 0,
  824. retain: false,
  825. dup: false,
  826. topic: null,
  827. payload: null,
  828. sessionPresent: false
  829. }, 'unsuccessful connack')
  830. })
  831. eos(s.outStream, function () {
  832. t.pass('ended')
  833. removeEos()
  834. })
  835. })
  836. test('unauthorized connection should not unregister the correct one with same clientId', function (t) {
  837. t.plan(4)
  838. const broker = aedes({
  839. authenticate: function (client, username, password, callback) {
  840. if (username === 'correct') {
  841. callback(null, true)
  842. } else {
  843. const error = new Error()
  844. error.returnCode = 4
  845. callback(error, false)
  846. }
  847. }
  848. })
  849. t.teardown(broker.close.bind(broker))
  850. broker.on('clientError', function (client, err) {
  851. t.equal(err.message, 'bad user name or password')
  852. t.equal(err.errorCode, 4)
  853. t.equal(broker.connectedClients, 1, 'my-client still connected')
  854. })
  855. connect(setup(broker), {
  856. clientId: 'my-client',
  857. username: 'correct'
  858. }, function () {
  859. t.equal(broker.connectedClients, 1, 'my-client connected')
  860. connect(setup(broker), {
  861. clientId: 'my-client',
  862. username: 'unauthorized'
  863. }, function () {
  864. // other unauthorized connection with the same clientId should not unregister the correct one.
  865. t.fail('unauthorized should not connect')
  866. })
  867. })
  868. })
  869. test('set authentication method in config options', function (t) {
  870. t.plan(5)
  871. const s = setup(aedes({
  872. authenticate: function (client, username, password, cb) {
  873. t.type(client, Client, 'client is there')
  874. t.equal(username, 'my username', 'username is there')
  875. t.same(password, Buffer.from('my pass'), 'password is there')
  876. cb(null, false)
  877. }
  878. }))
  879. t.teardown(s.broker.close.bind(s.broker))
  880. s.outStream.on('data', function (packet) {
  881. t.same(packet, {
  882. cmd: 'connack',
  883. returnCode: 5,
  884. length: 2,
  885. qos: 0,
  886. retain: false,
  887. dup: false,
  888. topic: null,
  889. payload: null,
  890. sessionPresent: false
  891. }, 'unsuccessful connack')
  892. })
  893. eos(s.outStream, function () {
  894. t.equal(s.broker.connectedClients, 0, 'no connected clients')
  895. })
  896. s.inStream.write({
  897. cmd: 'connect',
  898. protocolId: 'MQTT',
  899. protocolVersion: 4,
  900. clean: true,
  901. clientId: 'my-client',
  902. username: 'my username',
  903. password: 'my pass',
  904. keepalive: 0
  905. })
  906. })
  907. test('change a topic name inside authorizeForward method in QoS 1 mode', function (t) {
  908. t.plan(3)
  909. const broker = aedes({
  910. authorizeForward: function (client, packet) {
  911. packet.payload = Buffer.from('another-world')
  912. packet.messageId = 2
  913. return packet
  914. }
  915. })
  916. t.teardown(broker.close.bind(broker))
  917. const expected = {
  918. cmd: 'publish',
  919. topic: 'hello',
  920. payload: Buffer.from('another-world'),
  921. dup: false,
  922. length: 22,
  923. qos: 1,
  924. retain: false,
  925. messageId: 2
  926. }
  927. broker.on('client', function (client) {
  928. client.subscribe({
  929. topic: 'hello',
  930. qos: 1
  931. }, function (err) {
  932. t.error(err, 'no error')
  933. broker.publish({
  934. topic: 'hello',
  935. payload: Buffer.from('world'),
  936. qos: 1
  937. }, function (err) {
  938. t.error(err, 'no error')
  939. })
  940. })
  941. })
  942. const s = connect(setup(broker))
  943. s.outStream.once('data', function (packet) {
  944. t.same(packet, expected, 'packet matches')
  945. })
  946. })
  947. ;[true, false].forEach(function (cleanSession) {
  948. test(`unauthorized forward publish in QoS 1 mode [clean=${cleanSession}]`, function (t) {
  949. t.plan(2)
  950. const broker = aedes({
  951. authorizeForward: function (client, packet) {
  952. return null
  953. }
  954. })
  955. t.teardown(broker.close.bind(broker))
  956. broker.on('client', function (client) {
  957. client.subscribe({
  958. topic: 'hello',
  959. qos: 1
  960. }, function (err) {
  961. t.error(err, 'no error')
  962. broker.publish({
  963. topic: 'hello',
  964. payload: Buffer.from('world'),
  965. qos: 1
  966. }, function (err) {
  967. t.error(err, 'no error')
  968. })
  969. })
  970. })
  971. const s = connect(setup(broker), { clean: cleanSession })
  972. s.outStream.once('data', function (packet) {
  973. t.fail('Should have not recieved this packet')
  974. })
  975. })
  976. })
  977. test('prevent publish in QoS 0 mode', function (t) {
  978. t.plan(2)
  979. const broker = aedes({
  980. authorizeForward: function (client, packet) {
  981. return null
  982. }
  983. })
  984. t.teardown(broker.close.bind(broker))
  985. broker.on('client', function (client) {
  986. client.subscribe({
  987. topic: 'hello',
  988. qos: 0
  989. }, function (err) {
  990. t.error(err, 'no error')
  991. broker.publish({
  992. topic: 'hello',
  993. payload: Buffer.from('world'),
  994. qos: 0
  995. }, function (err) {
  996. t.error(err, 'no error')
  997. })
  998. })
  999. })
  1000. const s = connect(setup(broker))
  1001. s.outStream.once('data', function (packet) {
  1002. t.fail('Should have not recieved this packet')
  1003. })
  1004. })