Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a REST call in Node.js that requires client certificate for authentication

Is there a way to make a rest call that requires a client certificate for authentication through Node.js ?

like image 531
Anand Divakaran Avatar asked May 10 '14 06:05

Anand Divakaran


People also ask

How do I send a client certificate in HTTP request?

The client certificate is sent during the TLS handshake when establishing a connection and can't be sent via HTTP within that connection. The communication is layered like this: HTTP (application-layer protocol) within. TLS (presentation-layer protocol) within.


1 Answers

Yes, you can do that quite simply, here done using a regular https request;

var https = require('https'),                  // Module for https
    fs =    require('fs');                     // Required to read certs and keys

var options = {
    key:   fs.readFileSync('ssl/client.key'),  // Secret client key
    cert:  fs.readFileSync('ssl/client.crt'),  // Public client key
    // rejectUnauthorized: false,              // Used for self signed server
    host: "rest.localhost",                    // Server hostname
    port: 8443                                 // Server port
};

callback = function(response) {
  var str = '';    
  response.on('data', function (chunk) {
    str += chunk;
  });

  response.on('end', function () {
    console.log(str);
  });
}

https.request(options, callback).end();
like image 121
Joachim Isaksson Avatar answered Sep 22 '22 03:09

Joachim Isaksson