-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to make roles in loopback 4 #4291
Comments
@vucv138 hi, what do you mean by creating roles? like |
@vucv138, we're working on the documentation, see #3694. In the meanwhile, could you please check out this blog post series: https://strongloop.com/strongblog/building-an-online-game-with-loopback-4-pt7/? I'd like to close this issue as duplicate of strongloop/loopback#3694. Please continue the discussion over there if needed. Thanks. |
@dhmlau @deepakrkris @jannyHou The AuthorizationContext interface has a "roles" attribute, and in the authorization package README is a controller method decorated with It is nearly impossible for me to determine what the correct way of implementing role based authorization is. Maybe you can clear some things up for me. |
@deepakrkris @jannyHou, could you please help? Thanks. |
You can take a look at this extension, it can help you to use HRBAC authorization model |
@dhmlau @deepakrkris @jannyHou |
@RipkensLar sorry about the late reply. Please go through the shopping example , specifically the user order controller , to see how authorization decorator is used ( https://github.com/strongloop/loopback4-example-shopping/blob/master/packages/shopping/src/controllers/user-order.controller.ts ). The authorization section in the read me of the shopping app, gives details on how authorization has been enforced (https://github.com/strongloop/loopback4-example-shopping/blob/master/README.md#authorization) That said, there is an error that you have pointed out in the loopback.io documentation example under https://loopback.io/doc/en/lb4/Loopback-component-authorization.html#configuring-api-endpoints ,
|
@RipkensLar to give an easier pointer to start from the example shopping app , please take a look at https://github.com/strongloop/loopback4-example-shopping/blob/master/packages/shopping/src/application.ts#L79
The above binding tells the loopback authorization package what function will act as an authorizer for every api call. In our example shopping app that function is defined in the provider https://github.com/strongloop/loopback4-example-shopping/blob/master/packages/shopping/src/services/authorizor.ts Before doing all this the loopback authorization package itself should be registered (https://github.com/strongloop/loopback4-example-shopping/blob/master/packages/shopping/src/application.ts#L75) |
@RipkensLar other than the doc fix I have mentioned in comment #4291 (comment) if you feel the general readability of authorization docs https://loopback.io/doc/en/lb4/Loopback-component-authorization.html is obscure, please suggest improvements and I will have it updated. |
@RipkensLar please review and add your suggestions : #4361 |
@RipkensLar the extension suggested by @koliberr136a1 is for hierarchical roles and is not a generic implementation. The loopback shopping app has much simpler examples for authorization. |
Thanks for your detailed replies @deepakrkris. I understand how I would set the whole thing up, but the part that is missing for me is this right here:
Where do I have to define that a certain user has the role |
@RipkensLar , how and where user-role configuration is done is part of the developer's solution and LoopBack does not enforce any details. Please take a look at the acceptance tests in the authorization package, https://github.com/strongloop/loopback-next/tree/master/packages/authorization/src/__tests__/acceptance . |
Casbin supports to store roles (and permission assignment, we call them policy rules together) into file (rbac_policy.csv) or DB (all supported DB are listed in: https://casbin.org/docs/en/adapters). You can use either: https://casbin.org/docs/en/management-api |
@deepakrkris @hsluoyz
This is my rbac_model.conf:
And in my database I have these two rows:
But when I decorate a method in my controller with another role than |
@RipkensLar looks like you are missing the |
@deepakrkris ok, I added the
Which says that there is a role called |
I also tried to access to user's roles through the AuthorizationContext, but the roles are always just an empty array. Same for the scopes. I took a look into the authorization component and the context is set here: But I am unable to find another part where it actually gets the roles. Is this the desired behavior? |
@RipkensLar these are casbin specific questions and not LoopBack related. |
@RipkensLar if you would like to know what is the role of the user for the incoming request , you can check it in the authorizer function provided by you . for example , in the shopping example |
But where does loopback load the roles of the user for the incoming request? In the |
@RipkensLar that is a very valid question. I assumed that would be taken care by the authentication component. But looks like there is an overlap in functionalities between authentication and authorization components. I am checking with the team. I will reply back asap. |
@RipkensLar Sorry for the late reply, after reading through all the discussion I share your confusion and all the questions about building an RBAC system using
The role map is defined in the casbin policy file, like what you have as
When a request comes in, the user name should be included in the user profile so that And since your policy says The code you pointed out: is some feature we haven't developed, we now only has an abstraction for but haven't figured out a concrete plan of applying it in the authorization system. And that's why at this moment, the implementation of concept "Role" relies on your authorizer and 3rd party module like casbin. |
About the example You can design your app in a way that the {username: 'alice', roles: ['admin'], email: '[email protected]'} Then the authorization works in this way:
While the casbin module works in a more complicated way, here is the difference:
|
Thank you @deepakrkris and @jannyHou for your help. Got it working as described:
|
FYI - I submitted a PR to improve the basic use example in |
Closing as done since PR#4405 has merged. |
I am unable to get the "role" to be included in the a jwt token and also the principals object does not have it. Below is a snippet of loopback:authorization:interceptor Security context
Additionally, I am confused as to why the jwt encoding seems to only allow certain fields. For example, if I attempt to encode a userProfile with the kv "username": "john_doe" the loopback4's jwt encoding drops it but if I encode with "name": "john_doe" then it is included. What parts of my code would you need to see to lend assistance? |
Your docs not detail. help me please!
The text was updated successfully, but these errors were encountered: