我正在开发Node.js项目,我们需要为客户提供安全授权,就Facebook和Twitter而言,我们必须使用他们的API验证令牌
我谷歌它发现了许多例子,但都使用第三方API i-e Facebook,Twitter等
但问题是我们必须发出我们自己的令牌,设备将在与我们的API交谈时使用。 是否为Node.js实现了任何模块,用于验证和生成令牌?
答案 0 :(得分:4)
我不清楚你想要找到一个节点模块(所有这些都是第三方API)的意思,它提供的OAuth2客户端功能可以在没有额外服务器调用的情况下对Facebook和Twitter起作用。
然而,话虽如此,你可以看看这些:
由于Node.js是javascript,你可以包含为客户端浏览器编写的常规javascript,只要你模拟客户端javascript所需的必要浏览器环境元素。
您的问题让我觉得封装此处提供的OAuth库可能很有用:https://github.com/andreassolberg/jso,并将其汇总到Node模块中,这样您就可以拥有一个客户端,其行为类似于针对OAuth2提供商的浏览器但是来自Node。
所以这是该项目的开始:
这里是github(https://github.com/hoonto/node-jso) 或
npm instal node-jso
它不太可能工作,因为我需要修补它在本地存储和xhr的方式,但这是非常微不足道的我想修复。
我提供了一些回调函数,所以你可以像这样使用它:
var jso = require("node-jso")('http://www.google.com');
jso.onlocation = function(location){
//Handle location changes
};
jso.onhash = function(hash){
//Handle hash changes
};
jso.onhref = function(href){
//Handle href changes
};
jso.jso_configure({
"facebook": {
client_id: "xxxxxxxxxx",
redirect_uri: "http://localhost/~andreas/jso/",
authorization: "https://www.facebook.com/dialog/oauth",
presenttoken: "qs"
}
});
如果您愿意提供帮助,请随时提交拉取请求!
答案 1 :(得分:1)
你在hoonto的帖子中看到的客户端ID是你必须生成的客户端ID扔掉Facebook网站。这是您的应用程序的客户端ID,而不是用户的客户端ID。 因此,您只需填写一次,没有oAuth模块就可以使用。
您需要为每个社交媒体(FB,TW和GP)创建一个应用程序。
对于Facebook,您可以使用应用页面获取此客户端ID: https://developers.facebook.com/apps
对于Google Plus,您可以在控制台页面中生成客户端ID:https://code.google.com/apis/console/
答案 2 :(得分:1)
这会对令牌https://github.com/jaredhanson/passport-http-bearer进行身份验证,您可以通过多种方式生成令牌(md5 of email + password + salt)
答案 3 :(得分:1)
答案 4 :(得分:1)
如果我理解你的意思,我的项目具有相同的功能。 我的应用程序充当Oauth2提供程序,但将授权委托给其他提供程序。我的应用程序不支持范围,因为它需要额外的对话,所以我还没有使用所有的oauth2功能。但我计划将来。 我用过这个模块:
工作的主要帮助是来自oauth2orize模块的示例。
答案 5 :(得分:0)
您可以在没有任何额外模块的情况下执行简单的仅限Twitter应用程序的OAuth2。
var https = require('https');
var querystring = require('querystring');
var postData = querystring.stringify({
'grant_type' : 'client_credentials'
})
var options = {
hostname: 'api.twitter.com',
path: '/oauth2/token',
method: 'POST',
headers: {
'Authorization': 'Basic ' + new Buffer(
process.env.CONSUMER_KEY + ":" + process.env.CONSUMER_SECRET
).toString('base64'),
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
'Content-Length': postData.length
} // headers
}
var req = https.request(options, function(res) {
console.log("statusCode: ", res.statusCode);
console.log("headers: ", res.headers);
res.on('data', function(d) {
process.stdout.write(d);
});
});
req.on('error', function(e) {
console.error(e);
});
req.write(postData);
req.end();
如果你把上面的代码放在foo.js中,那么你应该得到类似的东西:
$ node foo.js
statusCode: 200
headers: { 'cache-control': 'no-cache, no-store, must-revalidate, pre-check=0, post-check=0',
connection: 'close',
'content-disposition': 'attachment; filename=json.json',
'content-length': '155',
'content-type': 'application/json;charset=utf-8',
date: 'Mon, 07 Sep 2015 03:50:59 GMT',
expires: 'Tue, 31 Mar 1981 05:00:00 GMT',
'last-modified': 'Mon, 07 Sep 2015 03:50:59 GMT',
ml: 'S',
pragma: 'no-cache',
server: 'tsa_a',
'set-cookie': [ 'guest_id=v1%3A144159785995688631; Domain=.twitter.com; Path=/; Expires=Wed, 06-Sep-2017 03:50:59 UTC' ],
status: '200 OK',
'strict-transport-security': 'max-age=631138519',
'x-connection-hash': 'e3fa57b3959c2fdf39fdf4bdd5851b35',
'x-content-type-options': 'nosniff',
'x-frame-options': 'SAMEORIGIN',
'x-response-time': '21',
'x-transaction': '2296d03323316c3e',
'x-tsa-request-body-time': '0',
'x-twitter-response-tags': 'BouncerCompliant',
'x-ua-compatible': 'IE=edge,chrome=1',
'x-xss-protection': '1; mode=block' }
{"token_type":"bearer","access_token":"[YOUR ACCESS TOKEN HERE!!!]"}