Внедрение токена паспорта-http-носителя с помощью sails.js

Я пытаюсь реализовать паспортную стратегию passport-http-bearer, но не нашел пользователя с информацией Bearer realm="Users".

Мой request является почтовым запросом:

{'token':'simple_access_token',} 

Кто-нибудь знает, почему эта ошибка возникает? Также я знаю, что здесь 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);
    },

Ответы

Ответ 1

В последнее время нам пришлось реализовать защиту API на основе Sails с токенами-носителями, и вот что мы сделали (протестировано с помощью 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());
  }
};

2) Обеспечьте необходимые контроллеры/действия политикой в ​​ config/policies.js:

module.exports.policies = {
  // Default policy for all controllers and actions
  '*': 'authenticated'
};

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

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

Таким образом, вы сможете получить доступ к API, указав свой канал в заголовке Authorization: Bearer 8j4s36....

В этом примере для запроса/выпуска токенов использовался отдельный сервер, но вы также можете сделать это в одном приложении (тогда вам придется применять политику только к выбранным контроллерам).