• Stars
    star
    115
  • Rank 305,000 (Top 7 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 6 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Demo for Client Certificate Authentication with Node.js Tutorial

Client Certificate Authentication (mTLS) with Node.js

This is demo on how to do client authentication with certificates, mTLS or mutual TLS - as opposed to username and passwords with out of the box (OOTB) Node.js.

This demo has a server with two clients:

  • "Alice" who has a server-signed trusted certificate
  • "Bob" who has an invalid self-signed certificate

Diagram

Based on the following tutorials:

Demo: How to Use

First install required dependencies with npm install. Then the demo works as follows:

Step 1 - Start Server

We start a sever that by default only accepts requests authenticated by client certificates

npm run server

You can test this is working by opening https://localhost:4433/ in your browser.

Step 2 - Test Valid Client (Alice)

Alice has a valid certificate issued by server, so she can talk to the server:

$ npm run valid-client

> node ./client/valid-app.js

Hello Alice, your certificate was issued by localhost!

Step 3 - Test Invalid Client (Bob)

Bob has a self-issued certificate, which is rejected by the server:

$ npm run invalid-client

> node ./client/invalid-app.js

Sorry Bob, certificates from Bob are not welcome here.

Reference - Introduction to Creating Certificates

Server Certificates

  • CN: localhost
  • O: Client Certificate Demo
openssl req \
	-x509 \
	-newkey rsa:4096 \
	-keyout server/server_key.pem \
	-out server/server_cert.pem \
	-nodes \
	-days 365 \
	-subj "/CN=localhost/O=Client\ Certificate\ Demo"

This command shortens following three commands:

  • openssl genrsa
  • openssl req
  • openssl x509

which generates two files:

  • server_cert.pem
  • server_key.pem

Create Client Certificates

For demo, two users are created:

  • Alice, who has a valid certificate, signed by the server
  • Bob, who creates own certificate, self-signed

Create Alice's Certificate (server-signed and valid)

We create a certificate for Alice.

  • sign alice's Certificate Signing Request (CSR)...
  • with our server key via -CA server/server_cert.pem and -CAkey server/server_key.pem flags
  • and save results as certificate
# generate server-signed (valid) certifcate
openssl req \
	-newkey rsa:4096 \
	-keyout client/alice_key.pem \
	-out client/alice_csr.pem \
	-nodes \
	-days 365 \
	-subj "/CN=Alice"

# sign with server_cert.pem
openssl x509 \
	-req \
	-in client/alice_csr.pem \
	-CA server/server_cert.pem \
	-CAkey server/server_key.pem \
	-out client/alice_cert.pem \
	-set_serial 01 \
	-days 365

Create Bob's Certificate (self-signed and invalid)

Bob creates own without our server key.

# generate self-signed (invalid) certifcate
openssl req \
	-newkey rsa:4096 \
	-keyout client/bob_key.pem \
	-out client/bob_csr.pem \
	-nodes \
	-days 365 \
	-subj "/CN=Bob"

# sign with bob_csr.pem
openssl x509 \
	-req \
	-in client/bob_csr.pem \
	-signkey client/bob_key.pem \
	-out client/bob_cert.pem \
	-days 365

Notes

  • Let's Encrypt is a "free, automated, and open" Certificate Authority
  • PEM: Privacy Enhanced Mail is a Base64 encoded DER certificate

OpenSSL commands

Command Documentation Description
genrsa Docs Generates an RSA private key
req Docs Primarily creates and processes certificate requests in PKCS#10 format. It can additionally create self signed certificates for use as root CAs for example.
x509 Docs The x509 command is a multi purpose certificate utility. It can be used to display certificate information, convert certificates to various forms, sign certificate requests like a "mini CA" or edit certificate trust settings.

View all openssl commands β†’

More Repositories

1

newtonjs-graph

Cloud Architecture Graphs for Humans
JavaScript
95
star
2

azure-nodejs-demo

Containerized Node.js Demo App for Azure App Service
JavaScript
73
star
3

azure-pipelines-monorepo

Example of a multi-pipeline monorepo with Azure Pipelines
JavaScript
61
star
4

cloudkube-aks-clusters

3 Clusters, 1 Repo. Opinionated infrastructure as code for my Azure Kubernetes clusters for running demo apps.
HCL
33
star
5

cloud-architecture-review

Cloud Architecture Review App
Vue
28
star
6

wordpress-on-azure

Reference Cloud Native Architecture for Wordpress on Azure App Service
PHP
25
star
7

tidy-jsdoc

A clean JSDoc3 template
JavaScript
25
star
8

windows-dev-setup

Dev environment on windows with Oh My ZSH, Hyper.js and more
Shell
19
star
9

gmail-first-gutters-demo

HTML
17
star
10

antwort-cli

CLI for Automated Email Development, incl. template generation and inlining CSS
Ruby
11
star
11

tidy-revealjs

A custom theme and build system for reveal.js
CSS
10
star
12

cloudkube-shared-infra

Shared Infra for cloudkube.io projects. And example of how to leverage InnerSource example for shared configuration management.
HCL
10
star
13

cosmosdb-mongoose-example

Azure examples for Cosmos DB with Node.js and mongoose incl. cost optimization
JavaScript
9
star
14

service-principal-keyvault-demo

Infra as Code example of creating an Azure service principal and storing its credentials in Key Vault.
HCL
8
star
15

newtonjs-talk-slides

Slides for "Visualizing cloud architectures in real time with d3.js"
HTML
8
star
16

lowercase-linter

GitHub Action to ensure files are lower case only. Suggests valid names in kebab-case format.
JavaScript
8
star
17

hello-welt

Simple containerized Node.js app for demos.
JavaScript
6
star
18

azure-pipelines-templates

Re-usable Azure Pipelines templates and examples
6
star
19

standard-healthcheck

Simple health check endpoint for Express per IETF response format
JavaScript
6
star
20

express-starter

A custom node starter template with preconfigured linting, unit tests and ci pipeilnes
JavaScript
5
star
21

angular-starter

An angular starter template with integrations for Accessibility testing, angular-redux, Jenkins CI and Travis CI.
TypeScript
5
star
22

azure-openid-connect-demo

OpenID Connect demo with Azure Active Directory
JavaScript
4
star
23

nexus-private-npm-registry

Run a local npm registry for private modules and mirror npmjs.org
Shell
4
star
24

azure-terraform-cli

A lean Docker image with Terraform CLI, Azure CLI including devops extension pre-installed. Can be used as CI/CD agent.
Dockerfile
3
star
25

julie-ng

3
star
26

azure-terraform-workspaces

Terraform templates to create workspaces incl. service principals in individual resource groups
HCL
2
star
27

azure-infra-as-code-comparison

Comparing Infrastructure as Code options for Azure. Use Case - leveraging Azure Image Builder and Shared Image Gallery to create custom Virtual Machines
Bicep
2
star
28

jekyll-hub-template

A jekyll template for creating client project hubs a la 24ways.org
CSS
2
star
29

arm-what-if-exit-code-test

2
star
30

azure-pipelines-acr-example

Example Azure DevOps Pipeline for building and deploying containers
JavaScript
2
star
31

azure-pipelines-yaml-trigger

1
star
32

finetune-devops-for-people

Slides for my 20022 DevOps.js Talk - Fine-tuning DevOps for People over Perfection
JavaScript
1
star
33

azure-jenkins-master

Docker for Jenkins Master pre-configured with Azure plugins and opinionated secure defaults
Dockerfile
1
star
34

cloudkube-example-sandbox

Infra as Code for Sandbox Demo
HCL
1
star
35

openid-demo-frontend

JavaScript
1
star
36

angular-on-azure

HTML
1
star
37

azure-nuxtjs-webapp

Nuxt.js demo used also for App Service Deployment comparison: git vs zip deploy
Vue
1
star
38

newtonjs-demo-data-editor

Graph data editor for newton.js demos
JavaScript
1
star
39

cloudkube-networking-iac

Separate IaC repo for networking
HCL
1
star
40

cloudkube-ui

Shared CSS styles for demos to be imported by other projects with npm. Multi-repo example.
SCSS
1
star
41

azure-openid-workshop

Hands-On Workshop on implementing OpenID Connect with Azure Active Directory
1
star
42

appgw-aca-internal-debug

HCL
1
star