Brad Schoening
Brad Schoening

Reputation: 1381

Axios with Mutual TLS: how to provide credentials

In a browser, I can load Axios from a CDN with a script tag:

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

However, the class https.Agent, used for mutual TLS, is not present and it is not clear how to load it. My code is roughly:

const httpsAgent = new https.Agent({
  rejectUnauthorized: false, 
  cert: ...,
  key: ...
})
....
axios.post('https://....', {...}, { httpsAgent } )

Error in Console: Uncaught ReferenceError: https is not defined

Local examples often show require (below), but https should be loadable from a CDN like unpkg?

const https = require('https');
const axios = require("axios");

(PS: its useless to google for 'https')

Upvotes: 1

Views: 3571

Answers (1)

Brad Schoening
Brad Schoening

Reputation: 1381

I resolved this using client browser certificates. When the web server has SSL client verification enabled, it will request the clients certificate. With this technique, the cert is passed out of band and not with the Axios post() call.

On Nginx, this can be configured with ssl_verify_client and ssl_trusted_certificate which determines the CA(s) used to verify the certificate. During the initial TLS exchange, a popup in the users browser will prompt them to select the certificate to send.

Users must configure a client .p12 (combined cert and key) in your browser. On Chrome:

Chrome Preferences > Privacy and Security (Additional Settings) > Manage Certificates

HTTPS request can then be made without explicit auth:

    axios.post('https:....., dataArg)

Upvotes: 1

Related Questions