def __init__( self , cfg , userEmail , passwd=None, ticket_id=None):
     self.log = getLogger('Mobyle.Session.AuthenticatedSession')
     self.cfg = cfg
     assert not(passwd and ticket_id), "please provide either a ticket id or a password" # TODO: clean up the parameters check
     """the maximum size of a session ( in bytes )"""
     self.sessionLimit = self.cfg.sessionlimit()
     self.__userEmail = userEmail
     authenticatedSessionAllowed = self.cfg.authenticatedSession()        
     
     if authenticatedSessionAllowed == "no" :
         self.log.error("can't create  session AUTHENTICATED_SESSION is set to \"no\" in Local/Config/Config.py")          
         raise SessionError , "can't create  authenticated session: permission denied"
           
     key = self.__newSessionKey()
     sessionDir = os.path.normpath( os.path.join( self.cfg.user_sessions_path() , AuthenticatedSession.DIRNAME , key  ) )
     self.key = key 
     """ the user/session  key"""
     self.Dir = sessionDir
     """ the path to this session directory """
    
     if os.path.exists( sessionDir ): #the session already exist
         if passwd:
             if not self.checkPasswd( passwd ):
                 self.log.info( "authentified/%s : Authentication Failure "% ( self.getKey() ) )
                 raise AuthenticationError , "Authentication Failure"
             else:
                 self._getTicket()
         elif ticket_id and not self.checkTicket( ticket_id ):
             raise AuthenticationError , "Invalid ticket or expired ticket"
         else:
             self.__generateTicketId(ticket_id or None)
         
     else: #creation of new Session
         chk = self.__userEmail.check()
         if not chk:
             msg = " %s %s FORBIDDEN can't create authenticated session : %s" % ( self.__userEmail ,
                                 os.environ[ 'REMOTE_ADDR' ] ,
                                 self.__userEmail.getMessage()
                                 )
             self.log.error( msg )
             raise SessionError , "you are not allowed to register on this server for now"
         try:
             os.makedirs( sessionDir , 0755 ) #create parent directory 
         except Exception, err:
             self.log.critical( "unable to create authenticated session : %s : %s" % ( sessionDir , err) , exc_info = True)
             raise SessionError , "unable to create authenticated session"
         mymd5 = md5()
         mymd5.update( passwd )
         cryptPasswd = mymd5.hexdigest()
         
         authenticatedSessionAllowed = self.cfg.authenticatedSession()        
         if authenticatedSessionAllowed == "yes":  
             Transaction.create( os.path.join( sessionDir , self.FILENAME ) , 
                                 True , #authenticated
                                 True , #activated
                                 userEmail = str( self.__userEmail ) , 
                                 passwd = cryptPasswd )
             self.__generateTicketId()
         elif authenticatedSessionAllowed == "email" :
             activatingKey = self.__newActivatingKey()
             try:
                 from Mobyle.Net import Email
                 mail = Email( self.__userEmail )
                 mail.send( 'CONFIRM_SESSION' , { 'SENDER'         : self.cfg.sender() ,
                                                  'HELP'           : self.cfg.mailHelp() ,
                                                  'SERVER_NAME'    : self.cfg.portal_url() ,
                                                  'ACTIVATING_KEY' : activatingKey ,
                                                  'CGI_URL'        : self.cfg.cgi_url() } 
                            )
                 
                 Transaction.create( os.path.join( sessionDir , self.FILENAME ) , 
                                     True ,  #authenticated
                                     False , #activated
                                     activatingKey = activatingKey , 
                                     userEmail = self.__userEmail , 
                                     passwd = cryptPasswd )
                 self.__generateTicketId()
                 # api create( id , authenticated , activated , activatingKey = None , userEmail = None, passwd = None)
             except MobyleError , err :
                 msg = "can't send an activation email, session creation aborted"
                 self.log.error( "%s : %s " % ( msg , err ) )
                 os.rmdir( self.Dir )
                 raise SessionError , msg
Exemple #2
0
def emailResults(cfg, userEmail, registry, ID, job_path, serviceName, jobKey, FileName=None):
    """
    @param cfg: the configuration of Mobyle    
    @type cfg: Config instance
    @param userEmail: the user email address
    @type userEmail: EmailAddress instance
    @param registry: the registry of deployed services
    @type registry: Registry.registry object
    @param ID: the ID of the job
    @type ID: string
    @param job_path: the absolute path to the job 
    @type job_path: string
    @param serviceName: the name of the service
    @type serviceName: string
    @param jobKey: the key of the job
    @type jobKey: string
    @param FileName: the absolute path of zip file to attach to the email
    @type FileName: string or None
    """
    from Mobyle.Net import Email
    from Mobyle.MobyleError import EmailError, TooBigError
    import os

    dont_email_result, maxmailsize = cfg.mailResults()
    if dont_email_result:
        return
    else:
        if userEmail:
            mail = Email(userEmail)
            jobInPortalUrl = "%s/portal.py#jobs::%s" % (cfg.cgi_url(), registry.getJobPID(ID))

            if FileName is not None:
                zipSize = os.path.getsize(FileName)
                mailDict = {
                    "SENDER": cfg.sender(),
                    "HELP": cfg.mailHelp(),
                    "SERVER_NAME": cfg.portal_url(),
                    "JOB_URL": jobInPortalUrl,
                    "RESULTS_REMAIN": cfg.remainResults(),
                    "JOB_NAME": serviceName,
                    "JOB_KEY": jobKey,
                }
                if zipSize > maxmailsize - 2048:
                    # 2048 octet is an estimated size of email headers
                    try:
                        mail.send("RESULTS_TOOBIG", mailDict)
                        return
                    except EmailError, err:
                        msg = str(err)
                        adm = Admin(job_path)
                        adm.setMessage(msg)
                        adm.commit()
                        u_log.error("%s/%s : %s" % (serviceName, jobKey, msg))
                        return
                else:
                    try:
                        mail.send("RESULTS_FILES", mailDict, files=[FileName])
                        return
                    except TooBigError, err:
                        try:
                            mail.send("RESULTS_TOOBIG", mailDict)
                        except EmailError, err:
                            msg = str(err)
                            adm = Admin(job_path)
                            adm.setMessage(msg)
                            adm.commit()
                            u_log.error("%s/%s : %s" % (serviceName, jobKey, msg))

                        return