def webLogin(request: HttpRequest, response: HttpResponse, user: User, password: str) -> bool: """ Helper function to, once the user is authenticated, store the information at the user session. @return: Always returns True """ from uds import REST if user.id != ROOT_ID: # If not ROOT user (this user is not inside any authenticator) manager_id = user.manager.id else: manager_id = -1 # If for any reason the "uds" cookie is removed, recreated it cookie = getUDSCookie(request, response) user.updateLastAccess() request.session.clear() request.session[USER_KEY] = user.id request.session[PASS_KEY] = cryptoManager().symCrypt( password, cookie) # Stores "bytes" # Ensures that this user will have access through REST api if logged in through web interface REST.Handler.storeSessionAuthdata(request.session, manager_id, user.name, password, get_language(), request.os, user.is_admin, user.staff_member, cookie) return True
def getRootUser(): # pylint: disable=unexpected-keyword-arg, no-value-for-parameter from uds.models import Authenticator u = User(id=ROOT_ID, name=GlobalConfig.SUPER_USER_LOGIN.get(True), real_name=_('System Administrator'), state=State.ACTIVE, staff_member=True, is_admin=True) u.manager = Authenticator() u.getGroups = lambda: [] u.updateLastAccess = lambda: None u.logout = lambda: None return u
def __init__(self, request: 'HttpRequest', path: str, operation: str, params: typing.Any, *args: str, **kwargs): logger.debug('Data: %s %s %s', self.__class__, self.needs_admin, self.authenticated) if ( self.needs_admin or self.needs_staff ) and not self.authenticated: # If needs_admin, must also be authenticated raise Exception( 'class {} is not authenticated but has needs_admin or needs_staff set!!' .format(self.__class__)) self._request = request self._path = path self._operation = operation self._params = params self._args = args self._kwargs = kwargs self._headers = {} self._authToken = None if self.authenticated: # Only retrieve auth related data on authenticated handlers try: self._authToken = self._request.META.get(AUTH_TOKEN_HEADER, '') self._session = SessionStore(session_key=self._authToken) if 'REST' not in self._session: raise Exception( ) # No valid session, so auth_token is also invalid except Exception: # Couldn't authenticate self._authToken = None self._session = None if self._authToken is None: raise AccessDenied() if self.needs_admin and not self.getValue('is_admin'): raise AccessDenied() if self.needs_staff and not self.getValue('staff_member'): raise AccessDenied() self._user = self.getUser() else: self._user = User() # Empty user for non authenticated handlers
def getRootUser() -> User: # pylint: disable=unexpected-keyword-arg, no-value-for-parameter user = User(id=ROOT_ID, name=GlobalConfig.SUPER_USER_LOGIN.get(True), real_name=_('System Administrator'), state=State.ACTIVE, staff_member=True, is_admin=True) user.manager = Authenticator() # Fake overwrite some methods, a bit cheating? maybe? :) user.getGroups = lambda: [] # type: ignore user.updateLastAccess = lambda: None # type: ignore user.logout = lambda: None # type: ignore return user
def getRootUser() -> User: """ Returns an user not in DB that is ROOT for the platform Returns: User: [description] """ user = User( id=ROOT_ID, name=GlobalConfig.SUPER_USER_LOGIN.get(True), real_name=_('System Administrator'), state=State.ACTIVE, staff_member=True, is_admin=True, ) user.manager = Authenticator() # type: ignore # Fake overwrite some methods, a bit cheating? maybe? :) user.getGroups = lambda: [] # type: ignore user.updateLastAccess = lambda: None # type: ignore user.logout = lambda: None # type: ignore return user