Repository navigation
Description
Hi friends,
There is a big problem. I've implemented a solution, but I think there have to be a real update from the loopback team. Here the case:
- There are 2 users, userA and userB. Both extend from the User model
- userA has a lot of rows with users already registered from 1 to n, and userB from 1 to m
- You loggin with userA, and you can do for example GET userAs/idGivenWhenLoggedIn and you will have an answer, but...
- You can also do GET userBs/idGivenWhenLoggedIn, without have logged in with userB. This issue is when both share the same userId, case it doesn't discriminate with the model used to log in, just the userId -> poblem.
This is very insecure, that can drive many developers that haven't found a solution yet, to make mistakes and maybe without realising.
The same thing is when it used the $owner and the $authenticate, etc. to create ACL rules. I implemented a solution to those cases, adding a custom "roleResolver", extending an accessTokenCustom model from the accessToken model, and adding the attribute "model" when some model logged in (the ones that extend from the User model). But those things, doesn't solve the initial problem, planted above.
One thing we can do is:
- Implement my solution + reset the ACLs (can that be possible?) + implement everything from scratch, without using the $authenticated and $owner roles
- Loopback developers, implement those changes, because it drives us to many errors that are hard to handle in small and, overall, big platforms.
The solution I comment above, is that (I wrote those lines in a general way):
- RoleResolver:
Role.registerResolver("customRole", function(role, context, cb) {
function reject(err) {
if(err) {
return cb(err);
}
cb(null, false);
}
if(!context.accessToken.userId) {
console.log("anonymous user is logged in");
return reject(); // do not allow anonymous users
}
if (context.accessToken.model == "Model") {
if (context.modelName == "Model")
{
if (context.modelId == context.accessToken.userId)
{
console.log("Model is logged in with id");
return cb(null, true);
}
}
else
{
console.log("Model is logged in");
return cb(null, true);
}
}
reject();
});
- ACL (is an example of the use)
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "customRole",
"permission": "ALLOW"
}
- AccessTokenCustom (model based in AccessToken)
{
"name": "AccessTokenCustom",
"base": "AccessToken",
"idInjection": true,
"options": {
"validateUpsert": true
}
-
Replace accessTokens relations in models extended from the User model:
"relations": { "accessTokens": { "type": "hasMany", "model": "AccessTokenCustom", "foreignKey": "userId" } } -
Finally, fill the model attribute when a user log in:
userExtendedFromUserModel.prototype.createAccessToken = function(ttl, options, cb) {
if (cb === undefined && typeof options === "function") {
// createAccessToken(ttl, cb)
cb = options;
options = undefined;
}
cb = cb || utils.createPromiseCallback();
if (typeof ttl === "object" && !options) {
// createAccessToken(options, cb)
options = ttl;
ttl = options.ttl;
}
options = options || {};
var userModel = this.constructor;
ttl = Math.min(ttl || userModel.settings.ttl, userModel.settings.maxTTL);
this.accessTokens.create({
ttl: ttl,
model: userModel.modelName
}, cb);
return cb.promise;
};
This solution that I created with the help of some answers, most of them from this issues, is not enough. We really need something powerful, something from the root. In this moment I'm working in a project and I need to find a elegant way to do these things.
That solution doesn't solve the problem I presented above. Maybe it could be solved using a extended User model without inherit its ACL rules and the solution commented. Supposing this case, there is some way to extend a model from a base model without inherit its ACL rules? If the answer is yes, I really appreciate know "how" (for now), otherwise what can I do to solve the main problem?
For now, I suggest (from your side, the loopback team):
- Add the "Model Name" as a column in the AccessToken, that would be very useful to implement custom role resolvers and also to not modifying the prototype method (is not a good idea do that)
- Enable the model option to reset the ACL rules when you extend from a model.
With both, is possible to implement better solutions using loopback. I believe there are better ones rather than mine, but ok, it's a good point to start with, and are not very complex to implement in a release (soon please, I really need it).
I'd really appreciate your help.
Thanks,
Sebastián.
Activity
[-]Multi-User Problem Using ACL[/-][+]Multi-User Problem Using ACL and Extended Models Issues[/+]richardpringle commentedon Aug 24, 2016
@bajtos @loay, please review.
richardpringle commentedon Aug 24, 2016
@sebastianfelipe
With the current
AccessToken/loginimplementation, I think it's safe to say that LoopBack does not support an application with multiple User models without unique IDs between them.Perhaps we can make that a future consideration.
cc/ @raymondfeng
richardpringle commentedon Aug 24, 2016
@crandmck
It might be a good idea to specifically document the limitations of extending the built-in User model, what do you think?
raymondfeng commentedon Aug 24, 2016
The current version of LoopBack has to pick one official User/AccessToken as there is no concept of user partition. With multiple user registries, a discriminator is needed, for example, realm or organization so that user federation/delegation can happen.
For future releases, we are evaluating it as part of multi-tenancy support. For example, we can adopt the github model, which consists of organization/team/user.
crandmck commentedon Aug 25, 2016
Yes, we should add this. How would you word it? Perhaps something like:
Does that sound right?
21 remaining items
sebastianfelipe commentedon May 29, 2017
Hi. I unerstood that this feature was implemented in Loopback 3. But to make it real, you have to specify a polymorphic relation, between user models and the access token model. Did you do that? :)
hendratommy commentedon May 29, 2017
Hi,
I did. I did exactly like the doc said. Below is my models:
You can download my test project at:
https://drive.google.com/open?id=0B1PeypjU-qrGY0k5V0lzLUtrQk0
EDIT
Not sure if this related. But even though i set ACL to deny everyone but i still can access /:id
Or did i have wrong concept about ACL?
ebarault commentedon May 29, 2017
hi, yes, actually the built-in dynamic role
$ownerdoes not currently support multi-user models in case 2 users have the same id, which can happen with SQL dbs, but never happens with MongoDB for example.I have a pull-request in progress to address this.
What db are you using?
this last comment makes me think you have something badly configured. it should not work.
sebastianfelipe commentedon May 29, 2017
So... Why this thread is closed? Specifically, that was the problem before and what I see is that is not solved yet. Well, you always can create a role-resolver with a custom owner, comparing de moldeName and modelId from the url with the model name and id from the fetched access token. If you need it, you can do it as well. Cheers.
ebarault commentedon May 29, 2017
well, it is very specific to the $owner dynamic role which is currently very limited. the solution provided with multi-user support allows you to create a custom role resolver.
i'd suggest to open a new issue rather than reopening this one.
hendratommy commentedon May 29, 2017
I'm using mysql db in my actual project, but the test project i've uploaded earlier use in-memory db.
In my test project, it just an simple project. I just create new empty serve with 3 models. So i don't think i've add weird things. I will double check this.
EDIT:
I have double check this, tried to downgrade to 3.7.0, using just one extended User model (Customer) with built in AccessToken, etc., but cannot make it work. Just want to be clear below is my ACL:
I got access denied if i do not logged in (as expected), but if I login using Customer with id=1. I got access denied when accessing /Customers/2 as expected but if i access /Customers/1 it return successfuly which is not expected. Any advice what am i missing?
EDIT 2:
So, looking at role.js it is seems accessing User type model is default permission which will return true if the target id and principalId is match. So i think, defining ACL as $owner in User model will not affect anything. It's kind of makes sense though.
As for $owner with multiple users, i think i will go with custom role resolver for now :)
ebarault commentedon May 29, 2017
i'm sorry, but it's quite hard to investigate like this. Could you fork and share on your github account the loopback sandbox?
https://github.com/strongloop/loopback-sandbox
hendratommy commentedon May 30, 2017
Hi,
Sure. Here is my fork from loopback-sandbox:
https://github.com/hendratommy/my-loopback-sandbox-defaultacl
sebastianfelipe commentedon Jul 29, 2017
Hey man, sorry for the late. To prevent that bug use what I proposed above. Is a patch over the problem. It doesn't rely on the polymorphic one. I have used it in some projects and make the correspondents tests and everything works perfectly. Cheers.
sebastianfelipe commentedon Aug 28, 2017
This thread is unsolved yet. Recently I implemented the polymorphic relation and the $owner permission is not enough. Is necessary to explicitly implement a verification of the modelName or the principalType as it is implemented in the polymorphic relation. That means if your entering to /Admin/2, then the accessToken needs to be userId: 1 AND principalType: Admin, not other. In my case, I could use the resource (data from an specific endpoint), using an accessToken from an Admin2. Thanks.
sebastianfelipe commentedon Aug 28, 2017
Adding to this possible solution, in the model definition "common/models/role.js", line 224 Role.isOwner contains the necessary method to validate if a User is an owner or not. In that case, there is an operation that validate if the user is a User class or a SubClass of a User class. There is the problem.
First, in line 250 -> if (isUserClass(modelClass)), where is the verification if the model class is a user model or a subclass of a user model. Nested to that, in line 253 -> if (principalType === Principal.USER || principalType === userModelName), we finally find that the OR logic operation is not enough to discriminate whether the model is DIRECTLY related to the model, and not a submodel or something similar. For example, a User class inherits its properties to a CustomUser1 and a CustomUser2, using the lines 250 and 253, a CustomUser2 can use a CustomUser1 resource with an $owner permission, because is a sub class from a User model, and the line 250 is passing it, and then the OR operation do the same. For me that is the mistake we need to solve, that needs to be related EXACTLY with the modelName used in the endpoint, not a subclass or a submodel from a any User class.
If I'm right, there is the solution of it and I think no extra patch is needed from us as developers. I hope to be right and have helped to solve it.
Thanks,
Sebastián.
Lahrachtdi commentedon Aug 30, 2017
the built-in dynamic role $owner still not working with multiple-user model even when we do exactly what's in the docs, also there is some mistakes in the doc when they give solutions for static roles assignments for multiple-user models, but still don't work, I started redefining dynamic roles myself and I think this gonna work, here is my custom dynamic role $owner definition, maybe it can help you.
sebastianfelipe commentedon Aug 31, 2017
Yes, I have custom roles too, but is not the idea if that role exists by default.
ghassenlassoued commentedon Mar 2, 2018
Hi , since this discussion was 6 months ago, I need to ask again if there are any solutions ?
codekeyz commentedon Jan 15, 2019
Have been working with Loopback for a while now, decided to make this starter for building multi-user api.
Check this repo https://github.com/codekeyz/loopback-multirole-api-starter