You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+62-37Lines changed: 62 additions & 37 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,17 +22,7 @@
22
22
23
23
## Overview
24
24
25
-
A loopback-next extension for authorization in loopback applications. Its a very simplistic yet powerful and effective implementation using simple string based permissions.
26
-
27
-
It provides three ways of integration
28
-
29
-
1.**User level permissions only** - Permissions are associated directly to user. In this case, each user entry in DB contains specific array of permission keys.
30
-
2.**Role based permissions** - Permissions are associated to roles and users have a specific role attached. This actually reduces redundancy in DB a lot, as most of the time, users will have many common permissions. If that is not the case for you, then, use method #1 above.
31
-
3.**Role based permissions with user level override** - This is the most flexible architecture. In this case, method #2 is implemented as is. On top of it, we also add user-level permissions override, allow/deny permissions over role permissions. So, say there is user who can perform all admin role actions except he cannot remove users from the system. So, DeleteUser permission can be denied at user level and role can be set as Admin for the user.
32
-
33
-
[Extension enhancement using CASBIN authorisation](#Extension-enhancement-using-CASBIN-authorisation)
34
-
35
-
Refer to the usage section below for details on integration
25
+
A LoopBack 4 extension for Authorization Capabilities. It's very simple to integration yet powerful and effective.
36
26
37
27
## Install
38
28
@@ -46,9 +36,35 @@ For a quick starter guide, you can refer to our [loopback 4 starter](https://git
46
36
47
37
## Usage
48
38
39
+
### Ways of Integration:
40
+
41
+
On a higher level, it provides three ways of integration:
42
+
43
+
#### 1. User Level Permissions Only
44
+
45
+
Where permissions are associated directly to user. In this case, each user entry in DB contains specific array of permission keys.
46
+
47
+
#### 2. Role Based Permissions
48
+
49
+
Where permissions are associated to roles and users have a specific role attached. This actually reduces redundancy in DB a lot, as most of the time, users will have many common permissions. If that is not the case for you, then, use the first method.
50
+
51
+
#### 3. Role Based Permissions with User Level Flexibility
52
+
53
+
This is the most flexible architecture. In this case, method #2 is implemented as is.
54
+
55
+
On top of it, we also add user-level permissions override, allow/deny permissions over role permissions. So, say there is user who can perform all admin role actions except he cannot remove users from the system. So, DeleteUser permission can be denied at user level and role can be set as Admin for the user.
56
+
57
+
[Extension enhancement using CASBIN authorisation](#Extension-enhancement-using-CASBIN-authorisation)
58
+
59
+
Refer to the usage section below for details on integration.
60
+
49
61
In order to use this component into your LoopBack application, please follow below steps.
50
62
51
-
- Add component to application.
63
+
### Steps
64
+
65
+
#### Bind Component
66
+
67
+
Add `AuthorizationComponent` to your application, Like below:
If using method #3 from above, implement UserPermissionsOverride interface in User model and add user level permissions array as below.
107
127
Do this if there is a use-case of explicit allow/deny of permissions at user-level in the application.
108
128
You can skip otherwise.
109
129
@@ -128,7 +148,11 @@ export class User extends Entity implements UserPermissionsOverride<string> {
128
148
}
129
149
```
130
150
131
-
- For method #3, we also provide a simple provider function [_AuthorizationBindings.USER_PERMISSIONS_](<[./src/providers/user-permissions.provider.ts](https://github.com/sourcefuse/loopback4-authorization/blob/master/src/providers/user-permissions.provider.ts)>) to evaluate the user permissions based on its role permissions and user-level overrides. Just inject it
151
+
#### User Permissions Provider
152
+
153
+
For method #3, This extension exposes a provider function [AuthorizationBindings.USER_PERMISSIONS](https://github.com/sourcefuse/loopback4-authorization/blob/master/src/providers/user-permissions.provider.ts) to evaluate the user permissions based on its role permissions and user-level overrides.
- Add a step in custom sequence to check for authorization whenever any end
145
-
point is hit.
168
+
Add a step in custom sequence to check for authorization whenever any endpoint is hit.
146
169
147
170
```ts
148
171
import {inject} from'@loopback/context';
@@ -223,8 +246,7 @@ export class MySequence implements SequenceHandler {
223
246
224
247
The above sequence also contains user authentication using [loopback4-authentication](https://github.com/sourcefuse/loopback4-authentication) package. You can refer to the documentation for the same for more details.
225
248
226
-
- Now we can add access permission keys to the controller methods using authorize
227
-
decorator as below.
249
+
Now we can add access permission keys to the controller methods using authorize decorator as below:
- To Override the permissions provided in enum `permissionKey` file:
303
+
### Overriding Permissions
282
304
283
-
If the userPermission object is provided,it overrides the default permissions in the authorizationMetadata object.
305
+
API endpoints provided by ARC API (aka Sourceloop) services have their permissions pre-defined in them bundled.
284
306
285
-
For providing a userPermission object with custom permissions for specific controller methods, you can bind the following in `application.ts` file in your application.
307
+
In order to override them you can bind your custom permissions in the `AuthorizationBindings.PERMISSION` binding key.
308
+
This accepts an object that should have Controller class name as the root level key and the value of which is another object of method to permissions array mapping.
You can easily check the name of the controller and it's method name from the source code of the services or from the Swagger UI (clicking the endpoint in swagger append the controller and method name in the URL like `LoginController.login` where `login` is the method name).
326
+
327
+
## Serving the static files:
303
328
304
329
Authorization configuration binding sets up paths that can be accessed without any authorization checks, allowing static files to be served directly from the root URL of the application.The allowAlwaysPaths property is used to define these paths for the files in public directory i.e for a test.html file in public directory ,one can provide its path as follows:
If ,in case the file is in some other folder then `app.static()` can be called multiple times to configure the app to serve static assets from different directories.
344
+
If, in case the file is in some other folder then `app.static()` can be called multiple times to configure the app to serve static assets from different directories.
For more details,refer [here](https://loopback.io/doc/en/lb4/Serving-static-files.html#:~:text=One%20of%20the%20basic%20requirements,the%20API%20are%20explained%20below.)
328
353
329
-
# Extension enhancement using CASBIN authorisation
354
+
##Extension enhancement using CASBIN authorisation
330
355
331
356
As a further enhancement to these methods, we are using [casbin library](https://casbin.org/docs/en/overview) to define permissions at level of entity or resource associated with an API call. Casbin authorisation implementation can be performed in two ways:
332
357
333
358
1.**Using default casbin policy document** - Define policy document in default casbin format in the app, and configure authorise decorator to use those policies.
334
359
2.**Defining custom logic to form dynamic policies** - Implement dynamic permissions based on app logic in casbin-enforcer-config provider. Authorisation extension will dynamically create casbin policy using this business logic to give the authorisation decisions.
335
360
336
-
## Usage
361
+
### Casbin Usage
337
362
338
363
In order to use this enhacement into your LoopBack application, please follow below steps.
0 commit comments