<s> import base64 <EOL> import hashlib <EOL> import hmac <EOL> import json <EOL> def base64_url_decode ( inp ) : <EOL> padding_factor = ( <NUM_LIT:4> - len ( inp ) % <NUM_LIT:4> ) % <NUM_LIT:4> <EOL> inp += "<STR_LIT:=>" * padding_factor <EOL> return base64 . b64decode ( unicode ( inp ) . translate ( dict ( zip ( map ( ord , u'<STR_LIT>' ) , u'<STR_LIT>' ) ) ) ) <EOL> def parse_signed_request ( signed_request , secret ) : <EOL> l = signed_request . split ( '<STR_LIT:.>' , <NUM_LIT:2> ) <EOL> encoded_sig = l [ <NUM_LIT:0> ] <EOL> payload = l [ <NUM_LIT:1> ] <EOL> sig = base64_url_decode ( encoded_sig ) <EOL> data = json . loads ( base64_url_decode ( payload ) ) <EOL> if data . get ( '<STR_LIT>' ) . upper ( ) != '<STR_LIT>' : <EOL> return None <EOL> else : <EOL> expected_sig = hmac . new ( secret , msg = payload , digestmod = hashlib . sha256 ) . digest ( ) <EOL> if sig != expected_sig : <EOL> return None <EOL> else : <EOL> return data </s>
