#NEWLINE# @ defer . inlineCallbacks #NEWLINE# def get_user_from_macaroon( self , macaroon_str ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# macaroon = pymacaroons . Macaroon . deserialize ( macaroon_str ) #NEWLINE# self . validate_macaroon ( macaroon , 'access' , False ) #NEWLINE# user_prefix = 'user_id = ' #NEWLINE# user = None #NEWLINE# guest = False #NEWLINE# for caveat in macaroon . caveats : #NEWLINE# #INDENT# if caveat . caveat_id . startswith ( user_prefix ) : #NEWLINE# #INDENT# user = UserID . from_string ( caveat . caveat_id [ len ( user_prefix ) : ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( caveat . caveat_id == 'guest = true' ) : #NEWLINE# #INDENT# guest = True #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if ( user is None ) : #NEWLINE# #INDENT# raise AuthError ( self . TOKEN_NOT_FOUND_HTTP_STATUS , 'No user caveat in macaroon' , errcode = Codes . UNKNOWN_TOKEN ) #NEWLINE# #UNINDENT# if guest : #NEWLINE# #INDENT# ret = { #NEWLINE# #INDENT# 'user' : user , #NEWLINE# 'is_guest' : True , #NEWLINE# 'token_id' : None , #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# ret = ( yield self . _look_up_user_by_access_token ( macaroon_str ) ) #NEWLINE# if ( ret [ 'user' ] != user ) : #NEWLINE# #INDENT# logger . error ( 'Macaroon user (%s) != DB user (%s)' , user , ret [ 'user' ] ) #NEWLINE# raise AuthError ( self . TOKEN_NOT_FOUND_HTTP_STATUS , 'User mismatch in macaroon' , errcode = Codes . UNKNOWN_TOKEN ) #NEWLINE# #UNINDENT# #UNINDENT# defer . returnValue ( ret ) #NEWLINE# #UNINDENT# except ( pymacaroons . exceptions . MacaroonException , TypeError , ValueError ) : #NEWLINE# #INDENT# raise AuthError ( self . TOKEN_NOT_FOUND_HTTP_STATUS , 'Invalid macaroon passed.' , errcode = Codes . UNKNOWN_TOKEN )