auth.py (2031B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\carbon\common\lib\cherrypy\lib\auth.py 2 import cherrypy 3 from cherrypy.lib import httpauth 4 5 def check_auth(users, encrypt = None, realm = None): 6 request = cherrypy.serving.request 7 if 'authorization' in request.headers: 8 ah = httpauth.parseAuthorization(request.headers['authorization']) 9 if ah is None: 10 raise cherrypy.HTTPError(400, 'Bad Request') 11 if not encrypt: 12 encrypt = httpauth.DIGEST_AUTH_ENCODERS[httpauth.MD5] 13 if hasattr(users, '__call__'): 14 try: 15 users = users() 16 if not isinstance(users, dict): 17 raise ValueError('Authentication users must be a dictionary') 18 password = users.get(ah['username'], None) 19 except TypeError: 20 password = users(ah['username']) 21 22 else: 23 if not isinstance(users, dict): 24 raise ValueError('Authentication users must be a dictionary') 25 password = users.get(ah['username'], None) 26 if httpauth.checkResponse(ah, password, method=request.method, encrypt=encrypt, realm=realm): 27 request.login = ah['username'] 28 return True 29 request.login = False 30 return False 31 32 33 def basic_auth(realm, users, encrypt = None, debug = False): 34 if check_auth(users, encrypt): 35 if debug: 36 cherrypy.log('Auth successful', 'TOOLS.BASIC_AUTH') 37 return 38 cherrypy.serving.response.headers['www-authenticate'] = httpauth.basicAuth(realm) 39 raise cherrypy.HTTPError(401, 'You are not authorized to access that resource') 40 41 42 def digest_auth(realm, users, debug = False): 43 if check_auth(users, realm=realm): 44 if debug: 45 cherrypy.log('Auth successful', 'TOOLS.DIGEST_AUTH') 46 return 47 cherrypy.serving.response.headers['www-authenticate'] = httpauth.digestAuth(realm) 48 raise cherrypy.HTTPError(401, 'You are not authorized to access that resource')