<s> import arrow <EOL> import json <EOL> import requests <EOL> from base64 import standard_b64decode <EOL> from cryptography . hazmat . backends import default_backend <EOL> from cryptography . hazmat . primitives import hashes <EOL> from cryptography . hazmat . primitives . asymmetric . padding import PKCS1v15 <EOL> from . misfit import MisfitObject <EOL> def string_to_sign ( data ) : <EOL> """<STR_LIT>""" <EOL> strings = [ ] <EOL> for key in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if key in data : <EOL> strings += [ key , data [ key ] ] <EOL> strings . append ( "<STR_LIT>" ) <EOL> return '<STR_LIT:\n>' . join ( strings ) . encode ( '<STR_LIT:utf8>' ) <EOL> class MisfitMessage ( MisfitObject ) : <EOL> """<STR_LIT>""" <EOL> DELETED = '<STR_LIT>' <EOL> CREATED = '<STR_LIT>' <EOL> UPDATED = '<STR_LIT>' <EOL> class MisfitNotification ( MisfitObject ) : <EOL> def __init__ ( self , data ) : <EOL> """<STR_LIT>""" <EOL> data_dict = json . loads ( data . decode ( '<STR_LIT:utf8>' ) ) <EOL> super ( MisfitNotification , self ) . __init__ ( data_dict ) <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> self . verify_signature ( ) <EOL> if self . Type == '<STR_LIT>' : <EOL> self . Message = [ MisfitMessage ( m ) for m in json . loads ( self . Message ) ] <EOL> elif self . Type == '<STR_LIT>' : <EOL> requests . get ( self . SubscribeURL ) <EOL> def verify_signature ( self ) : <EOL> """<STR_LIT>""" <EOL> cert_url = self . data [ '<STR_LIT>' ] <EOL> cert_str = requests . get ( cert_url ) . content . encode ( '<STR_LIT:utf8>' ) <EOL> cert = default_backend ( ) . load_pem_x509_certificate ( cert_str ) <EOL> pubkey = cert . public_key ( ) <EOL> signature = standard_b64decode ( self . data [ '<STR_LIT>' ] . encode ( '<STR_LIT:utf8>' ) ) <EOL> verifier = pubkey . verifier ( signature , PKCS1v15 ( ) , hashes . SHA1 ( ) ) <EOL> verifier . update ( string_to_sign ( self . data ) ) <EOL> assert verifier . verify ( ) is None </s>
