# 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
usersafe_encoding = str.maketrans('-$%', 'OIl')

# Is merged into sigcloud
class auth:
    # First try if it is a valid token
    # Failure may just indicate that we were passed a password
    def auth_token(self):
        try:
            token = b64decode(self.password.translate(usersafe_encoding) + '=======')
        except:
            logging.debug('Not a token (not base64)')
            return False

        jid = self.username + '@' + self.domain

        if len(token) != 23:
            logging.debug('Not a token (len: %d != 23)' % len(token))
            return False

        (version, mac, header) = unpack('> B 16s 6s', token)
        if version != 0:
            logging.debug('Not a token (version: %d != 0)' % version)
            return False;

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


# 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

usersafe_encoding = str.maketrans('-$%', 'OIl')

# Is merged into sigcloud
class auth:
    # First try if it is a valid token
    # Failure may just indicate that we were passed a password
    def auth_token(self):
        try:
            token = b64decode(self.password.translate(usersafe_encoding) + '=======')
        except:
            logging.debug('Not a token (not base64)')
            return False

        jid = self.username + '@' + self.domain

        if len(token) != 23:
            logging.debug('Not a token (len: %d != 23)' % len(token))
            return False

        (version, mac, header) = unpack('> B 16s 6s', token)
        if version != 0:
            logging.debug('Not a token (version: %d != 0)' % version)
            return False;

        (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))