#NEWLINE# def authenticate( self , site , site_url , assertion ) : #NEWLINE# #INDENT# 'Verifies BrowserID assertion\n\n        Returns:\n             Object that represents a user with an email verified by\n             the assertion. If a user with such email does not exists,\n             but there are open locations that require login, the user\n             object is created. In other cases, None is returned.\n\n        Raises:\n            AuthenticationError: verification failed.\n        ' #NEWLINE# try : #NEWLINE# #INDENT# result = self . verifier . verify ( assertion = assertion , audience = site_url ) #NEWLINE# #UNINDENT# except BrowserIDException as ex : #NEWLINE# #INDENT# return AuthenticationError ( 'Failed to contact Persona verification service' ) #NEWLINE# #UNINDENT# if ( not result ) : #NEWLINE# #INDENT# raise AuthenticationError ( 'BrowserID assertion verification failed.' ) #NEWLINE# #UNINDENT# user = site . users . find_item_by_email ( result . email ) #NEWLINE# if ( user is not None ) : #NEWLINE# #INDENT# return user #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# if site . locations . has_open_location_with_login ( ) : #NEWLINE# #INDENT# return site . users . create_item ( result . email ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return None #NEWLINE# #UNINDENT# #UNINDENT# except ValidationError as ex : #NEWLINE# #INDENT# raise AuthenticationError ( ', ' . join ( result . messages ) ) #NEWLINE# #UNINDENT# except LimitExceeded as ex : #NEWLINE# #INDENT# raise AuthenticationError ( str ( ex ) )