direct-io.hg

changeset 12063:48c5a79c4fbf

[XEND] Xen API Auth Manager for handling sessions

Signed-off-by: Alastair Tse <atse@xensource.com>
author acnt2@huggins.lce.cl.cam.ac.uk
date Thu Oct 05 17:29:19 2006 +0100 (2006-10-05)
parents 5fe8e9ebcf5c
children c11e827d6ac6
files tools/python/xen/xend/XendAuthSessions.py
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/tools/python/xen/xend/XendAuthSessions.py	Thu Oct 05 17:29:19 2006 +0100
     1.3 @@ -0,0 +1,86 @@
     1.4 +#============================================================================
     1.5 +# This library is free software; you can redistribute it and/or
     1.6 +# modify it under the terms of version 2.1 of the GNU Lesser General Public
     1.7 +# License as published by the Free Software Foundation.
     1.8 +#
     1.9 +# This library is distributed in the hope that it will be useful,
    1.10 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.11 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.12 +# Lesser General Public License for more details.
    1.13 +#
    1.14 +# You should have received a copy of the GNU Lesser General Public
    1.15 +# License along with this library; if not, write to the Free Software
    1.16 +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.17 +#============================================================================
    1.18 +# Copyright (C) 2006 XenSource Ltd.
    1.19 +#============================================================================
    1.20 +
    1.21 +import time
    1.22 +
    1.23 +from xen.xend import uuid
    1.24 +from xen.xend.XendError import *
    1.25 +from xen.xend.XendLogging import log
    1.26 +
    1.27 +class XendAuthSessions:
    1.28 +    """Keeps track of Xen API Login Sessions. (Example only)"""
    1.29 +
    1.30 +    def __init__(self):
    1.31 +        self.sessions = {}
    1.32 +        self.users = {'atse': 'passwd'}
    1.33 +
    1.34 +    def init(self):
    1.35 +        pass
    1.36 +
    1.37 +    def login_with_password(self, username, password):
    1.38 +        if self.is_authorized(username, password):
    1.39 +            new_session = uuid.createString()
    1.40 +            self.sessions[new_session] = (username, time.time())
    1.41 +            return new_session
    1.42 +
    1.43 +        raise XendError("Login failed")
    1.44 +
    1.45 +    def logout(self, session):
    1.46 +        if self.is_session_valid(session):
    1.47 +            del self.sessions[session]
    1.48 +
    1.49 +    def is_session_valid(self, session):
    1.50 +        log.debug('isSessionValid: %s' % session)
    1.51 +        if type(session) == type(str()):
    1.52 +            return (session in self.sessions)
    1.53 +        return False
    1.54 +    
    1.55 +    def is_authorized(self, username, password):
    1.56 +        if username in self.users and self.users[username] == password:
    1.57 +            return True
    1.58 +        return False
    1.59 +
    1.60 +    def get_user(self, session):
    1.61 +        try:
    1.62 +            return self.sessions[session][0]
    1.63 +        except (KeyError, IndexError):
    1.64 +            return None
    1.65 +
    1.66 +
    1.67 +def instance():
    1.68 +    """Singleton constructor. Use this instead of the class constructor.
    1.69 +    """
    1.70 +    global inst
    1.71 +    try:
    1.72 +        inst
    1.73 +    except:
    1.74 +        inst = XendAuthSessions()
    1.75 +        inst.init()
    1.76 +    return inst
    1.77 +
    1.78 +# Handy Authentication Decorators
    1.79 +# -------------------------------
    1.80 +def session_required(func):
    1.81 +    def check_session(self, session, *args, **kwargs):
    1.82 +        if instance().is_session_valid(session):
    1.83 +            return func(self, session, *args, **kwargs)
    1.84 +        else:
    1.85 +            return {'Status': 'Failure',
    1.86 +                    'ErrorDescription': XEND_ERROR_SESSION_INVALID}
    1.87 +    return check_session
    1.88 +
    1.89 +