Tag: passport.js

与护照使用WebSocket

当有人打开WebSocket连接时,如何访问当前会话的护照login? 我发现了一个很好的项目expression,这似乎工作得很好 app.ws('/', function(ws, req) { ws.on('message', function(msg) { console.log('express-ws — ', msg); }); console.log('socket', req.user); //current user == req.user }); 但是,我怎样才能获得这个相同的信息与一个简单的WebSocket连接? var WebSocketServer = require('ws').Server, wss = new WebSocketServer({ port: 3001 }); wss.on('connection', function(socket){ //Where is the current user???? console.log('connection'); socket.on('message', function(message){ console.log('message received', message); }); }); (这第二个连接确实工作正常 – 但我不能为我的生活find任何方式从Passportlogin信息)

Passport.js:使用骨干时访问用户

我在node.js应用程序中使用passport.js来获取OAuth工作(真棒!),但是现在,我遇到了一件事情。 我在客户端使用backbone.js来创build视图(我试图创build一个SinglePage应用程序…),唯一的方法,我想获得用户ID或类似的东西在骨干网中创build首先是一个隐藏在jade模板中的input,然后将其传递给我的backbone视图中的构造函数….或者只是将值赋给一个javascriptvariables,并将其传递给pipe理所有视图的路由器,并将其传递给每个视图那需要它 我认为这不是一个好主意,可能有更好的select! 你会怎么做? 谢谢!

Express.js / Passport应用程序为每个请求创build新的会话,尽pipe会话ID在请求标头中

之前没有注意到这一点,因为对于客户端 – 服务器通信,通过XHR(至今)还没有需要validation的事情。 在实现一些集成testing时,我正在创buildrealy请求node-xmlhttprequest到应用程序的一个实例。 用户在第一个请求中被authentication,理论上用户的唯一标识符和一些其他相关信息被存储在会话中(正如我所说的,这对于真正的客户端来说很好,他们不需要通过XHR来确认他们的身份)。 出于某种原因,即使随后的请求正在使用完全相同的会话ID触发,我无法在后续请求中检索会话,因此无法看到用户已通过身份validation,这导致失败的testing,其中预期的行为不会发生,HTTP 401 UNAUTHORIZED正在填满terminal。 我见过几个这样的问题,但是“同一个会话ID”似乎并不存在于其中。 我是否需要手动添加Set-Cookie头到所有的XHR请求? 太糟糕了,还有更好的办法! 所以人们喜欢源代码,这里有一些来自testing,发出这些请求: // Ensure logged in before sending request, to verify that authorized // users encounter the expected behaviour. jQuery.post(domain+'/login', {email:'test@express.app', password:'12345678'}, function(data,x,y){ data.should.equal("true") jQuery.ajax({url:domain+'/event', type:"POST", name: "TestEvent", location: "TestVille", startDate: new Date('2013-09-01'), startTime: '6:45 pm', description: "Test Event #1", success: state.success, error: state.error, completed: […]

Facebook持久性令牌和使用Node.js,Express和Passport“保持login状态”

我想知道如何(如果可能的话)有可能从Facebook获得持久性令牌,因为从Passport身份validation只给出一个短时间的令牌(从我读的)。 如果这是可能的,我会知道如何让用户仍然login,只要他重新连接到我的网站。 这是我的(但不是真正一致的)代码: var passport = require('passport') , FacebookStrategy = require('passport-facebook').Strategy; var express = require('express'), app = express(); passport.use(new FacebookStrategy({ clientID: "xxxxx", clientSecret: "xxxxxx", callbackURL: "/auth/facebook/callback" }, function(accessToken, refreshToken, profile, done) { done(null, profile); } )); app.get('/login', function(req, res) { res.end('<a href="/auth/facebook">Login with Facebook</a>') ; }) app.get('/', function(req, res) { res.end('logged in !') ; }) […]

如何在node.js中使用Passport访问序列化用户

我已经能够使用Passport进行身份validation,但是我不知道如何访问序列化的用户。 我已经读了几个地方,访问它的方式是与req.user,但是当我尝试与GET /你好,它说,用户是未定义的… 这是我的app.js var express = require('express'); var routes = require('./routes'); var user = require('./routes/user'); var authentication = require('./routes/authentication'); var content = require('./routes/content'); var posts = require('./routes/post.js'); var upload = require('./routes/upload.js'); var media = require('./routes/media.js'); var http = require('http'); var path = require('path'); var passport = require('passport'); var LocalStrategy = require('passport-local').Strategy; var mongoose = […]

与Facebookcallback函数的CORS错误

我有以下代码: app.js: var passport = require('passport') , FacebookStrategy = require('passport-facebook').Strategy , … passport.serializeUser(function(user, done) { console.log('serializing user') done(null, user); }) passport.deserializeUser(function(obj, done) { console.log('deserializeUser') done(null, obj) }) passport.use(new FacebookStrategy({ clientID: FBAPP.id, clientSecret: FBAPP.secret, callbackURL: "http://www.mylocal.com:3000/auth/facebook/callback" }, function(accessToken, refreshToken, profile, done) { // asynchronous verification, for effect… process.nextTick(function () { return done(null, profile) }) } )) […]

正在validation用户是否是基于req.user安全的pipe理员?

我在我的express.js应用程序中使用简单的中间件function来validation用户是否具有pipe理员权限: function isAdmin (req, res, next) { if (req.user.admin) return next(); res.redirect("/"); } passport正在用于帐户validation。 这是安全的还是可以req.user.admin被注入请求谁不应该有pipe理员权限的用户? 我应该先find一个用户,然后检查用户是否具有pipe理员权限? 例如: function isAdmin (req, res, next) { if (req.user) { User.findOne({ "_id" : req.user._id }, function (err, user) { if (err) { throw err; } else if (user.admin) { return next(); } else { res.redirect("/"); } }) } else […]

PassportJS / NodeJS使用Google Auth保护REST API

我有一个应用程序使用passport -google-oauth的护照来允许Google身份validation与RESTful API端点。 我正在创build需要与这些API端点通信的其他应用程序(例如Chrome扩展)。 如何在护照中使用Google身份validation来保护REST API? 我通常阅读了很多关于保护REST API的东西(即如果我有自己的login),但是如果我的应用程序依赖于第三方login,我该怎么做呢? (即Google,Facebook,Twitter等) 谢谢

需要Mongoose.js上的几列之一

在Mongoose.js中,是否有任何方法只需要一个(或多个,但不是全部)多个列? 就我而言,我正在使用Passport,并希望我的用户通过我提供的提供商之一进行注册,或者自行创build。 不过,我不想要求用户通过任何一个提供商注册,而是要求他/她希望的任何一个。 下面是一个示例架构,来自Passport上的scotch.io教程 ( 注意 :这是一个例子,我不打算在我的应用中使用它,但可能使用类似的东西): // app/models/user.js // load the things we need var mongoose = require('mongoose'); var bcrypt = require('bcrypt-nodejs'); // define the schema for our user model var userSchema = mongoose.Schema({ local : { email : String, password : String, }, facebook : { id : String, token : String, email […]

Mongoose ValidationError在Passport KeystoneJS中创build新用户

我在我的项目中使用KeystoneJS的Passport身份validation插件 。 如果用户帐户存在并且与正在使用的社交networking绑定,那么一切都很好。 当创build一个新用户时,使用config选项'auto create user': true ,最后在oauthcallback路由上出现500错误。 日志说这是一个validation错误。 ValidationError: Validation failed at model.Document.invalidate (/app/node_modules/keystone/node_modules/mongoose/lib/document.js:1021:32) at /app/node_modules/keystone/node_modules/mongoose/lib/document.js:970:16 at validate (/app/node_modules/keystone/node_modules/mongoose/lib/schematype.js:610:7) at /app/node_modules/keystone/node_modules/mongoose/lib/schematype.js:627:9 at Array.forEach (native) at SchemaString.SchemaType.doValidate (/app/node_modules/keystone/node_modules/mongoose/lib/schematype.js:614:19) at /app/node_modules/keystone/node_modules/mongoose/lib/document.js:968:9 at process._tickCallback (node.js:419:13) 什么可能导致这个? 编辑 用户型号: var keystone = require('keystone'), Types = keystone.Field.Types, social = require('keystone-social-login'); /** * User Model * ========== */ var User […]