test_auth_digest.py (4214B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\carbon\common\lib\cherrypy\test\test_auth_digest.py 2 import cherrypy 3 from cherrypy.lib import auth_digest 4 from cherrypy.test import helper 5 6 class DigestAuthTest(helper.CPWebCase): 7 8 def setup_server(): 9 10 class Root: 11 12 def index(self): 13 return 'This is public.' 14 15 index.exposed = True 16 17 class DigestProtected: 18 19 def index(self): 20 return "Hello %s, you've been authorized." % cherrypy.request.login 21 22 index.exposed = True 23 24 def fetch_users(): 25 return {'test': 'test'} 26 27 get_ha1 = cherrypy.lib.auth_digest.get_ha1_dict_plain(fetch_users()) 28 conf = {'/digest': {'tools.auth_digest.on': True, 29 'tools.auth_digest.realm': 'localhost', 30 'tools.auth_digest.get_ha1': get_ha1, 31 'tools.auth_digest.key': 'a565c27146791cfb', 32 'tools.auth_digest.debug': 'True'}} 33 root = Root() 34 root.digest = DigestProtected() 35 cherrypy.tree.mount(root, config=conf) 36 37 setup_server = staticmethod(setup_server) 38 39 def testPublic(self): 40 self.getPage('/') 41 self.assertStatus('200 OK') 42 self.assertHeader('Content-Type', 'text/html;charset=utf-8') 43 self.assertBody('This is public.') 44 45 def testDigest(self): 46 self.getPage('/digest/') 47 self.assertStatus(401) 48 value = None 49 for k, v in self.headers: 50 if k.lower() == 'www-authenticate': 51 if v.startswith('Digest'): 52 value = v 53 break 54 55 if value is None: 56 self._handlewebError('Digest authentification scheme was not found') 57 value = value[7:] 58 items = value.split(', ') 59 tokens = {} 60 for item in items: 61 key, value = item.split('=') 62 tokens[key.lower()] = value 63 64 missing_msg = '%s is missing' 65 bad_value_msg = "'%s' was expecting '%s' but found '%s'" 66 nonce = None 67 if 'realm' not in tokens: 68 self._handlewebError(missing_msg % 'realm') 69 elif tokens['realm'] != '"localhost"': 70 self._handlewebError(bad_value_msg % ('realm', '"localhost"', tokens['realm'])) 71 if 'nonce' not in tokens: 72 self._handlewebError(missing_msg % 'nonce') 73 else: 74 nonce = tokens['nonce'].strip('"') 75 if 'algorithm' not in tokens: 76 self._handlewebError(missing_msg % 'algorithm') 77 elif tokens['algorithm'] != '"MD5"': 78 self._handlewebError(bad_value_msg % ('algorithm', '"MD5"', tokens['algorithm'])) 79 if 'qop' not in tokens: 80 self._handlewebError(missing_msg % 'qop') 81 elif tokens['qop'] != '"auth"': 82 self._handlewebError(bad_value_msg % ('qop', '"auth"', tokens['qop'])) 83 get_ha1 = auth_digest.get_ha1_dict_plain({'test': 'test'}) 84 base_auth = 'Digest username="test", realm="wrong realm", nonce="%s", uri="/digest/", algorithm=MD5, response="%s", qop=auth, nc=%s, cnonce="1522e61005789929"' 85 auth_header = base_auth % (nonce, '11111111111111111111111111111111', '00000001') 86 auth = auth_digest.HttpDigestAuthorization(auth_header, 'GET') 87 ha1 = get_ha1(auth.realm, 'test') 88 response = auth.request_digest(ha1) 89 auth_header = base_auth % (nonce, response, '00000001') 90 self.getPage('/digest/', [('Authorization', auth_header)]) 91 self.assertStatus(401) 92 base_auth = 'Digest username="test", realm="localhost", nonce="%s", uri="/digest/", algorithm=MD5, response="%s", qop=auth, nc=%s, cnonce="1522e61005789929"' 93 auth_header = base_auth % (nonce, '11111111111111111111111111111111', '00000001') 94 auth = auth_digest.HttpDigestAuthorization(auth_header, 'GET') 95 ha1 = get_ha1('localhost', 'test') 96 response = auth.request_digest(ha1) 97 auth_header = base_auth % (nonce, response, '00000001') 98 self.getPage('/digest/', [('Authorization', auth_header)]) 99 self.assertStatus('200 OK') 100 self.assertBody("Hello test, you've been authorized.")