11// this server is intended to be used with https://github.com/eclipse/iottestware
22
33import {
4- AuthenticationResult ,
5- Context ,
6- MqttServer ,
7- Topic ,
4+ AuthenticationResult ,
5+ Context ,
6+ MqttServer ,
7+ Topic ,
88} from "../server/mod.ts" ;
99import { logger } from "../utils/utils.ts" ;
1010
@@ -18,61 +18,61 @@ const notAuthorizedTable = new Set();
1818notAuthorizedTable . add ( [ "123-456-789" , "eclipse/iot/tesware/0data" ] ) ;
1919
2020function isAuthenticated (
21- _ctx : Context ,
22- clientId : string ,
23- username : string ,
24- password : Uint8Array ,
21+ _ctx : Context ,
22+ clientId : string ,
23+ username : string ,
24+ password : Uint8Array ,
2525) : AuthenticationResult {
26- const pwd = utf8Decoder . decode ( password ) ;
27- logger . debug (
28- `Verifying authentication of client '${ clientId } ' with username '${ username } ' and password '${ pwd } '` ,
29- ) ;
26+ const pwd = utf8Decoder . decode ( password ) ;
27+ logger . debug (
28+ `Verifying authentication of client '${ clientId } ' with username '${ username } ' and password '${ pwd } '` ,
29+ ) ;
3030
31- if ( ! userTable . has ( username ) ) {
32- if ( ! strictUsername . test ( username ) ) {
33- return AuthenticationResult . badUsernameOrPassword ;
34- }
35- }
36- const pass = userTable . get ( username ) ;
37- if ( pwd === pass ) {
38- return AuthenticationResult . ok ;
39- }
40- return AuthenticationResult . notAuthorized ;
31+ if ( ! userTable . has ( username ) ) {
32+ if ( ! strictUsername . test ( username ) ) {
33+ return AuthenticationResult . badUsernameOrPassword ;
34+ }
35+ }
36+ const pass = userTable . get ( username ) ;
37+ if ( pwd === pass ) {
38+ return AuthenticationResult . ok ;
39+ }
40+ return AuthenticationResult . notAuthorized ;
4141}
4242
4343function isAuthorizedToPublish ( ctx : Context , topic : Topic ) : boolean {
44- logger . debug (
45- `Checking authorization of client '${ ctx . store ?. clientId } ' to publish on topic '${ topic } '` ,
46- ) ;
47- // if (notAuthorizedTable.has([ctx.client,topic])){
48- // return false;
49- // }
50- return true ;
44+ logger . debug (
45+ `Checking authorization of client '${ ctx . store ?. clientId } ' to publish on topic '${ topic } '` ,
46+ ) ;
47+ // if (notAuthorizedTable.has([ctx.client,topic])){
48+ // return false;
49+ // }
50+ return true ;
5151}
5252function isAuthorizedToSubscribe ( ctx : Context , topic : Topic ) : boolean {
53- logger . debug (
54- `Checking authorization of client '${ ctx . store ?. clientId } ' to subscribe to topic '${ topic } '` ,
55- ) ;
56- return true ;
53+ logger . debug (
54+ `Checking authorization of client '${ ctx . store ?. clientId } ' to subscribe to topic '${ topic } '` ,
55+ ) ;
56+ return true ;
5757}
5858
5959/** MQTT server */
6060const port = Number ( Deno . args [ 0 ] ) || 1883 ;
6161const hostname = localhost ;
6262const listener = Deno . listen ( { hostname, port } ) ;
6363const mqttServer = new MqttServer ( {
64- handlers : {
65- isAuthenticated,
66- isAuthorizedToPublish,
67- isAuthorizedToSubscribe,
68- } ,
64+ handlers : {
65+ isAuthenticated,
66+ isAuthorizedToPublish,
67+ isAuthorizedToSubscribe,
68+ } ,
6969} ) ;
7070if ( listener . addr . transport === "tcp" ) {
71- logger . info (
72- `MQTT server is running on hostname: "${ listener . addr . hostname } " port:${ listener . addr . port } ` ,
73- ) ;
71+ logger . info (
72+ `MQTT server is running on hostname: "${ listener . addr . hostname } " port:${ listener . addr . port } ` ,
73+ ) ;
7474}
7575
7676for await ( const conn of listener ) {
77- mqttServer . serve ( conn ) ;
77+ mqttServer . serve ( conn ) ;
7878}
0 commit comments