# Path: xclib/utf8.py
# def utf8(u, opts='strict'):
#     return u.encode('utf-8', opts)
# 
# def unutf8(u, opts='strict'):
#     if opts == 'illegal':
#         try:
#             return u.decode('utf-8', 'strict')
#         except UnicodeError:
#             dec = u.decode('utf-8', 'ignore')
#             logging.error('Illegal UTF-8 sequence: %r' % dec)
#             sys.stderr.write('Illegal UTF-8 sequence: %r\n' % dec)
#             traceback.print_exc()
#             return 'illegal-utf8-sequence-' + dec
#     else:
#         return u.decode('utf-8', opts)


import logging
import hashlib
import hmac
import bcrypt
from datetime import datetime, timedelta
from struct import pack, unpack
from base64 import b64decode
from xclib.utf8 import utf8, unutf8

        (secretID, expiry) = unpack('> H I', header)
        expiry = datetime.utcfromtimestamp(expiry)
        if expiry < self.now:
            logging.debug('Token has expired')
            return False

        challenge = pack('> B 6s %ds' % len(jid), version, header, utf8(jid))
        response = hmac.new(self.secret, challenge, hashlib.sha256).digest()
        if hmac.compare_digest(mac, response[:16]):
            return True
        else:
            logging.warning('Token for %s has invalid signature (possible attack attempt!)' % jid)
            return False

    def auth_cloud(self):
        response = self.cloud_request({
            'operation':'auth',
            'username': self.username,
            'domain':   self.authDomain,
            'password': self.password
        })
        if response and 'result' in response:
            return response['result'] # 'error', 'success', 'noauth'
        return False

    def checkpw(self, pwhash):
        '''Compare self.password with pwhash.
        
        Try to be resistant to timing attacks and use `checkpw` if available.'''

# Path: xclib/utf8.py
# def utf8(u, opts='strict'):
#     return u.encode('utf-8', opts)
# 
# def unutf8(u, opts='strict'):
#     if opts == 'illegal':
#         try:
#             return u.decode('utf-8', 'strict')
#         except UnicodeError:
#             dec = u.decode('utf-8', 'ignore')
#             logging.error('Illegal UTF-8 sequence: %r' % dec)
#             sys.stderr.write('Illegal UTF-8 sequence: %r\n' % dec)
#             traceback.print_exc()
#             return 'illegal-utf8-sequence-' + dec
#     else:
#         return u.decode('utf-8', opts)


# Path: xclib/auth.py
import logging
import hashlib
import hmac
import bcrypt
from datetime import datetime, timedelta
from struct import pack, unpack
from base64 import b64decode
from xclib.utf8 import utf8, unutf8


        (secretID, expiry) = unpack('> H I', header)
        expiry = datetime.utcfromtimestamp(expiry)
        if expiry < self.now:
            logging.debug('Token has expired')
            return False

        challenge = pack('> B 6s %ds' % len(jid), version, header, utf8(jid))
        response = hmac.new(self.secret, challenge, hashlib.sha256).digest()
        if hmac.compare_digest(mac, response[:16]):
            return True
        else:
            logging.warning('Token for %s has invalid signature (possible attack attempt!)' % jid)
            return False

    def auth_cloud(self):
        response = self.cloud_request({
            'operation':'auth',
            'username': self.username,
            'domain':   self.authDomain,
            'password': self.password
        })
        if response and 'result' in response:
            return response['result'] # 'error', 'success', 'noauth'
        return False

    def checkpw(self, pwhash):
        '''Compare self.password with pwhash.
        
        Try to be resistant to timing attacks and use `checkpw` if available.'''

        pw = utf8(self.password)