loopback-next icon indicating copy to clipboard operation
loopback-next copied to clipboard

Extension: Add Authorization component

Open dhmlau opened this issue 9 years ago • 20 comments

Related to https://github.com/strongloop/loopback-next/issues/512

  • decoration at controller/method level may not be flexible enough
  • URL-based rules as an alternative
  • fine-grained access control on per-model-instance basis - ideally solved at Repository level
  • local User/AccessToken auth scheme and models
  • ACL/Role-based rules vs. scope-based rules
  • JWT
  • OAuth

dhmlau avatar Aug 24 '17 19:08 dhmlau

Marking as Core-GA per our discussion.

dhmlau avatar Aug 24 '17 19:08 dhmlau

As a general framework approach for authorization, I've used Pundit extensively in Ruby on Rails and found it extremely flexible and powerful. SailsJs has something similar https://sailsjs.com/documentation/concepts/policies

Pundit is really a documented pattern and a support library implementing it in very little code. It imposes a minimum of assumptions on what your authentication logic is, since it boils down to :

  1. a plain Ruby "policy" class for each model class, with a) a constructor which receives a user model and a model object or class and stores them as instance variables b) methods loosely named after actions the user would ask for authorization to perform on that model. These methods return true if the user should be able to perform the action on the model, otherwise false.
  2. utility methods for assisting the process of creating an instance of the right policy object

Thats it! How you implement the methods in b) is entirely arbitrary, and since it is just a plain class/object, you can use inheritance or whatever you like, and application code can arbitrarily instantiate a policy object and query it anywhere.

I believe this approach can be adopted and recommended at a framework level, and used directly by app developers, or used to implement other authorization strategies.

buzzware avatar Jan 05 '19 16:01 buzzware

Is there going to be a User and Role model included in the authorization framework, or will that be provided by the app?

Will the framework concern be annotation on the controllers to describe authorized roles per path/verb?

elv1s avatar Mar 20 '19 23:03 elv1s

I would proposal we provide built-in User and Role models and allow app developers replace them.

jannyHou avatar Mar 26 '19 17:03 jannyHou

Is there going to be a User and Role model included in the authorization framework, or will that be provided by the app?

At the moment, we don't have a plan to ship built-in User/Role models. There are probably some interfaces to capture the basic requirements for such entities if necessary.

Will the framework concern be annotation on the controllers to describe authorized roles per path/verb?

I expect to have decorators to allow developers express their authorization requirements at controller class/method level.

raymondfeng avatar Mar 26 '19 20:03 raymondfeng

+1 to for leaving out User/Role models from the core authorization framework. In my experience from LB3, built-in User/Role models were difficult to maintain and very often not flexible enough to support different real-world use cases.

However, I think it probably makes sense to provide a standalone package offering a minimal implementation of User & Role models for demonstration purposes. LB4 users can then use this minimal implementation to build their own User & Role classes.

bajtos avatar Mar 28 '19 09:03 bajtos

@bajtos We did implement a separate authorisation component in our first lb4 application. It was implemented as decorators at method level in controllers. The permissions were picked from access token (JWT). The implementation was not as vast as mentioned here, but it was good workable solution for us. If there is any way I can help/contribute to this task, please do let me know. I can share our implementation if needed. But it was more or less following the same guidelines mentioned here for authentication component. These guidelines were really helpful.

samarpanB avatar Apr 04 '19 07:04 samarpanB

@samarpanB, I think that would be helpful if you could add documentation on how to create such component.
I'm thinking to add LoopBack Component > Authorization on the sidebar where the content from @samarpanB will be on that page.
Thoughts, @samarpanB @strongloop/loopback-maintainers?

dhmlau avatar Apr 07 '19 13:04 dhmlau

Related: @osmanmesutozcan has created an extension https://github.com/xanthous-tech/loopback-4-accesscontrol (mentioned in https://github.com/strongloop/loopback-next/issues/2254)

dhmlau avatar Apr 07 '19 13:04 dhmlau

@dhmlau Sure. That's a good idea. I'll put that documentation up. Can you please help me with where to start ? I think https://github.com/strongloop/loopback-next/tree/master/docs/site is the place where all pages md files are kept. So I need to create a new one here, right ?

samarpanB avatar Apr 08 '19 07:04 samarpanB

@samarpanB , you're right. For sidebar, it's from https://github.com/strongloop/loopback-next/blob/master/docs/site/sidebars/lb4_sidebar.yml. Thanks!

dhmlau avatar Apr 08 '19 18:04 dhmlau

@dhmlau added documenation in above PR. Please have a look.

samarpanB avatar Apr 10 '19 11:04 samarpanB

Hi everone,

I want to check whether user loged in berfore check I check his/her permission, I dont want to add "@authenticate" decorator to method when i added "@access" decorator to it (option configure the access control). How can I do that.

lvlinh961 avatar Apr 11 '19 09:04 lvlinh961

@raymondfeng I was trying to look for the doc page on https://loopback.io/doc/en/lb4/Using-components.html. But it’s not there yet. Is it not yet moved there ? As per my understanding, there is a daily job which moves it there.

samarpanB avatar Apr 20 '19 03:04 samarpanB

We need to publish a release to npm 1st.

raymondfeng avatar Apr 20 '19 03:04 raymondfeng

@samarpanB I just made a release. The document site will be updated soon.

raymondfeng avatar Apr 20 '19 04:04 raymondfeng

@samarpanB, it's up: https://loopback.io/doc/en/lb4/Loopback-component-authorization.html. Thanks for your contribution!

dhmlau avatar Apr 22 '19 14:04 dhmlau

Thanks for your work @samarpanB. I've just been looking at your implementation.

Could I ask if there are plans to map roles to scopes at this point in time? Personally I'd find an implementation or roles based on oath scopes a logical path to follow especially in conjunction jwt for auth.

EDIT: NM Found this: https://github.com/strongloop/loopback-next/issues/1035#issuecomment-429774436

dougal83 avatar May 08 '19 14:05 dougal83

See https://github.com/strongloop/loopback-next/pull/1205

raymondfeng avatar May 15 '19 17:05 raymondfeng

Closing as done.

dhmlau avatar Aug 21 '19 14:08 dhmlau