@@ -188,3 +188,85 @@ test_file = container.create_file('text.txt', 'text/plain')
188188test_file.upload(file_like_io.read())
189189```
190190
191+ ## Authorization
192+
193+ Core offers a simple way of verifying the authorization of a user and their role.
194+
195+ Checking the permission involves three steps
196+ 1 . Preparing a permission request object
197+ 2 . Getting an authorizer object from core
198+ 3 . Requesting if the permission is valid/true
199+
200+ ### Preparing the permission request
201+
202+ ``` python
203+ from python_ms_core.core.auth.models.permission_request import PermissionRequest
204+
205+ permission_request = PermissionRequest(
206+ user_id = ' <userID>' ,
207+ org_id = ' <orgID>' ,
208+ should_satisfy_all = False ,
209+ permissions = [' permission1' , ' permission2' ]
210+ )
211+
212+ ```
213+
214+ In the above example, ` should_satisfy_all ` helps in figuring out if all the permissions are needed or any one of the permission is sufficient.
215+
216+ ### Getting the authorizer from core
217+
218+ Core exposes ` get_authorizer ` method with 2 parameters
219+
220+ 1 . ` request_params ` parameter which is instance of ` PermissionRequest ` class (Mandatory Parameter).
221+ 2 . ` config ` parameter (Optional)
222+
223+ There are two types of ` Authorizer ` objects in core.
224+ 1 . HostedAuthorizer: checks the permissions against a hosted API
225+ 2 . SimulatedAuthorizer: makes a simulated authorizer used for local/non-hosted environment.
226+
227+ The following code demonstrates getting the simulated and hosted authorizer
228+ ``` python
229+ from python_ms_core import Core
230+ core = Core()
231+ # HostedAuthorizer
232+
233+ hosted_authorizer = core.get_authorizer(config = {' provider' : ' Hosted' , ' api_url' : ' <AUTH_API_URL>' })
234+
235+ simulated_authorizer = core.get_authorizer(config = {' provider' : ' Simulated' })
236+
237+ ```
238+ In case ` api_url ` is not provided for ` Hosted ` auth provider, the core will pick it up from environment variable ` AUTHURL `
239+
240+ #### Requesting if certain permission is valid:
241+
242+ Use the method ` has_ermission(request_params) ` to know if the permission request is valid/not.
243+
244+ ``` python
245+
246+ # Complete Example
247+ from python_ms_core import Core
248+ from python_ms_core.core.auth.models.permission_request import PermissionRequest
249+
250+ core = Core()
251+
252+
253+ permission_request = PermissionRequest(
254+ user_id = ' <userID>' ,
255+ org_id = ' <orgID>' ,
256+ should_satisfy_all = False ,
257+ permissions = [' permission1' , ' permission2' ]
258+ )
259+
260+ # With Hosted provider
261+ auth_provider = core.get_authorizer(config = {' provider' : ' Hosted' , ' api_url' : ' <AUTH_API_URL>' })
262+ response = auth_provider.has_permission(request_params = permission_request)
263+ # Response will be boolean
264+
265+ # With Simulated provider
266+ auth_provider = core.get_authorizer(config = {' provider' : ' Simulated' })
267+ response = auth_provider.has_permission(request_params = permission_request)
268+ # Response will be boolean
269+ ```
270+
271+ #### How does Simulated authentication work?
272+ With simulated authentication, the method ` has_permission ` simply returns the value given in ` should_satisfy_all ` property in the permission request.
0 commit comments