paho.mqtt.javascript icon indicating copy to clipboard operation
paho.mqtt.javascript copied to clipboard

Question: Is it possible to use client certificate for authentication in paho-mqtt javascript client?

Open bharanidharan81 opened this issue 7 years ago • 8 comments

From the application point of view, you will have only one option (set useSSL to true). Then Paho JavaScript client will establish a secure websocket connection (i.e. wss://) . For two authentication, client requires a certificate to establish the connection.

Is it possible to use client authentication with a certificate instead of username/password with the Paho javascript client ? If so, how? please share the tutorials and documentation to proceed.

bharanidharan81 avatar Jan 17 '19 05:01 bharanidharan81

x2

acervantess avatar Jun 28 '19 15:06 acervantess

I have the same question

KateJC avatar Sep 23 '19 03:09 KateJC

@bharanidharan81 Any update on this issue? Have you found the solution?? Is there anyone who has a solution as I'm also looking for the same.

gathasite avatar Feb 26 '20 13:02 gathasite

looking for the solution

nijisakai avatar Mar 03 '20 12:03 nijisakai

I solved it by using browserMqtt.js based on MQTT.js https://www.npmjs.com/package/mqtt

I got the browserMqtt.js by these steps:

npm install mqtt --save
npm install -g browserify
npm install -g webpack-cli
npm install -g webpack@4
cd node_modules/mqtt
npm install . 
browserify mqtt.js -s mqtt > browserMqtt.js
webpack mqtt.js ./browserMqtt.js --output-library mqtt

the server side is using mosquitto the client side config using cert file is

<script src="./browserMqtt.js"></script>
<script>
            // var KEY = '/var/www/html/test02/client-key.key';
            // var CERT = '/var/www/html/test02/client-cert.crt';
            // var TRUSTED_CA_LIST = '/var/www/html/test02/cacert.crt';
            // var PORT = '8883';
            // var HOST = '159.210.65.6';
            var options = {
                port: '8081',
                host: '159.210.65.6',
                keyPath: '/var/www/html/test02/client-key.pem',
                certPath: '/var/www/html/test02/client-cert.pem',
                rejectUnauthorized : false, 
                //The CA list will be used to determine if server is authorized
                ca: ['/var/www/html/test02/cacert.pem'],
                protocol: 'wss',
                protocolId: 'MQTT',
                // username: 'qqq',
                // password: 'bbb',
                clientId: 'mqttjs_' + Math.random().toString(16).substr(2, 8)
            };

            var client = mqtt.connect(options);

            client.subscribe('messages');
            client.publish('messages', 'Current time is: ' + new Date());
            client.on('message', function(topic, message) {
            console.log(message);
            });

            client.on('connect', function(){
                console.log('Connected');
            });    
                    
</script>

nijisakai avatar Mar 04 '20 10:03 nijisakai

From the application point of view, you will have only one option (set useSSL to true). Then Paho JavaScript client will establish a secure websocket connection (i.e. wss://) . For two authentication, client requires a certificate to establish the connection.

Is it possible to use client authentication with a certificate instead of username/password with the Paho javascript client ? If so, how? please share the tutorials and documentation to proceed.

have found a response ? I try to establish connection with client certificate in react native app

KrifaYounes avatar May 19 '20 23:05 KrifaYounes

I solved it by using browserMqtt.js based on MQTT.js https://www.npmjs.com/package/mqtt

I got the browserMqtt.js by these steps:

npm install mqtt --save
npm install -g browserify
npm install -g webpack-cli
npm install -g webpack@4
cd node_modules/mqtt
npm install . 
browserify mqtt.js -s mqtt > browserMqtt.js
webpack mqtt.js ./browserMqtt.js --output-library mqtt

the server side is using mosquitto the client side config using cert file is

<script src="./browserMqtt.js"></script>
<script>
            // var KEY = '/var/www/html/test02/client-key.key';
            // var CERT = '/var/www/html/test02/client-cert.crt';
            // var TRUSTED_CA_LIST = '/var/www/html/test02/cacert.crt';
            // var PORT = '8883';
            // var HOST = '159.210.65.6';
            var options = {
                port: '8081',
                host: '159.210.65.6',
                keyPath: '/var/www/html/test02/client-key.pem',
                certPath: '/var/www/html/test02/client-cert.pem',
                rejectUnauthorized : false, 
                //The CA list will be used to determine if server is authorized
                ca: ['/var/www/html/test02/cacert.pem'],
                protocol: 'wss',
                protocolId: 'MQTT',
                // username: 'qqq',
                // password: 'bbb',
                clientId: 'mqttjs_' + Math.random().toString(16).substr(2, 8)
            };

            var client = mqtt.connect(options);

            client.subscribe('messages');
            client.publish('messages', 'Current time is: ' + new Date());
            client.on('message', function(topic, message) {
            console.log(message);
            });

            client.on('connect', function(){
                console.log('Connected');
            });    
                    
</script>

@nijisakai is this working, can we use this

aravindarc avatar Jul 12 '20 03:07 aravindarc

I solved it by using browserMqtt.js based on MQTT.js https://www.npmjs.com/package/mqtt I got the browserMqtt.js by these steps:

npm install mqtt --save
npm install -g browserify
npm install -g webpack-cli
npm install -g webpack@4
cd node_modules/mqtt
npm install . 
browserify mqtt.js -s mqtt > browserMqtt.js
webpack mqtt.js ./browserMqtt.js --output-library mqtt

the server side is using mosquitto the client side config using cert file is

<script src="./browserMqtt.js"></script>
<script>
            // var KEY = '/var/www/html/test02/client-key.key';
            // var CERT = '/var/www/html/test02/client-cert.crt';
            // var TRUSTED_CA_LIST = '/var/www/html/test02/cacert.crt';
            // var PORT = '8883';
            // var HOST = '159.210.65.6';
            var options = {
                port: '8081',
                host: '159.210.65.6',
                keyPath: '/var/www/html/test02/client-key.pem',
                certPath: '/var/www/html/test02/client-cert.pem',
                rejectUnauthorized : false, 
                //The CA list will be used to determine if server is authorized
                ca: ['/var/www/html/test02/cacert.pem'],
                protocol: 'wss',
                protocolId: 'MQTT',
                // username: 'qqq',
                // password: 'bbb',
                clientId: 'mqttjs_' + Math.random().toString(16).substr(2, 8)
            };

            var client = mqtt.connect(options);

            client.subscribe('messages');
            client.publish('messages', 'Current time is: ' + new Date());
            client.on('message', function(topic, message) {
            console.log(message);
            });

            client.on('connect', function(){
                console.log('Connected');
            });    
                    
</script>

@nijisakai is this working, can we use this

It works fine with my project

nijisakai avatar Jun 03 '21 07:06 nijisakai