home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 February / maximum-cd-2011-02.iso / DiscContents / digsby_setup85.exe / lib / ZSI / digest_auth.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-11-24  |  3.2 KB  |  93 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. import re
  5. from md5 import md5
  6. import random
  7. import time
  8. import httplib
  9. random.seed(int(time.time() * 10))
  10.  
  11. def H(val):
  12.     return md5(val).hexdigest()
  13.  
  14.  
  15. def KD(secret, data):
  16.     return H('%s:%s' % (secret, data))
  17.  
  18.  
  19. def A1(username, realm, passwd, nonce = None, cnonce = None):
  20.     if nonce and cnonce:
  21.         return '%s:%s:%s:%s:%s' % (username, realm, passwd, nonce, cnonce)
  22.     return '%s:%s:%s' % (username, realm, passwd)
  23.  
  24.  
  25. def A2(method, uri):
  26.     return '%s:%s' % (method, uri)
  27.  
  28.  
  29. def dict_fetch(d, k, defval = None):
  30.     if d.has_key(k):
  31.         return d[k]
  32.     return defval
  33.  
  34.  
  35. def generate_response(chaldict, uri, username, passwd, method = 'GET', cnonce = None):
  36.     authdict = { }
  37.     qop = dict_fetch(chaldict, 'qop')
  38.     domain = dict_fetch(chaldict, 'domain')
  39.     nonce = dict_fetch(chaldict, 'nonce')
  40.     stale = dict_fetch(chaldict, 'stale')
  41.     algorithm = dict_fetch(chaldict, 'algorithm', 'MD5')
  42.     realm = dict_fetch(chaldict, 'realm', 'MD5')
  43.     opaque = dict_fetch(chaldict, 'opaque')
  44.     nc = '00000001'
  45.     if not cnonce:
  46.         cnonce = H(str(random.randint(0, 10000000)))[:16]
  47.     
  48.     if algorithm.lower() == 'md5-sess':
  49.         a1 = A1(username, realm, passwd, nonce, cnonce)
  50.     else:
  51.         a1 = A1(username, realm, passwd)
  52.     a2 = A2(method, uri)
  53.     secret = H(a1)
  54.     data = '%s:%s:%s:%s:%s' % (nonce, nc, cnonce, qop, H(a2))
  55.     authdict['username'] = '"%s"' % username
  56.     authdict['realm'] = '"%s"' % realm
  57.     authdict['nonce'] = '"%s"' % nonce
  58.     authdict['uri'] = '"%s"' % uri
  59.     authdict['response'] = '"%s"' % KD(secret, data)
  60.     authdict['qop'] = '"%s"' % qop
  61.     authdict['nc'] = nc
  62.     authdict['cnonce'] = '"%s"' % cnonce
  63.     return authdict
  64.  
  65.  
  66. def fetch_challenge(http_header):
  67.     m = fetch_challenge.wwwauth_header_re.match(http_header)
  68.     if m is None:
  69.         raise RuntimeError, 'expecting "WWW-Authenticate header [Basic,Digest]"'
  70.     m is None
  71.     d = dict(challenge = m.groups()[0])
  72.     m = fetch_challenge.auth_param_re.search(http_header)
  73.     while m is not None:
  74.         (k, v) = http_header[m.start():m.end()].split('=')
  75.         d[k.lower()] = v[1:-1]
  76.         m = fetch_challenge.auth_param_re.search(http_header, m.end())
  77.     return d
  78.  
  79. fetch_challenge.wwwauth_header_re = re.compile('\\s*([bB]asic|[dD]igest)\\s+(?:[\\w]+="[^"]+",?\\s*)?')
  80. fetch_challenge.auth_param_re = re.compile('[\\w]+="[^"]+"')
  81.  
  82. def build_authorization_arg(authdict):
  83.     vallist = []
  84.     for k in authdict.keys():
  85.         vallist += [
  86.             '%s=%s' % (k, authdict[k])]
  87.     
  88.     return 'Digest ' + ', '.join(vallist)
  89.  
  90. if __name__ == '__main__':
  91.     print _copyright
  92.  
  93.