loopback icon indicating copy to clipboard operation
loopback copied to clipboard

Loopback resolving wrong AuthToken subclass

Open JuergenSimon opened this issue 9 years ago • 9 comments

Consider the following entries in a model-config.json:

  "AccessToken": {
    "dataSource": "postgres",
    "public": false,
    "relations": {
      "user": {
        "type": "belongsTo",
        "model": "Admin"
      }
    }
  },
  "UDIDToken": {
    "dataSource": "postgres",
    "public": false,
    "relations": {
      "user": {
        "type": "belongsTo",
        "model": "Patient"
      }
    }
  },
  "Admin": {
    "dataSource": "postgres",
    "public": true,
    "relations": {
      "accessTokens": {
        "type": "hasMany",
        "model": "AccessToken",
        "options": {
          "disableInclude": true
        }
      }
    }
  },
  "Patient": {
    "dataSource": "postgres",
    "public": true,
    "relations": {
      "accessTokens": {
        "type": "hasMany",
        "model": "UDIDToken",
        "options": {
          "disableInclude": true
        }
      }
    }
  }

When I log in as Admin, it creates an AccessToken entry in the database. When trying to log out with that token, the class looked up for a token is UDIDToken, NOT AccessToken. Same goes for other requests (other than logout). Is something missing in my configuration? Or is this a bug?

JuergenSimon avatar Oct 17 '17 14:10 JuergenSimon

No mate, is not a bug, you have 2 classes assuming there will be 2 different AccessTokens models. Patient hasMany accessTokens (replacing the name itself when the base is User, that is, everything), and the AccessToken in the Admin model. Well, that's not possible in this framework... yet. The solution to do that is using just one common AccessToken model, but with a polymorphic relation among the user models. In this link -> https://loopback.io/doc/en/lb3/Authentication-authorization-and-permissions.html#access-control-with-multiple-user-models you can find how to implement that. And BE AWARE with the issue #2516

Hope to have answered your question, Sebastián.

sebastianfelipe avatar Oct 18 '17 03:10 sebastianfelipe

Yeah, I dug a bit and found these lines in rest.js:

     if (app.isAuthEnabled) {
        var AccessToken = registry.getModelByType('AccessToken');
        handlers.push(loopback.token({model: AccessToken, app: app}));
      }

That's when I realised the current implementation would not support my use case and I had to re-model. Thanks for the response, though. I guess you can close the issue, unless you want to keep it around for implementing a fix?

PS: polymorphic won't fix my specific issue.

JuergenSimon avatar Oct 18 '17 12:10 JuergenSimon

Your welcome :). I don't have the privileges to close it, probably you could do it as owner of your issue.

sebastianfelipe avatar Oct 18 '17 13:10 sebastianfelipe

@JuergenSimon Can you share your solution ? . I have a same issue , i don't have a approach .

heyliamme avatar Oct 20 '17 03:10 heyliamme

I haven't implemented one yet, but I'm going to handle it like this:

  • Add a custom remote method for logging in the second class of users.
  • Get an authentication token from the user super class (in the remote method)
  • Handle ACL by using $authenticated
  • Log out can be handled normally.

JuergenSimon avatar Oct 20 '17 11:10 JuergenSimon

@JuergenSimon
Why must create new method login? When i login by default method , it created a new token and stored to database. It just does not recognize the corresponding token when i login . Get an authentication token from the user super , how can that be done ? Thanks

heyliamme avatar Nov 21 '17 04:11 heyliamme

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jan 20 '18 05:01 stale[bot]

This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.

stale[bot] avatar Feb 03 '18 05:02 stale[bot]

This behaviour should be more explicit. Also, the app should crash with an explicit message instead of just randomly picking one of the several Access Token classes available.

Ten0 avatar Feb 21 '19 15:02 Ten0