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

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. import Rand
  5. import m2
  6. import Cookie
  7. import binascii
  8. import re
  9. import time
  10. _MIX_FORMAT = 'exp=%s&data=%s&digest='
  11. _MIX_RE = re.compile('exp=(\\d+\\.\\d+)&data=(.+)&digest=(\\S*)')
  12.  
  13. def mix(expiry, data, format = _MIX_FORMAT):
  14.     return format % (repr(expiry), data)
  15.  
  16.  
  17. def unmix(dough, regex = _MIX_RE):
  18.     mo = regex.match(dough)
  19.     if mo:
  20.         return (float(mo.group(1)), mo.group(2))
  21.     return None
  22.  
  23.  
  24. def unmix3(dough, regex = _MIX_RE):
  25.     mo = regex.match(dough)
  26.     if mo:
  27.         return (float(mo.group(1)), mo.group(2), mo.group(3))
  28.     return None
  29.  
  30. _TOKEN = '_M2AUTH_'
  31.  
  32. class AuthCookieJar:
  33.     _keylen = 20
  34.     
  35.     def __init__(self):
  36.         self._key = Rand.rand_bytes(self._keylen)
  37.  
  38.     
  39.     def _hmac(self, key, data):
  40.         return binascii.b2a_base64(m2.hmac(key, data, m2.sha1()))[:-1]
  41.  
  42.     
  43.     def makeCookie(self, expiry, data):
  44.         dough = mix(expiry, data)
  45.         return AuthCookie(expiry, data, dough, self._hmac(self._key, dough))
  46.  
  47.     
  48.     def isGoodCookie(self, cookie):
  49.         if cookie.isExpired():
  50.             return 0
  51.         c = self.makeCookie(cookie._expiry, cookie._data)
  52.         if c._expiry == cookie._expiry and c._data == cookie._data and c._mac == cookie._mac:
  53.             pass
  54.         return c.output() == cookie.output()
  55.  
  56.     
  57.     def isGoodCookieString(self, cookie_str):
  58.         c = Cookie.SmartCookie()
  59.         c.load(cookie_str)
  60.         if not c.has_key(_TOKEN):
  61.             return 0
  62.         undough = unmix3(c[_TOKEN].value)
  63.         if undough is None:
  64.             return 0
  65.         (exp, data, mac) = undough
  66.         c2 = self.makeCookie(exp, data)
  67.         if not c2.isExpired():
  68.             pass
  69.         return c2._mac == mac
  70.  
  71.  
  72.  
  73. class AuthCookie:
  74.     
  75.     def __init__(self, expiry, data, dough, mac):
  76.         self._expiry = expiry
  77.         self._data = data
  78.         self._mac = mac
  79.         self._cookie = Cookie.SmartCookie()
  80.         self._cookie[_TOKEN] = '%s%s' % (dough, mac)
  81.         self._name = '%s%s' % (dough, mac)
  82.  
  83.     
  84.     def expiry(self):
  85.         return self._expiry
  86.  
  87.     
  88.     def data(self):
  89.         return self._data
  90.  
  91.     
  92.     def mac(self):
  93.         return self._mac
  94.  
  95.     
  96.     def output(self):
  97.         return self._cookie.output()
  98.  
  99.     
  100.     def value(self):
  101.         return self._cookie[_TOKEN].value
  102.  
  103.     
  104.     def isExpired(self):
  105.         return time.time() > self._expiry
  106.  
  107.     
  108.     def name(self):
  109.         return self._name
  110.  
  111.     
  112.     def headerValue(self):
  113.         return self.value()
  114.  
  115.  
  116.