我在使用 Passport 通过 Node 进行身份验证方面非常陌生,因此有很多代码片段
我的服务器配置为:
var router = require('./app/config/routes');
var googleStrategy = require('./app/config/passport');
var session = require("express-session");
var passport = require('passport');
app.use(session({secret : '<secret-key>'}));
app.use(passport.initialize());
app.use(passport.session());
googleStrategy(passport);
我的路线配置为
module.exports = function(app, passport) {
app.get('/auth/google', function() {
passport.authenticate('google', {scope: ['profile', 'email']});
});
app.get('/auth/google/callback', function() {
passport.authenticate('google', {
successRedirect: '/profile',
failureRedirect: '/fail'
});
});
.... ALSO configured /profile and /fail
};
我的 Passport 配置为
passport.serializeUser(function(user, callback){
console.log('serializing user.');
callback(null, user);
});
passport.deserializeUser(function(user, callback){
console.log('deserialize user.');
callback(null, user);
});
var processRequest = function(token, refreshToken, profile, callback){
process.nextTick(function(){
console.log('id : '+ profile.id);
console.log('name :'+ profile.displayName);
console.log('email :' + profile.emails);
console.log('token : '+ token);
});
};
passport.use(new GoogleStrategy({
clientID: 'client ID',
clientSecret : 'client SECRET',
callbackURL : 'http://127.0.0.1:8080/auth/google/callback',
realm : 'http://127.0.0.1:8080'
}, processRequest));
问题:去 /auth/google 时,我从来没有得到确认屏幕。我应该看什么?
将路由更改为如下所示的配置使其工作。
app.get('/auth/google',
passport.authenticate('google', {scope: ['profile', 'email']})
);
app.get('/auth/google/callback',
passport.authenticate('google', {
successRedirect: '/profile',
failureRedirect: '/fail'
})
);
最佳答案
目前 google 很好地支持 OAUTH2 身份验证和授权协议(protocol),所以最好使用相同的。 Here is google 的文档。使用 'passport-google-oauth' 模块。这是实现。这应该是应用程序对象配置,还可以看到从 passport-google-oauth 模块中使用了 oauth2strategy 对象,还可以查看 app.get 路由注册中的范围。
var googleStrategy = require('passport-google-oauth').OAuth2Strategy;
app.configure(function() {
app.set('views', './views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.session({secret:'MySecret'}));
app.use(passport.initialize());
app.use(passport.session());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static('./public'));
});
app.get('/auth/google', select.passport.authenticate('google',{scope: 'https://www.googleapis.com/auth/plus.me https://www.google.com/m8/feeds https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile'}));
app.get('/auth/google/callback', function() {
passport.authenticate('google', {
successRedirect: '/profile',
failureRedirect: '/fail'
});
});
app.get('/logout', function (req, res) {
req.logOut();
res.redirect('/');
});
但在创建新策略之前,请转到谷歌开发者控制台并获取 clientID 和 secret 。以下是步骤
的快照
然后转到凭据(在 API 下方),然后单击 Create New Client Id ,并为您的应用注册域和回调(将域配置为 localhost ),这里是它的快照!
5.然后你会得到你的新ID和 secret 。使用它们来创建新的策略
passport.use(new googleStrategy({
clientID: '<TheNewclientID>',
clientSecret: '<The New Secret>',
callbackURL: "http://locahost:8080/auth/google/callback"
},
function (accessToken, refreshToken, profile, done) {
console.log(profile); //profile contains all the personal data returned
done(null, profile)
}
));
6.现在序列化和反序列化
passport.serializeUser(function(user, callback){
console.log('serializing user.');
callback(null, user.id);
});
passport.deserializeUser(function(user, callback){
console.log('deserialize user.');
callback(null, user.id);
});
运行服务器并转到 localhost:8080/auth/google(不要使用 127.0.0.1:8080 而不是 locahost )。这应该可以正常工作:)
[其他有用的链接: 在 this 中查看 kvcrawford 对模块 repo 的第一条评论页 Passport-google 是另一个流行的模块,用于使用 google 提供登录,现在有点过时了,这里是 link关于它最近的问题]
关于node.js - 本地主机上的 Passport 谷歌oauth,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24352975/
如何只加载map边界内的标记gmaps4rails?当然,在平移和/或缩放后加载新的。与此直接相关的是,如何获取map的当前边界和缩放级别? 最佳答案 我是这样做的,我只在用户完成平移或缩放后替换标记,如果您需要不同的行为,请使用不同的事件监听器:在你看来(index.html.erb):{"zoom"=>15,"auto_adjust"=>false,"detect_location"=>true,"center_on_user"=>true}},false,true)%>在View的底部添加:functiongmaps4rail
我有一个Rails2.3.5应用程序,其中包含我希望保护的API。没有用户-它是一个应用到应用风格的网络服务(更像是亚马逊服务而不是facebook),所以我想使用两条腿的OAuth方法来实现它。我一直在尝试使用oauth-plugin服务器实现作为开始:http://github.com/pelle/oauth-plugin...但它的构建需要三足(网络重定向流)oauth。在我深入研究对其进行更改以支持两条腿之前,我想看看是否有更简单的方法,或者是否有人有更好的方法让Rails应用程序实现成为两条腿的OAuth提供程序。 最佳答案
我开始了一个新的Rails3.2.5项目,Assets管道不再工作了。CSS和Javascript文件不再编译。这是尝试生成Assets时日志的输出:StartedGET"/assets/application.css?body=1"for127.0.0.1at2012-06-1623:59:11-0700Servedasset/application.css-200OK(0ms)[2012-06-1623:59:11]ERRORNoMethodError:undefinedmethod`each'fornil:NilClass/Users/greg/.rbenv/versions/1
rails新手。只是想了解\assests目录中的这两个文件。例如,application.js文件有如下行://=requirejquery//=requirejquery_ujs//=require_tree.我理解require_tree。只是将所有JS文件添加到当前目录中。根据上下文,我可以看出requirejquery添加了jQuery库。但是它从哪里得到这些jQuery库呢?我没有在我的Assets文件夹中看到任何jquery.js文件——或者直接在我的整个应用程序中没有看到任何jquery.js文件?同样,我正在按照一些说明安装TwitterBootstrap(http:
我有一个包含多个组件的存储库,其中大部分是用JavaScript(Node.js)编写的,一个是用Ruby(RubyonRails)编写的。我想要一个.travis.yml文件来触发一个运行每个组件的所有测试的构建。根据thisTravisCIGoogleGroupthread,目前还没有官方支持。我的目录结构是这样的:.├──构建服务器├──核心├──扩展├──网络应用├──流浪文件├──package.json├──.travis.yml└──生成文件我希望能够运行特定版本的Ruby(2.2.2)和Node.js(0.12.2)。我已经有了一个make目标,所以maketest在每
我想用一个(自己的)omniauth提供商来衡量每秒可以登录多少次。我需要了解此omniauth/oauth请求的性能如何,以及此身份验证是否具有可扩展性?到目前为止我得到了什么:defperformance_auth(user_count=10)bm=Benchmark.realtimedouser_count.timesdo|n|forkdoclick_on'Logout'omniauth_config_mock(:provider=>"foo",:uid=>n,:email=>"foo#{n}@example.net")visit"/account/auth/foo/"enden
我正在尝试为使用omniauth-google-oauth2gem创建session编写测试。我是否需要将env["omniauth.auth"]变量与post:create一起传递?也许当我试图这样做时,我做错了。我得到的错误如下所示...Rake测试错误1)Error:SessionsControllerTest#test_should_get_create:NoMethodError:undefinedmethod`provider'fornil:NilClassapp/models/user.rb:6:in`from_omniauth'app/controllers/sessi
在安装了openssllib的linux机器上,当您执行带有“-nodes”选项的“opensslpkcs12”时,您将获得带有未加密私钥的输出,但如果您跳过–nodes选项,则输出将具有加密的私钥。e.g.opensslpkcs12-intest.pfx-outtest.pem你应该看到像下面这样加密的私钥-----BEGINENCRYPTEDPRIVATEKEY-----MIIFDjBABgkqhkiGG7s=-----ENDENCRYPTEDPRIVATEKEY-----如何使用ruby的开放ssl库实现上述目标?这就是我用ruby生成私钥的方式:@private_key
我正在使用一些旧代码并使用ActiveResource进行非常基本的Twitter集成。我想尽可能少地接触应用程序代码,并在仍然使用ActiveResource的同时引入OAuth。不幸的是,我找不到简单的方法来做到这一点。我确实遇到了oauth-active-resourcegem,但它并没有完全记录下来,而且它似乎是为创建完整的API包装器库而设计的。您可以想象,我想避免为这一遗留更改创建整个TwitterActiveResourceAPI包装器。有什么成功案例吗?在我的例子中,离开ActiveResource可能比让它工作更快。我很高兴被证明是错误的!
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭9年前。多年来,我一直在使用多种语言进行编程,并且认为自己总体上相当擅长。但是,我从未编写过任何自动化测试:没有单元测试,没有TDD,没有BDD,什么都没有。我已经尝试开始为我的项目编写适当的测试套件。我可以看到在进行任何更改后能够自动测试项目中所有代码的理论值(value)。我可以看到像RSpec和Mocha这样的测试框架应该如何使设置和运行所述测试变得相当容易