def getAuthenticatedSession( self , userEmailAddr , passwd=None, ticket_id=None ): """ @return: an already existing authenticated session. @param userEmailAddr: the user email @type userEmailAddr: a Mobyle.Net.EmailAddress instance @param passwd: the session pass word @type passwd: string @raise AuthenticationError: if the passwd doesn't match the session passwd @raise AuthenticationError: the session doesn't already exists """ mymd5 = md5() mymd5.update( str( userEmailAddr ) ) key = mymd5.hexdigest() try: session = self.__sessions[ key ] if session.checkPasswd( passwd ): return session else: raise AuthenticationError , "There is no user with this email and password" except KeyError: sessionDir = os.path.normpath( os.path.join( self.cfg.user_sessions_path() , AuthenticatedSession.DIRNAME , key ) ) if os.path.exists( sessionDir ): session = AuthenticatedSession( self.cfg , userEmailAddr , passwd=passwd, ticket_id=ticket_id ) self.__sessions[ session.getKey() ] = session return session else: raise AuthenticationError , "There is no user with this email"
def testGetSessionWithTicket(self): ## Create a new session self.cfg._authenticated_session = "email" # BUG? # pourquoi un ticket n'est cree que dans le cas self.cfg._authenticated_session = 'email' # et pas quand self.cfg._authenticated_session = 'yes' ???? session1 = AuthenticatedSession(self.cfg, self.email, passwd=self.passwd) ## Fetch an existing session session2 = AuthenticatedSession(self.cfg, self.email, ticket_id=session1.ticket_id) self.assertEqual(session1.getDir(), session2.getDir()) self.assertRaises(SessionError, AuthenticatedSession, self.cfg, self.email, ticket_id=session1.ticket_id + "1")
def testGetSessionWithPasswd(self): ## Create a new session session1 = AuthenticatedSession(self.cfg, self.email, passwd=self.passwd) ## Fetch an existing session session2 = AuthenticatedSession(self.cfg, self.email, passwd=self.passwd) self.assertEqual(session1.getDir(), session2.getDir()) self.assertRaises(AuthenticationError, AuthenticatedSession, self.cfg, self.email, passwd="bad_" + self.passwd) ## Creation should fail if disabled self.cfg._authenticated_session = "no" self.assertRaises(SessionError, AuthenticatedSession, self.cfg, self.email, passwd=self.passwd) self.cfg._authenticated_session = "yes" ##try to authenticated with an invalid email session1._AuthenticatedSession__userEmail.check = lambda x: False session2 = AuthenticatedSession(self.cfg, self.email, passwd=self.passwd)
def testCheckPasswd(self): session = AuthenticatedSession(self.cfg, self.email, passwd=self.passwd) self.assertFalse(session.checkPasswd(self.passwd + "false")) self.assertFalse(session.checkPasswd("")) self.assertTrue(session.checkPasswd(self.passwd)) newPasswd = "new_pass_word" session.setPasswd(newPasswd) self.assertFalse(session.checkPasswd(self.passwd)) self.assertTrue(session.checkPasswd(newPasswd))
def testMergeWith(self): ## Create an authenticated session auth_session = AuthenticatedSession(self.cfg, self.email, passwd=self.passwd) ## Merging a session with itself should fail self.assertRaises(SessionError, auth_session.mergeWith, auth_session) ## Create an anonymous session self.cfg._anonymous_session = "yes" key = "anonymous_01" self._makeFakeSession(key) anno_session = AnonymousSession(self.cfg, key=key) jobs = anno_session.getAllJobs() datas = anno_session.getAllData() ## Merge sessions auth_session.mergeWith(anno_session) newJobs = auth_session.getAllJobs() newDatas = auth_session.getAllData() self.assertEqual(jobs, newJobs) self.assertEqual(datas, newDatas)
def createAuthenticatedSession( self , userEmailAddr , passwd ): """ create an authenticated session with email as login and passwd as pass word @param userEmailAddr: the user email @type userEmailAddr: a Mobyle.Net.EmailAddress object @param passwd: the user password @type passwd: string @return: a new authenticated session @rtype: session instance @raise AuthenticationError: if there is already a session with this email, or the email is not allowed on this server """ 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" mymd5 = md5() mymd5.update( str( userEmailAddr ) ) key = mymd5.hexdigest() if self.__sessions.has_key( key ) : msg = "Try to create a new Session with email %s, the %s Session already exist" % ( userEmailAddr , key) self.log.error( msg ) raise AuthenticationError , "user with the email you specify already exist" else: sessionDir = os.path.normpath( os.path.join( self.cfg.user_sessions_path() , AuthenticatedSession.DIRNAME , key ) ) if os.path.exists( sessionDir ): msg = "Try to create a new Session with email %s, the %s Session already exist" % ( userEmailAddr , key) self.log.error( msg ) raise AuthenticationError , "user with the email you specify already exist" session = AuthenticatedSession( self.cfg , userEmailAddr , passwd ) self.__sessions[ session.getKey() ] = session return session
def testConfirmEmail(self): self.cfg._authenticated_session = "email" session = AuthenticatedSession(self.cfg, self.email, passwd=self.passwd) transaction = session._getTransaction(Transaction.READ) actKey = transaction.getActivatingKey() transaction.commit() self.assertFalse(session.isActivated()) self.assertRaises(AuthenticationError, session.confirmEmail, actKey + "False") session.confirmEmail(actKey) self.assertTrue(session.isActivated())
def testChangePasswd(self): session = AuthenticatedSession(self.cfg, self.email, passwd=self.passwd) newPasswd = "new_pass_word" self.assertRaises(AuthenticationError, session.changePasswd, self.passwd + "false", newPasswd) session.changePasswd(self.passwd, newPasswd) self.assertTrue(session.checkPasswd(newPasswd))