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

Improved error response for RESTful applications. #9

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,51 +1,49 @@
client-certificate-auth
client-certificate-auth-v2
========

middleware for Node.js implementing client SSL certificate
authentication/authorization

Copyright © 2013 Tony Gies
Improved Error Responses for RESTful Applications

April 30, 2013

[![Build Status](https://travis-ci.org/tgies/client-certificate-auth.png)](https://travis-ci.org/tgies/client-certificate-auth)
Original Work by [Tony Gies](https://github.com/tgies/client-certificate-auth)

installing
----------

client-certificate-auth is available from [npm](https://npmjs.org/package/client-certificate-auth.).
client-certificate-auth-v2 is available from [npm](https://npmjs.org/package/client-certificate-auth-v2.).

$ npm install client-certificate-auth
$ npm install client-certificate-auth-v2

requirements
------------

client-certificate-auth is tested against Node.js versions 0.6, 0.8, and 0.10.
client-certificate-auth-v2 is tested against Node.js versions 0.6, 0.8, 0.10 and 6.x.x.
It has no external dependencies (other than any middleware framework with which
you may wish to use it); however, to run the tests, you will need [mocha](https://npmjs.org/package/mocha) and
[should](https://npmjs.org/package/should).

synopsis
--------

client-certificate-auth provides HTTP middleware for Node.js (in particular
client-certificate-auth-v2 provides HTTP middleware for Node.js (in particular
Connect/Express) to require that a valid, verifiable client SSL certificate is
provided, and passes information about that certificate to a callback which must
return `true` for the request to proceed; otherwise, the client is considered
return `true` for the request to proceed; otherwise, the client is considered
unauthorized and the request is aborted.

usage
-----

The https server must be set up to request a client certificate and validate it
The https server must be set up to request a client certificate and validate it
against an issuer/CA certificate. What follows is a typical example using
[Express](http://expressjs.com):

```javascript
var express = require('express');
var fs = require('fs');
var https = require('https');
var clientCertificateAuth = require('client-certificate-auth');
var clientCertificateAuth = require('client-certificate-auth-v2');

var opts = {
// Server SSL private key and certificate
Expand Down Expand Up @@ -112,3 +110,6 @@ function checkAuth(cert, callback) {

app.use(checkAuth);
```

<strong>Note:</strong>
`If you are using this module for Client Side Certificate Authentication then inside opts variable for `<strong>cert</strong>` use a chained certificate and in `<strong>ca</strong>` use your custom CA which you have used to sign the client certificate.`
28 changes: 11 additions & 17 deletions lib/clientCertificateAuth.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/*!
* client-certificate-auth - node.js Connect/Express middleware to perform
* client-certificate-auth-v2 - node.js Connect/Express middleware to perform
* authentication based on a client SSL certificate
* Copyright (C) 2013 Tony Gies
* MIT License
*/

/**
/**
* Enforce SSL client authorization and provide a `callback(cert)` which will
* be passed the client certificate information (as obtained through
* `req.connection.getPeerCertificate()`) for additional validation, e.g. to
Expand All @@ -21,36 +21,30 @@ module.exports = function clientCertificateAuth(callback) {
if (!req.secure && req.header('x-forwarded-proto') != 'https') {
return res.redirect('https://' + req.header('host') + req.url);
}

// Ensure that the certificate was validated at the protocol level
if (!req.client.authorized) {
var e = new Error('Unauthorized: Client certificate required ' +
'(' + req.client.authorizationError + ')');
e.status = 401;
return next(e);
var e = req.client.authorizationError;
return res.status(401).send(e);
}

// Obtain certificate details
var cert = req.connection.getPeerCertificate();
if (!cert || !Object.keys(cert).length) {
// Handle the bizarre and probably not-real case that a certificate was
// validated but we can't actually inspect it
var e = new Error('Client certificate was authenticated but certificate ' +
'information could not be retrieved.');
e.status = 500;
return next(e);
var e = 'Client certificate was authenticated but certificate ' + 'information could not be retrieved.';

return res.status(500).send(e);
}

function doneAuthorizing(authorized) {
if (authorized) {
return next();
} else {
var e = new Error('Unauthorized');
e.status = 401;
return next(e);
var e = 'Unauthorized';
return res.status(401).send(e);
}
}

// console.log(callback);
// Fire the callback. If it returns true, the request may proceed. If it
// returns false, bail out with a 401 Unauthorized.
if (callback.length === 2) {
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "client-certificate-auth",
"version": "0.3.0",
"name": "client-certificate-auth-v2",
"version": "0.3.4",
"description": "middleware for Node.js implementing client SSL certificate authentication/authorization",
"homepage": "https://github.com/tgies/client-certificate-auth",
"homepage": "https://github.com/abhishek2244/client-certificate-auth",
"bugs": {
"url": "https://github.com/tgies/client-certificate-auth/issues"
"url": "https://github.com/abhishek2244/client-certificate-auth/issues"
},
"main": "index.js",
"engines": {
Expand All @@ -23,7 +23,7 @@
},
"repository": {
"type": "git",
"url": "https://github.com/tgies/client-certificate-auth.git"
"url": "https://github.com/abhishek2244/client-certificate-auth.git"
},
"keywords": [
"authentication",
Expand All @@ -32,7 +32,7 @@
"connect",
"middleware"
],
"author": "Tony Gies <tony.gies@gruppe86.net> (https://github.com/tgies)",
"author": "Abhishek Singh <vamp2244@gmail.com> (https://github.com/abhishek2244)",
"license": "MIT",
"readmeFilename": "README.md"
}