Muh*_*min 14 node.js express http-token-authentication sails.js passport.js
我正在尝试实施护照的passport-http-bearer
策略,但它没有找到有信息的用户Bearer realm="Users"
.
我request
是一个帖子请求:
{'token':'simple_access_token',}
Run Code Online (Sandbox Code Playgroud)
任何人都知道为什么会出现这种错误?我也知道这req
应该是https
或ssl
代替http
.我怎么做?
我使用的代码是:
bearerPassportToken: function(req,res){
passport.authenticate('bearer', function(err, user, info){
if ((err) || (!user)) {
if (err) return;
if (!user)
console.log("info);//Info: Bearer realm="Users"
res.redirect('/login');
return;
}
req.logIn(user, function(err){
if (err){
res.redirect('/login');
}
//Need to write code for redirection
;
});
})(req, res);
},
Run Code Online (Sandbox Code Playgroud)
bre*_*hin 23
我们最近必须使用承载令牌来实现基于Sails的API的保护,这就是我们所做的(测试过0.9.x
):
1)将护照作为自定义中间件连接config/passport.js
(或者可以config/express.js
根据您的喜好):
/**
* Passport configuration
*/
var passport = require('passport');
module.exports.express = {
customMiddleware: function(app)
{
app.use(passport.initialize());
app.use(passport.session());
}
};
Run Code Online (Sandbox Code Playgroud)
2)使用以下策略确保必要的控制器/操作config/policies.js
:
module.exports.policies = {
// Default policy for all controllers and actions
'*': 'authenticated'
};
Run Code Online (Sandbox Code Playgroud)
3)创建检查承载的策略api/policies/authenticated.js
:
/**
* Allow any authenticated user.
*/
var passport = require('passport');
module.exports = function (req, res, done) {
passport.authenticate('bearer', {session: false}, function(err, user, info) {
if (err) return done(err);
if (user) return done();
return res.send(403, {message: "You are not permitted to perform this action."});
})(req, res);
};
Run Code Online (Sandbox Code Playgroud)
4)定义护照的承载策略services/passport.js
(或者在其他任何地方,您认为它更适合您的特定应用):
var passport = require('passport'),
BearerStrategy = require('passport-http-bearer').Strategy;
/**
* BearerStrategy
*
* This strategy is used to authenticate either users or clients based on an access token
* (aka a bearer token). If a user, they must have previously authorized a client
* application, which is issued an access token to make requests on behalf of
* the authorizing user.
*/
passport.use('bearer', new BearerStrategy(
function(accessToken, done) {
Tokens.findOne({token: accessToken}, function(err, token) {
if (err) return done(err);
if (!token) return done(null, false);
if (token.userId != null) {
Users.find(token.userId, function(err, user) {
if (err) return done(err);
if (!user) return done(null, false);
// to keep this example simple, restricted scopes are not implemented,
// and this is just for illustrative purposes
var info = { scope: '*' }
done(null, user, info);
});
}
else {
//The request came from a client only since userId is null
//therefore the client is passed back instead of a user
Clients.find({clientId: token.clientId}, function(err, client) {
if (err) return done(err);
if (!client) return done(null, false);
// to keep this example simple, restricted scopes are not implemented,
// and this is just for illustrative purposes
var info = { scope: '*' }
done(null, client, info);
});
}
});
}
));
Run Code Online (Sandbox Code Playgroud)
通过这种方式,您可以通过将标记包含在Authorization
标题中来访问API : Bearer 8j4s36...
.
在此示例中,使用单独的服务器来请求/发出令牌,但您也可以在同一个应用程序中执行此操作(然后您必须仅将策略应用于所选控制器).
归档时间: |
|
查看次数: |
10120 次 |
最近记录: |