loopback icon indicating copy to clipboard operation
loopback copied to clipboard

Error "401 Authorization required" when querying methods involving relations

Open sam-artuso opened this issue 12 years ago • 40 comments

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 error 401 Authorization required. The same model used to work in Loopback 1.0.

Any clues?

sam-artuso avatar Jul 31 '14 20:07 sam-artuso

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.

daankets avatar Aug 12 '14 12:08 daankets

I think this has already been implemented: https://github.com/strongloop/loopback/issues/301

gcirne avatar Aug 12 '14 13:08 gcirne

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 for ROLE $authenticated. I consequently get access denied for these relationships.

daankets avatar Aug 12 '14 14:08 daankets

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 avatar Aug 19 '14 14:08 dagumak

@dagumak - can you post the ACL's you defined?

fabien avatar Aug 19 '14 14:08 fabien

@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": []
}

dagumak avatar Aug 19 '14 14:08 dagumak

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' }
    ]

fabien avatar Aug 19 '14 14:08 fabien

@dagumak the very least you could do is trace your current TestUser.settings.acls to compare them.

fabien avatar Aug 19 '14 14:08 fabien

@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 avatar Aug 19 '14 14:08 dagumak

@dagumak console.log is fine

fabien avatar Aug 19 '14 14:08 fabien

@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?

dagumak avatar Aug 19 '14 15:08 dagumak

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

fabien avatar Aug 19 '14 15:08 fabien

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.

raymondfeng avatar Aug 19 '14 15:08 raymondfeng

@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' } ]

dagumak avatar Aug 19 '14 16:08 dagumak

Is this resolved ? I am still having the same issue The last thread doesn't help

SwadhyayStudios avatar Jan 31 '15 18:01 SwadhyayStudios

[ { 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' } ]

SwadhyayStudios avatar Jan 31 '15 18:01 SwadhyayStudios

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" }

rubentorresbonet avatar Feb 07 '15 13:02 rubentorresbonet

Please create a test project on Github to reproduce the issue as per https://github.com/strongloop/loopback/wiki/Issues#bug-report

superkhau avatar Feb 26 '15 05:02 superkhau

@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- avatar Feb 26 '15 06:02 LetsDoIt-

@LetsDoIt- Have a look here: http://docs.strongloop.com/display/public/LB/Restricting+access+to+related+models

rubentorresbonet avatar Feb 26 '15 07:02 rubentorresbonet