Search code examples
node.jspassport.jsrolesaclnode.js-connect

Group-level role authorization in Node.js


I'm using Passport.js for authentication in an Express application.

I need to implement role-based authorization and I'm leaning towards connect-roles for it's easy integration with Passport.

I understand how basic roles are authorized (e.g. Admin, User, Editor), but I need to authroize these roles in the context of a groups.

A simplified use-case would be: An admin of a page can only see and edit details of the page he is managing.

How can the basic roles be combined with group assignment, is it necessarily a roles step or a matter of checking resource access rights in the passport authentication?


Solution

  • That's what I've done. It does not fully uses passport, but it works well (I took inspiration from Ghost). I do not know whether it is a good practise or it is safe, but here it is:

    The config.json contains the permissions:

    "user_groups": {
        "admin": {
          "full_name": "Administrators",
          "description": "Adminsitators.",
          "allowedActions": "all"
        },
        "modo": {
          "full_name": "Moderators",
          "description": "Moderators.",
          "allowedActions": ["mod:*", "comment:*", "user:delete browse add banish edit"]
        },
        "user": {
          "full_name": "User",
          "description": "User.",
          "allowedActions": ["mod:browse add star", "comment:browse add", "user:browse"]
        },
        "guest": {
          "full_name": "Guest",
          "description": "Guest.",
          "allowedActions": ["mod:browse", "comment:browse", "user:browse add"]
        }
      }
    

    Then there is the permissions.coffee file

    mongoose = require("mongoose")
    ###
    This utility function determine whether an user can do this or this
    using the permissions. e. g. "mod" "delete"
    
    @param userId the id of the user
    @param object the current object name ("mod", "user"...)
    @param action to be executed on the object (delete, edit, browse...)
    @param owner the optional owner id of the object to be "actionned"
    ###
    exports.canThis = ((userId, object, action, ownerId, callback) ->
      User = mongoose.model("User")
      if typeof ownerId is "function"
        callback = ownerId
        ownerId = undefined
      if userId is ""
        return process(undefined, object, action, ownerId, callback)
      User.findById(userId, (err, user) ->
        if err then return callback err
        process(user, object, action, ownerId, callback)
      )
    ).toPromise(@)
    
    process = (user, object, action, ownerId, callback) ->
      if user then role = user.role or "user"
      group = config.user_groups[role or "guest"]
      if not group then return callback(new Error "No suitable group")
    
      # Parses the perms
      actions = group.allowedActions
      for objAction in actions when objAction.indexOf object is 0
        # We get all the allowed actions for the object and group
        act = objAction.split(":")[1]
        obj = objAction.split(":")[0]
        if act.split(" ").indexOf(action) isnt -1 and obj is object
          return callback true
    
      callback false
    
    config = require "../config"
    

    Then some usage (using Q):

    exports.edit = (userid, name) ->
      # Q promise
      deferred = Q.defer()
      # default value
      can = false
      # We check wheteher it can or not
      canThis(userid, "user", "edit").then((can)->
        if not userid
          return deferred.reject(error.throwError "", "UNAUTHORIZED")
        User = mongoose.model "User"
        User.findOne({username: name}).select("username location website public_email company bio").exec()
      ).then((user) ->
        # Can the current user do that?
        if not user._id.equals(userid) and can is false
          return deferred.reject(new Error())
        # Done!
        deferred.resolve user
      ).fail((err) ->
        deferred.reject err
      )
      deferred.promise