alax / jsrp

JavaScript implementation of the Secure Remote Password protocol
MIT License
70 stars 24 forks source link

Synopsis

JSRP is a pure JavaScript implementation of SRP-6A, the Secure Remote Password protocol, as defined in RFC 2945. It can be used in Node.js or the browser via Browserify. It uses SHA-256 by default for hashing, although it will support any of Node's hashing functions. It currently supports 2048 and 4096 bit parameters.

Motivation

JSRP was written to make SRP simple to implement and work with from the browser and on the server. All high-level functions return hex strings that are easy to pass between server and client, as well as save. No need to waste time serializing and unserializing objects just to transmit them over the network.

Installation

To use JSRP in the browser, first add the script: (it can be downloaded from the Releases page.)

<script type="text/javascript" src="https://github.com/alax/jsrp/raw/master/jsrp-browser.js"></script>

To use JSRP in Node, simply run:

npm install jsrp

Usage

Browser

JSRP will be available from the jsrp global.

Node

Just require the module:

var jsrp = require('jsrp');

Example

The example will run in Node or the browser, JSRP is completely compatible with both.

Example Registration Process:

var client = new jsrp.client();

client.init({ username: 'testUser', password: 'password123' }, function () {
    client.createVerifier(function(err, result) {
        // result will contain the necessary values the server needs to
        // authenticate this user in the future.
        sendSaltToServer(result.salt);
        sendVerifierToServer(result.verifier);
    });
});

Example Login Process: (normally client and server wouldn't be in the same code, but the example is this way for the sake of brevity)

var client = new jsrp.client();
var server = new jsrp.server();

client.init({ username: 'username', password: 'password123' }, function() {
    // Client instance is ready to be used here.
});

server.init({ salt: 'LONG_HEX_VALUE', verifier: 'EVEN_LONGER_HEX_VALUE' }, function () {
    // Server instance is ready to be used here.
});

// Remember, both client and server must have called their
// init() callback before you can continue using them. The
// following functions would normally be called inside that
// callback.

cPubKey = client.getPublicKey();
server.setClientPublicKey(cPubKey);

salt = server.getSalt();
client.setSalt(salt);

sPubKey = server.getPublicKey();
client.setServerPublicKey(sPubKey);

client.getSharedKey() === server.getSharedKey() // will be true

API Reference

Client methods:

Server methods:

In either scenario, if you'd like to interact with the SRP protocol implementation directly, the SRP object will be available on the client/server object after running init(). You can access it using clientObj.srp or serverObj.srp. The intermediate values calculated by the client and server are also available on the objects themselves as well.

Testing

First, install the dependencies:

npm install

Also, you will need Mocha and CoffeeScript if you don't have them already:

npm install -g mocha coffee-script

Then simply run:

npm test

Browser Builds

To build JSRP for the browser, you will need Browserify and CoffeeScript:

npm install -g browserify coffee-script

Then run the following commands inside the JSRP directory:

coffee --compile --output lib src
browserify jsrp.js --standalone jsrp > jsrp-browser.js

Credits

JSRP would not exist if it wasn't for Node-SRP: https://github.com/mozilla/node-srp. They provided a solid reference implementation, but JSRP was born out of wanting a reliable browser implementation as well as server implementation.