node-restful icon indicating copy to clipboard operation
node-restful copied to clipboard

Authorization & Authentication

Open baugarten opened this issue 12 years ago • 5 comments

Authorize/Authenticate the user that uses the API

And API limits?

baugarten avatar Jan 24 '13 04:01 baugarten

I suppose this really reduces to #25

resource.before(*, function(req, res, next) {
  if (!authorized(req)) return next(err);
  next();
});

But we can make this API explicit

resource.authorize(auth_function);

baugarten avatar Apr 02 '13 19:04 baugarten

And what about a situation where we have multiple users owning some resources?

I want to be able to respond to GET with only the resources belonging to the authorized user. There is no way of passing filter to queries. Also, I don't know if the user is allowed to request a resource before I query it, so I'd have to fetch it anyway to be able to throw error.

This is not enough.

[edit]

I was able to hack it like this:

function filterAccess(req, res, next) {
    req.quer._conditions.owner=someuserIdFromSession;   
    return next();
}

It's ugly, but You get the point - it's just about server-side filtering

naugtur avatar Sep 24 '13 11:09 naugtur

First, you could just do

function filterAccess(req, res, next) {
  req.quer = req.quer.where('owner', someuserIdFromSession)
  next()
}

Next, you could solve this using a custom route

resource.route('getuserfromsession', function(req, res, next) {
  User.findById(req.session.userId, function(err, user) {
    res.json(user); // error checking, etc.
  });
})

Or

resource.before('get', function(req, res, next) {
  // You might have to do some extra checking to make sure this isn't a GET list
  req.params.id = req.session.userIdFromSesssion;
  next()  
});

None of these relate to authentication -- in the future, please just open up a new issues :)

baugarten avatar Sep 24 '13 12:09 baugarten

Ok, It's up to you, I just expected this functionality (checking if user has access to what he requests) to be available somewhere near authorization. Thanks anyway.

naugtur avatar Sep 25 '13 09:09 naugtur

I thought you wanted to populate request data from session data.

Authorization you can currently do on select routes

resource.before('get', function(req, res, next) {
  if (!authorized(req)) {
    return next({
      err: "Some error"
    });
  }
  return next();
})

baugarten avatar Sep 25 '13 10:09 baugarten