#NEWLINE# @ _cacheAuthUser #NEWLINE# def getCurrentUser( returnToken = False ) : #NEWLINE# #INDENT# '\n    Returns the currently authenticated user based on the token header or\n    parameter.\n\n    :param returnToken: Whether we should return a tuple that also contains the\n                        token.\n    :type returnToken: bool\n    :returns: the user document from the database, or None if the user is not\n              logged in or the token is invalid or expired.  If\n              returnToken=True, returns a tuple of (user, token).\n    ' #NEWLINE# event = events . trigger ( 'auth.user.get' ) #NEWLINE# if ( event . defaultPrevented and ( len ( event . responses ) > 0 ) ) : #NEWLINE# #INDENT# return event . responses [ 0 ] #NEWLINE# #UNINDENT# token = getCurrentToken ( ) #NEWLINE# def retVal( user , token ) : #NEWLINE# #INDENT# if returnToken : #NEWLINE# #INDENT# return ( user , token ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return user #NEWLINE# #UNINDENT# #UNINDENT# if ( ( token is None ) or ( token [ 'expires' ] < datetime . datetime . utcnow ( ) ) or ( 'userId' not in token ) ) : #NEWLINE# #INDENT# return retVal ( None , token ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# ensureTokenScopes ( token , getattr ( cherrypy . request , 'requiredScopes' , TokenScope . USER_AUTH ) ) #NEWLINE# #UNINDENT# except AccessException : #NEWLINE# #INDENT# return retVal ( None , token ) #NEWLINE# #UNINDENT# user = ModelImporter . model ( 'user' ) . load ( token [ 'userId' ] , force = True ) #NEWLINE# return retVal ( user , token )