Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to verify the assertion returned by navigator.id.get in node.js?

How to verify the assertion returned by navigator.id.get in node.js?

Problem

Alright, so I'm trying to verify an assertion returned by browserID to sign in a user. To do so I was trying to use the browserid-verifier package. However I wasn't able to install it. There's an error in the node-bigint package as shown below:

npm http GET https://registry.npmjs.org/browserid-verifier
npm http 304 https://registry.npmjs.org/browserid-verifier
npm http GET https://registry.npmjs.org/jwcrypto/0.1.1
npm http GET https://registry.npmjs.org/express/2.5.1
npm http 304 https://registry.npmjs.org/jwcrypto/0.1.1
npm http 304 https://registry.npmjs.org/express/2.5.1
npm http GET https://registry.npmjs.org/mime
npm http GET https://registry.npmjs.org/connect
npm http GET https://registry.npmjs.org/qs
npm http GET https://registry.npmjs.org/mkdirp/0.0.7
npm http GET https://registry.npmjs.org/browserify/1.8.1
npm http GET https://registry.npmjs.org/vows/0.5.13
npm http GET https://registry.npmjs.org/optimist/0.2.6
npm http GET https://github.com/benadida/node-bigint/tarball/2ac68
npm http 304 https://registry.npmjs.org/mime
npm http 304 https://registry.npmjs.org/qs
npm http 304 https://registry.npmjs.org/mkdirp/0.0.7
npm http 304 https://registry.npmjs.org/connect
npm http 304 https://registry.npmjs.org/browserify/1.8.1
npm http 304 https://registry.npmjs.org/vows/0.5.13
npm http GET https://registry.npmjs.org/formidable
npm http 304 https://registry.npmjs.org/optimist/0.2.6
npm http 200 https://github.com/benadida/node-bigint/tarball/2ac68

> [email protected] install /home/aaditmshah/struggleforlife/node_modules/browserid-verifier/node_modules/jwcrypto/node_modules/bigint
> node-waf configure build

npm http GET https://registry.npmjs.org/wordwrap
npm http GET https://registry.npmjs.org/eyes
Checking for program g++ or c++          : /usr/bin/g++ 
Checking for program cpp                 : /usr/bin/cpp 
Checking for program ar                  : /usr/bin/ar 
Checking for program ranlib              : /usr/bin/ranlib 
Checking for g++                         : ok  
Checking for node path                   : not found 
Checking for node prefix                 : ok /usr 
'configure' finished successfully (0.022s)
Waf: Entering directory `/home/aaditmshah/struggleforlife/node_modules/browserid-verifier/node_modules/jwcrypto/node_modules/bigint/build'
[1/2] cxx: bigint.cc -> build/Release/bigint_1.o
npm http GET https://registry.npmjs.org/detective
npm http GET https://registry.npmjs.org/deputy
npm http GET https://registry.npmjs.org/resolve
npm http GET https://registry.npmjs.org/nub
npm http GET https://registry.npmjs.org/commondir
npm http GET https://registry.npmjs.org/coffee-script
npm http 304 https://registry.npmjs.org/formidable
../bigint.cc:9:17: fatal error: gmp.h: No such file or directory
compilation terminated.
Waf: Leaving directory `/home/aaditmshah/struggleforlife/node_modules/browserid-verifier/node_modules/jwcrypto/node_modules/bigint/build'
Build failed:  -> task failed (err #1): 
    {task: cxx bigint.cc -> bigint_1.o}
npm ERR! error installing [email protected]
npm ERR! error installing [email protected]
npm ERR! error installing [email protected]

npm ERR! [email protected] install: `node-waf configure build`
npm ERR! `sh "-c" "node-waf configure build"` failed with 1
npm ERR! 
npm ERR! Failed at the [email protected] install script.
npm ERR! This is most likely a problem with the bigint package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node-waf configure build
npm ERR! You can get their info via:
npm ERR!     npm owner ls bigint
npm ERR! There is likely additional logging output above.
npm ERR! 
npm ERR! System Linux 3.0.0-17-generic
npm ERR! command "node" "/usr/bin/npm" "install" "browserid-verifier"
npm ERR! cwd /home/aaditmshah/struggleforlife
npm ERR! node -v v0.6.14
npm ERR! npm -v 1.1.4
npm ERR! code ELIFECYCLE
npm ERR! message [email protected] install: `node-waf configure build`
npm ERR! message `sh "-c" "node-waf configure build"` failed with 1
npm ERR! errno {}
npm http 304 https://registry.npmjs.org/resolve
npm ERR! 
npm ERR! Additional logging details can be found in:
npm ERR!     /home/aaditmshah/struggleforlife/npm-debug.log
npm not ok

So instead I tried writing my own code to verify the assertion. This is what I came up with:

var mongoLife = "mongodb://nodejitsu:[email protected]:10088/nodejitsudb449201704656";

var querystring = require("querystring");
var mongoose = require("mongoose");
var express = require("express");
var https = require("https");
var nowjs = require("now");

mongoose.connect(mongoLife);
var app = express.createServer();
app.use(express.static(__dirname + "/public"));
var everyone = nowjs.initialize(app);

everyone.now.login = function (assertion) {
    var client = this.now;

    var postData = querystring.stringify({
        audience: "http://life.nodejitsu.com/",
        assertion: assertion
    });

    var options = {
        host: "browserid.org",
        path: "/verify",
        method: "POST",
        port: 80,
        headers: {
            "Content-Type": "application/x-www-form-urlencoded",
            "Content-Length": postData.length
        }
    };

    var request = https.request(options, function (response) {
        response.setEncoding("utf8");
        response.on("data", function (responseText) {
            var user = JSON.parse(responseText);
            client.loggedIn(user.status === "okay" ? user.email : null);
        });
    });

    request.write(postData);
    request.end();
};

var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;

app.listen(8080);

However when a user tries to sign in I get the following error:

[Error: 139723080664896:error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol:s23_clnt.c:683:]
Error: 139723080664896:error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol:s23_clnt.c:683:

    at CleartextStream._puller (tls.js:501:24)
    at CleartextStream._pull (tls.js:432:19)
    at SecurePair.cycle (tls.js:727:20)
    at EncryptedStream.write (tls.js:130:13)
    at Socket.ondata (stream.js:38:26)
    at Socket.emit (events.js:67:17)
    at TCP.onread (net.js:367:14)
{ [Error: socket hang up] code: 'ECONNRESET' }
Error: socket hang up
    at createHangUpError (http.js:1107:15)
    at CleartextStream. (http.js:1210:27)
    at CleartextStream.emit (events.js:88:20)
    at Array.0 (tls.js:792:22)
    at EventEmitter._tickCallback (node.js:192:40)

Any help will be appreciated. All I'm trying to do is create an Http POST request to https://browserid.org/, pass the assertion and the audience, and check the return status. I would like to know exactly where I'm going wrong.

Problem courtesy of: Aadit M Shah

Solution

I figured out what was wrong. In my options I had set the port to 80. Since I'm using HTTPS the port should have been 443. So I removed the port to allow it to select a default port. Also since it receives data in chunks I just collected all the chunks into a single message and parsed the entire message when it was received. Yay, I'm so proud of myself.

everyone.now.login = function (assertion) {
    var client = this.now;

    var postData = querystring.stringify({
        audience: "http://life.nodejitsu.com/",
        assertion: assertion
    });

    var options = {
        host: "browserid.org",
        path: "/verify",
        method: "POST",
        headers: {
            "Content-Type": "application/x-www-form-urlencoded",
            "Content-Length": postData.length
        }
    };

    var request = https.request(options, function (response) {
        var responseText = "";

        response.on("data", function (chunk) {
            responseText += chunk;
        });

        response.on("end", function () {
            var user = JSON.parse(responseText);
            client.loggedIn(user.status === "okay" ? user.email : null);
        });
    });

    request.write(postData);
    request.end();
};
Solution courtesy of: Aadit M Shah

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

How to verify the assertion returned by navigator.id.get in node.js?

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×