# Path: UserManager.py
# class UserManager:
#     users = {}
#     @staticmethod
#     def HandleLogin(svc, username, passwd):
#         user = UserManager.LoadUser(username)
#         if (user == None):
#             raise Unauthorized('Login failed')
# 
#         if (user.Authorize(passwd)):
#             session = Session(user, svc.client_address[0])
#             ret = {}
#             ret['session'] = session.GetID()
# 
#             svc.writedata(json.dumps(ret))
# 
#         else:
#             raise Unauthorized('Login failed')
# 
#     @staticmethod
#     def LoadUser(user):
#         userec = UCache.GetUser(user)
#         if (userec == None):
#             return None
#         user = userec.userid
#         if (user not in UserManager.users):
#             ruser = UserManager.LoadNewUser(user)
#             if (ruser == None):
#                 return None
#             UserManager.users[user] = ruser
#         return UserManager.users[user]
# 
#     @staticmethod
#     def LoadUserByUid(uid):
#         userec = UCache.GetUserByUid(uid)
#         if userec is None:
#             return None
#         user = userec.userid
#         return UserManager.LoadUser(user)
# 
#     @staticmethod
#     def LoadNewUser(user):
#         userec = UCache.GetUser(user)
#         if (userec == None):
#             return None
# 
#         umemo = UserMemo.UserMemoMgr.LoadUsermemo(user)
#         if (umemo is None):
#             return None
# 
#         ruser = User.User(user, userec, umemo)
#         return ruser
# 
# Path: Log.py
# class Log:
#     HEADER = '\033[95m'
#     OKBLUE = '\033[94m'
#     OKGREEN = '\033[92m'
#     WARNING = '\033[93m'
#     FAIL = '\033[91m'
#     ENDC = '\033[0m'
#     DEBUG = '\033[90m'
# 
#     _debug = True
#     _info = True
#     _warning = True
#     _error = True
# 
#     @staticmethod
#     def debug(msg):
#         if (Log._debug):
#             print Log.DEBUG, "%s [DEBUG]" % time.ctime(), msg.encode('utf-8'), Log.ENDC
#             sys.stdout.flush()
#     
#     @staticmethod
#     def error(msg):
#         if (Log._error):
#             print Log.FAIL, "%s [ERROR]" % time.ctime(), msg.encode('utf-8'), Log.ENDC
#             exc_info = sys.exc_info()
#             if exc_info[0] is not None:
#                 # handling exception
#                 traceback.print_exc()
#             sys.stdout.flush()
# 
#     @staticmethod
#     def warn(msg):
#         if (Log._warning):
#             print Log.WARNING, "%s [WARN] " % time.ctime(), msg.encode('utf-8'), Log.ENDC
#             sys.stdout.flush()
# 
#     @staticmethod
#     def info(msg):
#         if (Log._info):
#             print Log.OKGREEN, "%s [INFO] " % time.ctime(), msg.encode('utf-8'), Log.ENDC
#             sys.stdout.flush()


from UserManager import UserManager
from Log import Log
import Session
import sasl
    def service_name(self):
        return self._service_name
    
    def username(self):
        return self._username

    def password(self):
        raise NotImplementedError

    def get_password(self):
        raise NotImplementedError

    def verify_token(self, token):
        """Verify token"""

        try:
            result = Session.SessionManager.CheckSession(token)
            if result is not None:
                self._username = result
            else:
                Log.warn("XMPPAuth: fail to verify session")
            return result is not None
        except Exception as e:
            Log.warn("XMPPAuth: exception in CheckSession: %r" % e)
            return False

    def verify_password(self, authorize, username, passwd):
        """Verify password"""

        if (authorize and username != authorize):

# Path: UserManager.py
# class UserManager:
#     users = {}
#     @staticmethod
#     def HandleLogin(svc, username, passwd):
#         user = UserManager.LoadUser(username)
#         if (user == None):
#             raise Unauthorized('Login failed')
# 
#         if (user.Authorize(passwd)):
#             session = Session(user, svc.client_address[0])
#             ret = {}
#             ret['session'] = session.GetID()
# 
#             svc.writedata(json.dumps(ret))
# 
#         else:
#             raise Unauthorized('Login failed')
# 
#     @staticmethod
#     def LoadUser(user):
#         userec = UCache.GetUser(user)
#         if (userec == None):
#             return None
#         user = userec.userid
#         if (user not in UserManager.users):
#             ruser = UserManager.LoadNewUser(user)
#             if (ruser == None):
#                 return None
#             UserManager.users[user] = ruser
#         return UserManager.users[user]
# 
#     @staticmethod
#     def LoadUserByUid(uid):
#         userec = UCache.GetUserByUid(uid)
#         if userec is None:
#             return None
#         user = userec.userid
#         return UserManager.LoadUser(user)
# 
#     @staticmethod
#     def LoadNewUser(user):
#         userec = UCache.GetUser(user)
#         if (userec == None):
#             return None
# 
#         umemo = UserMemo.UserMemoMgr.LoadUsermemo(user)
#         if (umemo is None):
#             return None
# 
#         ruser = User.User(user, userec, umemo)
#         return ruser
# 
# Path: Log.py
# class Log:
#     HEADER = '\033[95m'
#     OKBLUE = '\033[94m'
#     OKGREEN = '\033[92m'
#     WARNING = '\033[93m'
#     FAIL = '\033[91m'
#     ENDC = '\033[0m'
#     DEBUG = '\033[90m'
# 
#     _debug = True
#     _info = True
#     _warning = True
#     _error = True
# 
#     @staticmethod
#     def debug(msg):
#         if (Log._debug):
#             print Log.DEBUG, "%s [DEBUG]" % time.ctime(), msg.encode('utf-8'), Log.ENDC
#             sys.stdout.flush()
#     
#     @staticmethod
#     def error(msg):
#         if (Log._error):
#             print Log.FAIL, "%s [ERROR]" % time.ctime(), msg.encode('utf-8'), Log.ENDC
#             exc_info = sys.exc_info()
#             if exc_info[0] is not None:
#                 # handling exception
#                 traceback.print_exc()
#             sys.stdout.flush()
# 
#     @staticmethod
#     def warn(msg):
#         if (Log._warning):
#             print Log.WARNING, "%s [WARN] " % time.ctime(), msg.encode('utf-8'), Log.ENDC
#             sys.stdout.flush()
# 
#     @staticmethod
#     def info(msg):
#         if (Log._info):
#             print Log.OKGREEN, "%s [INFO] " % time.ctime(), msg.encode('utf-8'), Log.ENDC
#             sys.stdout.flush()


# Path: xmppauth.py
from UserManager import UserManager
from Log import Log
import Session
import sasl

    def service_name(self):
        return self._service_name
    
    def username(self):
        return self._username

    def password(self):
        raise NotImplementedError

    def get_password(self):
        raise NotImplementedError

    def verify_token(self, token):
        """Verify token"""

        try:
            result = Session.SessionManager.CheckSession(token)
            if result is not None:
                self._username = result
            else:
                Log.warn("XMPPAuth: fail to verify session")
            return result is not None
        except Exception as e:
            Log.warn("XMPPAuth: exception in CheckSession: %r" % e)
            return False

    def verify_password(self, authorize, username, passwd):
        """Verify password"""

        if (authorize and username != authorize):

            Log.warn("XMPPAuth: user %s does not match authorize %s" % (username, authorize))