Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phuongtailtranminh/a6ea6811362cdc0b4b2b74b8edb98581 to your computer and use it in GitHub Desktop.
Save phuongtailtranminh/a6ea6811362cdc0b4b2b74b8edb98581 to your computer and use it in GitHub Desktop.
SSL Pinning Axios
const https = require('https');
const axios = require('axios');
const options = {
rejectUnauthorized: true,
checkServerIdentity: function(host, cert) {
// // Make sure the certificate is issued to the host we are connected to
// const err = tls.checkServerIdentity(host, cert);
// if (err) {
// return err;
// }
// // Pin the public key, similar to HPKP pin-sha25 pinning
// const pubkey256 = 'Ues/xSDzybWMEgs2jgMiz7GJUgX1dYZ6gKFXJso8nXU=';
// if (sha256(cert.pubkey) !== pubkey256) {
// return new Error('Certificate verification error');
// }
// OR Pin the exact certificate, rather than the pub key
const cert256 = 'E2:C6:8E:11:77:1E:6F:46:8B:A5:2F:21:4C:EF:57:6B:62:6B:9F:10:30:75:00:EB:DB:28:02:85:79:52:3D:CF';
if (cert.fingerprint256 !== cert256) {
return new Error('Certificate verification error');
}
},
};
const agent = new https.Agent(options);
axios.get('https://test-jb.sc.com/test/user-account/config', { httpsAgent: agent })
.then(response => {
console.log('All OK. Server matched our pinned cert or public key')
})
.catch(error => {
console.error(error.message)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment