<s> """<STR_LIT>""" <EOL> import hmac <EOL> try : <EOL> from hashlib import sha1 as sha <EOL> except ImportError : <EOL> import sha <EOL> import base64 <EOL> import re <EOL> class NotificationMessage ( object ) : <EOL> NOTIFICATION_WSDL = "<STR_LIT>" <EOL> NOTIFICATION_VERSION = '<STR_LIT>' <EOL> SERVICE_NAME = "<STR_LIT>" <EOL> OPERATION_NAME = "<STR_LIT>" <EOL> EVENT_PATTERN = r"<STR_LIT>" <EOL> EVENT_RE = re . compile ( EVENT_PATTERN ) <EOL> def __init__ ( self , d ) : <EOL> """<STR_LIT>""" <EOL> self . signature = d [ '<STR_LIT>' ] <EOL> self . timestamp = d [ '<STR_LIT>' ] <EOL> self . version = d [ '<STR_LIT>' ] <EOL> assert d [ '<STR_LIT>' ] == NotificationMessage . OPERATION_NAME , "<STR_LIT>" % NotificationMessage . OPERATION_NAME <EOL> self . events = [ ] <EOL> events_dict = { } <EOL> if '<STR_LIT>' in d : <EOL> events_dict = d [ '<STR_LIT>' ] <EOL> else : <EOL> for k in d : <EOL> v = d [ k ] <EOL> if k . startswith ( '<STR_LIT>' ) : <EOL> ed = NotificationMessage . EVENT_RE . search ( k ) . groupdict ( ) <EOL> n = int ( ed [ '<STR_LIT:n>' ] ) <EOL> param = str ( ed [ '<STR_LIT>' ] ) <EOL> if n not in events_dict : <EOL> events_dict [ n ] = { } <EOL> events_dict [ n ] [ param ] = v <EOL> for n in events_dict : <EOL> self . events . append ( Event ( events_dict [ n ] ) ) <EOL> def verify ( self , secret_key ) : <EOL> """<STR_LIT>""" <EOL> verification_input = NotificationMessage . SERVICE_NAME <EOL> verification_input += NotificationMessage . OPERATION_NAME <EOL> verification_input += self . timestamp <EOL> h = hmac . new ( key = secret_key , digestmod = sha ) <EOL> h . update ( verification_input ) <EOL> signature_calc = base64 . b64encode ( h . digest ( ) ) <EOL> return self . signature == signature_calc <EOL> class Event ( object ) : <EOL> def __init__ ( self , d ) : <EOL> self . event_type = d [ '<STR_LIT>' ] <EOL> self . event_time_str = d [ '<STR_LIT>' ] <EOL> self . hit_type = d [ '<STR_LIT>' ] <EOL> self . hit_id = d [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in d : <EOL> self . assignment_id = d [ '<STR_LIT>' ] <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . event_type , self . hit_id ) </s>
