I've reviewed the documentation at http://cherrypy.readthedocs.io/en/latest/basics.html#authentication in order to understand how to send my API call to Cherrypy and validate an API key that I'll have in the header of the HTTP request that I'll send from my client side program. The header will follow Basic Authorization with the header having the following example key and value Example: Authorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l
Then I want the Cherrypy function that I write to run only after some authorization has be completed. From the client, I'll call my function like: https:///myfunction?param1=value¶m2=value¶m3=value with the Basic Authorization header set up as seen above
and in Cherrypy I'll code the function like:
@cherrypy.expose
def myfunction(self, param1=1,param2=cat,param3=dog):
# do my work in the function
return
Note: the function will not have a user enter any credentials. The call will pre-populate the basic authorization header programmatically.
Can you set up the Cherrypy code example in such a way to explicitly show me how this can be achieved. Assume a beginner with Cherrypy (e.g. did the first 5 or so tutorials only ( http://docs.cherrypy.org/en/latest/tutorials.html#tutorials ). Thanks much.
def check_auth():
needs_auth = cherrypy.request.config.get('auth.require', False)
if needs_auth and not cherrypy.request.header.get('X-HTTP-APIKEY', None) == <your key here>:
raise cherrypy.HTTPError(404)
cherrypy.tools.auth = cherrypy.Tool('before_handler', check_auth, priority=50)
def needsauth():
'''A decorator that sets auth.require config
variable.'''
def decorate(f):
if not hasattr(f, '_cp_config'):
f._cp_config = dict()
if 'auth.require' not in f._cp_config:
f._cp_config['auth.require'] = []
f._cp_config['auth.require'] = True
return f
return decorate
@cherrypy.expose
@needsauth
def myfunction(....):
.....
The server encountered an unexpected condition which prevented it from fulfilling the request.
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/cherrypy/_cprequest.py", line 631, in respond
self._do_respond(path_info)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/cherrypy/_cprequest.py", line 690, in _do_respond
response.body = self.handler()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/cherrypy/lib/encoding.py", line 264, in __call__
ct.params['charset'] = self.find_acceptable_charset()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/cherrypy/lib/encoding.py", line 173, in find_acceptable_charset
if encoder(self.default_encoding):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/cherrypy/lib/encoding.py", line 114, in encode_string
for chunk in self.body:
TypeError: 'HelloWorld' object is not iterable
import cherrypy
def check_auth():
needs_auth = cherrypy.request.config.get('auth.require', False)
if needs_auth and not cherrypy.request.headers.get('X-HTTP-APIKEY', None) =="1234":
raise cherrypy.HTTPError(401)
cherrypy.tools.auth = cherrypy.Tool('before_handler', check_auth, priority=50)
def needsauth():
'''A decorator that sets auth.require config
variable.'''
def decorate(f):
if not hasattr(f, '_cp_config'):
f._cp_config = dict()
if 'auth.require' not in f._cp_config:
f._cp_config['auth.require'] = []
f._cp_config['auth.require'] = True
return f
return decorate
class main(object):
_cp_config = {'tools.auth.on': True,}
@cherrypy.expose
@needsauth()
def myfunction(self):
print 1
return '<html>Hi</html>'
cherrypy.quickstart(main())