Error "401 Authorization required" when querying methods involving relations
I've recently migrated an application to LoopBack 2.0.
All my models require user authentication and have these ACLs:
"acls": [
{
"accessType": "*",
"permission": "DENY",
"principalType": "ROLE",
"principalId": "$everyone"
},
{
"accessType": "READ",
"permission": "ALLOW",
"principalType": "ROLE",
"principalId": "$authenticated"
}
]
I have a model called issues. After logging in through the POST method /Users/login:
- I don't have any problems when calling the GET method
/issues. I get a list of issues as expected; - When I query a method involving a relation between two models (e.g. GET
/issues/{id}/categories), I get the error401 Authorization required. The same model used to work in Loopback 1.0.
Any clues?
I have the same issue. I also think the 401 Unauthorized is a wrong response. I am authenticated, so it should be 403 forbidden. I'm opening a new issue for that.
I think this has already been implemented: https://github.com/strongloop/loopback/issues/301
This issue (#426) itself is not yet resolved. Defining an ACL on a relation property does not seem to work for me.
Replicate by defining a model, adding a relationship to it (for example many user to many group). Then DENY ALL on * for ROLE $everyone, and finally ALLOW READ/EXECUTE on
I am having the exact same issue! I spent a few hours digging through anything I can find, but I still couldnt get it to work!
@dagumak - can you post the ACL's you defined?
@fabien I posted it here (https://github.com/strongloop/loopback/issues/459#issuecomment-52635774), but here it is:
{
"name": "TestUser",
"base": "User",
"strict": true,
"properties": {
"username": {
"type": "string",
"required": true
}
},
"validations": [],
"relations": {
"message": {
"type": "hasMany",
"model": "Message",
"foreignKey": "userId"
}
},
"acls": [
],
"methods": []
}
{
"name": "Message",
"base": "PersistedModel",
"properties": {
"type": {
"type": "string",
"required": true
},
"text": {
"type": "string",
"required": true
}
},
"validations": [],
"relations": {
"testUser": {
"type": "belongsTo",
"model": "TestUser",
"foreignKey": "userId"
}
},
"acls": [
{
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "ALLOW"
}
],
"methods": []
}
I think the core User ACL's are interfering with the ACL's you defined. I recall the following from one of my projects, which overrides all the ACL's:
TestUser.settings.acls = [
{ principalType: 'ROLE',
principalId: '$everyone',
permission: 'DENY' },
{ principalType: 'ROLE', // this is the important bit
principalId: '$owner',
permission: 'ALLOW' },
{ principalType: 'ROLE',
principalId: '$everyone',
permission: 'ALLOW',
property: 'create' },
{ principalType: 'ROLE',
principalId: '$owner',
permission: 'ALLOW',
property: 'deleteById' },
{ principalType: 'ROLE',
principalId: '$everyone',
permission: 'ALLOW',
property: 'login' },
{ principalType: 'ROLE',
principalId: '$everyone',
permission: 'ALLOW',
property: 'logout' },
{ principalType: 'ROLE',
principalId: '$owner',
permission: 'ALLOW',
property: 'findById' },
{ principalType: 'ROLE',
principalId: '$owner',
permission: 'ALLOW',
property: 'updateAttributes' },
{ principalType: 'ROLE',
principalId: '$everyone',
permission: 'ALLOW',
property: 'confirm' }
]
@dagumak the very least you could do is trace your current TestUser.settings.acls to compare them.
@Fabien I feel silly for asking, but other than console log, how can I do that?
On Tue, Aug 19, 2014 at 11:32 PM, Fabien Franzen [email protected] wrote:
@dagumak the very least you could do is trace your current TestUser.settings.acls to compare them.
Reply to this email directly or view it on GitHub: https://github.com/strongloop/loopback/issues/426#issuecomment-52642645
@dagumak console.log is fine
@fabien I am not very familiar with the framework yet, but where are the models loaded and a good place for me to console.log it?
You can put the following into a file at /server/boot/debug.js:
module.exports = function(app) {
var TestUser = app.loopback.getModel('TestUser');
console.log(TestUser.settings.acls);
};
You can run the application with DEBUG enabled:
$ DEBUG=loopback:security:* node .
Thanks,
Raymond Feng Co-Founder and Architect @ StrongLoop, Inc.
StrongLoop makes it easy to develop APIs in Node, plus get DevOps capabilities like monitoring, debugging and clustering.
On Aug 19, 2014, at 8:29 AM, Fabien Franzen [email protected] wrote:
You can put the following into a file at /server/boot/debug.js:
module.exports = function(app) { var TestUser = app.loopback.getModel('TestUser'); console.log(TestUser.settings.acls); }; — Reply to this email directly or view it on GitHub.
@fabien Thanks a bunch! Here it is:
[ { principalType: 'ROLE',
principalId: '$everyone',
permission: 'DENY' },
{ principalType: 'ROLE',
principalId: '$everyone',
permission: 'ALLOW',
property: 'create' },
{ principalType: 'ROLE',
principalId: '$owner',
permission: 'ALLOW',
property: 'deleteById' },
{ principalType: 'ROLE',
principalId: '$everyone',
permission: 'ALLOW',
property: 'login' },
{ principalType: 'ROLE',
principalId: '$everyone',
permission: 'ALLOW',
property: 'logout' },
{ principalType: 'ROLE',
principalId: '$owner',
permission: 'ALLOW',
property: 'findById' },
{ principalType: 'ROLE',
principalId: '$owner',
permission: 'ALLOW',
property: 'updateAttributes' },
{ principalType: 'ROLE',
principalId: '$everyone',
permission: 'ALLOW',
property: 'confirm' } ]
Is this resolved ? I am still having the same issue The last thread doesn't help
[ { principalType: 'ROLE', principalId: '$everyone', permission: 'DENY' }, { principalType: 'ROLE', principalId: '$everyone', permission: 'ALLOW', property: 'create' }, { principalType: 'ROLE', principalId: '$owner', permission: 'ALLOW', property: 'deleteById' }, { principalType: 'ROLE', principalId: '$everyone', permission: 'ALLOW', property: 'login' }, { principalType: 'ROLE', principalId: '$everyone', permission: 'ALLOW', property: 'logout' }, { principalType: 'ROLE', principalId: '$owner', permission: 'ALLOW', property: 'findById' }, { principalType: 'ROLE', principalId: '$owner', permission: 'ALLOW', property: 'updateAttributes' }, { principalType: 'ROLE', principalId: '$everyone', permission: 'ALLOW', property: 'confirm' }, { principalType: 'ROLE', principalId: '$everyone', permission: 'ALLOW', property: 'resetPassword', accessType: 'EXECUTE' }, { accessType: '*', principalType: 'ROLE', principalId: '$everyone', permission: 'ALLOW' } ]
Something that helped me with ACL and relations is to run "DEBUG=loopback:security:* slc run" and then check the property it is trying to access. At first I tried adding ACL to the "orders" property (since it was like /users/2/orders) but it seems like the ACL resolver tried to access this property: __get__orders. I set that in my json acl file and it is working like a charm.
Example: "acls": [ { "accessType": "EXECUTE", "principalType": "ROLE", "principalId": "$owner", "permission": "ALLOW", "property": "__get__orders" }
Please create a test project on Github to reproduce the issue as per https://github.com/strongloop/loopback/wiki/Issues#bug-report
@rubentorresbonet the acl that you defined with the property __get__orders is for which model? Is it for the user model or for the orders model?
@LetsDoIt- Have a look here: http://docs.strongloop.com/display/public/LB/Restricting+access+to+related+models