Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can use in Node.js with client certificate for security #159

Open
mhf-ir opened this issue Jun 10, 2023 · 2 comments
Open

How can use in Node.js with client certificate for security #159

mhf-ir opened this issue Jun 10, 2023 · 2 comments

Comments

@mhf-ir
Copy link

mhf-ir commented Jun 10, 2023

Description

I need to full control client in nodejs. server could be behind nginx with mutual tls validation.
using httpsAgent

Steps to reproduce

how can i add in Node.js ENV add ca client-cert and client-key?

Expected Behavior

axios client certificate supported fully

Actual Behavior

there is no documentation for that. Even in server hardening configuration.

@jasonbosco
Copy link
Member

Since axios supports global defaults, could you try doing something like this in your code:

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

// ...
const httpsAgent = new https.Agent({
  cert: fs.readFileSync('client.crt'),
  key: fs.readFileSync('client.key'),
  ca: fs.readFileSync('ca.crt'),
});

axios.defaults.httpsAgent = httpsAgent;

@mhf-ir
Copy link
Author

mhf-ir commented Jun 14, 2023

options must pass as instance of http client. i think could be extra options that merge all axios options might be needed for any usecase. why not?
global setting for entire library not good practice i think.
specially when you using axios other part of your project.

however it's good to replace axios with native JS ecosystem fetch:
https://caniuse.com/fetch
And since version v16.15.0 added:
https://nodejs.org/dist/latest-v16.x/docs/api/globals.html#fetch

for older browser: https://github.com/github/fetch (just be part of document)

<script>
if (!('fetch' in window)){
  document.write('<script src="https://cdn.jsdelivr.net/npm/whatwg-fetch@3.6.2/dist/fetch.umd.min.js"></script>');
}
</script>

many developer not care about older browser. could be enough

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants