Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
284 views
in Technique[技术] by (71.8m points)

node.js - Passing multiple scope (ACL) middlewares to routes are only testing for the first scope

My application is supposed to have 3 user scopes (User, Admin, Super Admin). I am trying to do this manually without using any external ACL library.

Here are my admin and super admin scope functions.

const adminScope = (req, res, next) => {
    if (req.user.scope !== 'admin') {
        return res.status(403).send({
            status: 'fail',
            message: 'You are not admin'
        })
    }
    next();
}


const superAdminScope = (req, res, next) => {
    if (req.user.scope !== 'superAdmin') {
        return res.status(403).send({
            status: 'fail',
            message: 'You are not Super Admin'
        })
    }
    next();
}

?

I am trying to use these with my routes as below

app.use('/admin', [passport.authenticate('jwt', { session: false }), adminScope], [adminPage])

?

The above works fine and checks if the scope of the user is admin or not.

I want all the routes in adminPages to be accessible by both Admins and Super Admins.

?

I tried by passing superAdminScope as third middleware.

app.use('/admin', [passport.authenticate('jwt', { session: false }), adminScope, superAdminScope], [adminPage])

It fails after checking just adminScope function and says

{
  status: "fail",
  message: "You are not admin"
}

?

I also tried passing both of them as an array but still the same output.

app.use('/admin', [passport.authenticate('jwt', { session: false }), [adminScope, superAdminScope]], [adminPage])
question from:https://stackoverflow.com/questions/65641978/passing-multiple-scope-acl-middlewares-to-routes-are-only-testing-for-the-firs

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Solved it using this tutorial.

Instead of having separate functions for admin and superAdmin, I created a function to check for It.

const checkForScope = (...scopes) => (req, res, next) => {
    if (!req.user) {
        return res.status(403).send({
            status: 'fail',
            message: 'You are not logged in'
        })
    }

    const hasScope = scopes.find(scope => req.user.scope === scope)

    if (!hasScope) {
        return res.status(403).send({
            status: 'fail',
            message: 'You dont have the rights to do this'
        })
    }

    return next();
}

?

And then I can use this function as middleware in express.

app.use('/admin', [passport.authenticate('jwt', { session: false }), checkForScope('admin', 'superAdmin')], [adminPage])

This will only allow users access to the routes if they have either admin or superAdmin scope.

?

I'm not sure If this is the best way to do this but It's working.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...