#NEWLINE# def decode( jwt , key = '' , verify = True ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# ( signing_input , crypto_segment ) = jwt . rsplit ( '.' , 1 ) #NEWLINE# ( header_segment , payload_segment ) = signing_input . split ( '.' , 1 ) #NEWLINE# #UNINDENT# except ValueError : #NEWLINE# #INDENT# raise DecodeError ( 'Not enough segments' ) #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# header_raw = base64url_decode ( binary ( header_segment ) ) . decode ( 'utf-8' ) #NEWLINE# payload_raw = base64url_decode ( binary ( payload_segment ) ) . decode ( 'utf-8' ) #NEWLINE# header = json . loads ( header_raw ) #NEWLINE# payload = json . loads ( payload_raw ) #NEWLINE# signature = base64url_decode ( binary ( crypto_segment ) ) #NEWLINE# #UNINDENT# except ( ValueError , TypeError ) : #NEWLINE# #INDENT# raise DecodeError ( 'Invalid segment encoding' ) #NEWLINE# #UNINDENT# if verify : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# method = signing_methods [ header [ 'alg' ] ] #NEWLINE# if ( not ( signature == method ( binary ( crypto_segment ) , binary ( key ) ) ) ) : #NEWLINE# #INDENT# raise DecodeError ( 'Signature verification failed' ) #NEWLINE# #UNINDENT# #UNINDENT# except KeyError : #NEWLINE# #INDENT# raise DecodeError ( 'Algorithm not supported' ) #NEWLINE# #UNINDENT# #UNINDENT# return payload