2012-04-06 52 views
2

好的,所以我試圖驗證browserID返回的斷言來登錄用戶。爲此,我嘗試使用browserid-verifier包。但是我無法安裝它。有一個在node-bigint包錯誤,如下圖所示:如何驗證node.js中navigator.id.get返回的斷言?

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 

所以不是我試着寫我自己的代碼來驗證該聲明。這是我想出了:

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); 

然而,當用戶試圖登錄我得到以下錯誤:

[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.<anonymous> (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) 

任何幫助將不勝感激。我所要做的就是創建一個到https://browserid.org/的HTTP POST請求,傳遞斷言和觀衆,並檢查返回狀態。我想知道我到哪裏去錯了。

回答

1

我想通了什麼是錯的。在我的選項中,我將端口設置爲80.由於我使用HTTPS,端口應該是443.因此,我刪除了端口以允許它選擇默認端口。此外,由於它以塊的形式接收數據,因此我只是將所有塊收集到一條消息中,並在收到完整消息時對其解析。耶,我爲自己感到驕傲。

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(); 
};