comparison Sphinx/source/plugins/python.rst @ 556:6a3d48510b0b

Python sample: "Forbid or allow access to REST resources (authorization)", deprecating advanced authorization plugin
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 10 Dec 2020 12:45:13 +0100
parents 6fb469a3c382
children 090cc988c35e
comparison
equal deleted inserted replaced
555:6fb469a3c382 556:6a3d48510b0b
708 will generate a workbook with one sheet that contains the list of 708 will generate a workbook with one sheet that contains the list of
709 studies, with the patient ID, the patient name and the study 709 studies, with the patient ID, the patient name and the study
710 description. 710 description.
711 711
712 712
713 .. _python_authorization:
714
715 Forbid or allow access to REST resources (authorization)
716 ........................................................
717
718 .. highlight:: python
719
720 The following Python script installs a callback that is triggered
721 whenever the HTTP server of Orthanc is accessed::
722
723 import orthanc
724 import pprint
725
726 def Filter(uri, **request):
727 print('User trying to access URI: %s' % uri)
728 pprint.pprint(request)
729 return True # False to forbid access
730
731 orthanc.RegisterIncomingHttpRequestFilter(Filter)
732
733 If access is not granted, the ``Filter`` callback must return
734 ``False``. As a consequence, the HTTP status code would be set to
735 ``403 Forbidden``. If access is granted, the ``Filter`` must return
736 ``true``. The ``request`` argument contains more information about the
737 request (such as the HTTP headers, the IP address of the caller and
738 the GET arguments).
739
740 Note that this is similar to the ``IncomingHttpRequestFilter()``
741 callback that is available in :ref:`Lua scripts <lua-filter-rest>`.
742
743 Thanks to Python, it is extremely easy to call remote Web services for
744 authorization. Here is an example using the ``requests`` library::
745
746 import json
747 import orthanc
748 import requests
749
750 def Filter(uri, **request):
751 body = {
752 'uri' : uri,
753 'headers' : request['headers']
754 }
755 r = requests.post('http://localhost:8000/authorize',
756 data = json.dumps(body))
757 return r.json() ['granted'] # Must be a Boolean
758
759 orthanc.RegisterIncomingHttpRequestFilter(Filter)
760
761 .. highlight:: javascript
762
763 This filter could be used together with the following Web service
764 implemented using `Node.js
765 <https://en.wikipedia.org/wiki/Node.js>`__::
766
767 const http = require('http');
768
769 const requestListener = function(req, res) {
770 let body = '';
771 req.on('data', function(chunk) {
772 body += chunk;
773 });
774 req.on('end', function() {
775 console.log(JSON.parse(body));
776 var answer = {
777 'granted' : false // Forbid access
778 };
779 res.writeHead(200);
780 res.end(JSON.stringify(answer));
781 });
782 }
783
784 http.createServer(requestListener).listen(8000);
785
786
787
788
713 Performance and concurrency 789 Performance and concurrency
714 --------------------------- 790 ---------------------------
715 791
716 **Important:** This section only applies to UNIX-like systems. The 792 **Important:** This section only applies to UNIX-like systems. The
717 ``multiprocessing`` package will not work on Microsoft Windows as the 793 ``multiprocessing`` package will not work on Microsoft Windows as the