<s> import base64 <EOL> import hashlib <EOL> import hmac <EOL> try : <EOL> import simplejson as json <EOL> except ImportError : <EOL> import json <EOL> import time <EOL> from datetime import datetime <EOL> from facepy . exceptions import * <EOL> class SignedRequest ( object ) : <EOL> """<STR_LIT>""" <EOL> user = None <EOL> """<STR_LIT>""" <EOL> data = None <EOL> """<STR_LIT>""" <EOL> page = None <EOL> """<STR_LIT>""" <EOL> raw = None <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , signed_request = None , application_secret_key = None , application_id = None , api_version = None ) : <EOL> """<STR_LIT>""" <EOL> self . signed_request = signed_request <EOL> self . application_secret_key = application_secret_key <EOL> self . application_id = application_id <EOL> self . api_version = api_version <EOL> self . raw = self . parse ( signed_request , application_secret_key ) <EOL> self . data = self . raw . get ( '<STR_LIT>' , None ) <EOL> self . page = self . Page ( <EOL> id = self . raw [ '<STR_LIT>' ] . get ( '<STR_LIT:id>' ) , <EOL> is_liked = self . raw [ '<STR_LIT>' ] . get ( '<STR_LIT>' ) , <EOL> is_admin = self . raw [ '<STR_LIT>' ] . get ( '<STR_LIT>' ) <EOL> ) if '<STR_LIT>' in self . raw else None <EOL> if '<STR_LIT:user>' not in self . raw : <EOL> self . fetch_user_data_and_token ( ) <EOL> self . user = self . User ( <EOL> id = self . raw . get ( '<STR_LIT>' ) , <EOL> locale = self . raw [ '<STR_LIT:user>' ] . get ( '<STR_LIT>' , None ) , <EOL> country = self . raw [ '<STR_LIT:user>' ] . get ( '<STR_LIT>' , None ) , <EOL> age = range ( <EOL> self . raw [ '<STR_LIT:user>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] , <EOL> self . raw [ '<STR_LIT:user>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] + <NUM_LIT:1> if '<STR_LIT>' in self . raw [ '<STR_LIT:user>' ] [ '<STR_LIT>' ] else <NUM_LIT:100> <EOL> ) if '<STR_LIT>' in self . raw [ '<STR_LIT:user>' ] else None , <EOL> oauth_token = self . User . OAuthToken ( <EOL> token = self . raw [ '<STR_LIT>' ] , <EOL> issued_at = datetime . fromtimestamp ( self . raw [ '<STR_LIT>' ] ) , <EOL> expires_at = datetime . fromtimestamp ( self . raw [ '<STR_LIT>' ] ) if self . raw [ '<STR_LIT>' ] > <NUM_LIT:0> else None <EOL> ) if '<STR_LIT>' in self . raw else None , <EOL> ) <EOL> def fetch_user_data_and_token ( self ) : <EOL> from urlparse import parse_qs <EOL> from . import GraphAPI , get_application_access_token <EOL> app_token = get_application_access_token ( self . application_id , self . application_secret_key , api_version = self . api_version ) <EOL> graph = GraphAPI ( app_token , version = self . api_version ) <EOL> qs = graph . get ( '<STR_LIT>' , code = self . raw [ '<STR_LIT:code>' ] , redirect_uri = '<STR_LIT>' , client_id = self . application_id , client_secret = self . application_secret_key ) <EOL> self . raw [ '<STR_LIT>' ] = parse_qs ( qs ) [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> self . raw [ '<STR_LIT>' ] = time . time ( ) + int ( parse_qs ( qs ) [ '<STR_LIT>' ] [ <NUM_LIT:0> ] ) <EOL> self . raw [ '<STR_LIT:user>' ] = graph . get ( self . raw [ '<STR_LIT>' ] ) <EOL> def parse ( cls , signed_request , application_secret_key ) : <EOL> """<STR_LIT>""" <EOL> def decode ( encoded ) : <EOL> padding = '<STR_LIT:=>' * ( len ( encoded ) % <NUM_LIT:4> ) <EOL> return base64 . urlsafe_b64decode ( encoded + padding ) <EOL> try : <EOL> encoded_signature , encoded_payload = ( str ( string ) for string in signed_request . split ( '<STR_LIT:.>' , <NUM_LIT:2> ) ) <EOL> signature = decode ( encoded_signature ) <EOL> signed_request_data = json . loads ( decode ( encoded_payload ) . decode ( '<STR_LIT:utf-8>' ) ) <EOL> except ( TypeError , ValueError ) : <EOL> raise SignedRequestError ( "<STR_LIT>" ) <EOL> if signed_request_data . get ( '<STR_LIT>' , '<STR_LIT>' ) . upper ( ) != '<STR_LIT>' : <EOL> raise SignedRequestError ( "<STR_LIT>" ) <EOL> expected_signature = hmac . new ( application_secret_key . encode ( '<STR_LIT:utf-8>' ) , msg = encoded_payload . encode ( '<STR_LIT:utf-8>' ) , <EOL> digestmod = hashlib . sha256 ) . digest ( ) <EOL> if signature != expected_signature : <EOL> raise SignedRequestError ( "<STR_LIT>" ) <EOL> return signed_request_data <EOL> parse = classmethod ( parse ) <EOL> def generate ( self ) : <EOL> """<STR_LIT>""" <EOL> payload = { <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> if self . data : <EOL> payload [ '<STR_LIT>' ] = self . data <EOL> if self . page : <EOL> payload [ '<STR_LIT>' ] = { } <EOL> if self . page . id : <EOL> payload [ '<STR_LIT>' ] [ '<STR_LIT:id>' ] = self . page . id <EOL> if self . page . is_liked : <EOL> payload [ '<STR_LIT>' ] [ '<STR_LIT>' ] = self . page . is_liked <EOL> if self . page . is_admin : <EOL> payload [ '<STR_LIT>' ] [ '<STR_LIT>' ] = self . page . is_admin <EOL> if self . user : <EOL> payload [ '<STR_LIT:user>' ] = { } <EOL> if self . user . country : <EOL> payload [ '<STR_LIT:user>' ] [ '<STR_LIT>' ] = self . user . country <EOL> if self . user . locale : <EOL> payload [ '<STR_LIT:user>' ] [ '<STR_LIT>' ] = self . user . locale <EOL> if self . user . age : <EOL> payload [ '<STR_LIT:user>' ] [ '<STR_LIT>' ] = { <EOL> '<STR_LIT>' : self . user . age [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' : self . user . age [ - <NUM_LIT:1> ] <EOL> } <EOL> if self . user . oauth_token : <EOL> if self . user . oauth_token . token : <EOL> payload [ '<STR_LIT>' ] = self . user . oauth_token . token <EOL> if self . user . oauth_token . expires_at is None : <EOL> payload [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> else : <EOL> payload [ '<STR_LIT>' ] = int ( time . mktime ( self . user . oauth_token . expires_at . timetuple ( ) ) ) <EOL> if self . user . oauth_token . issued_at : <EOL> payload [ '<STR_LIT>' ] = int ( time . mktime ( self . user . oauth_token . issued_at . timetuple ( ) ) ) <EOL> if self . user . id : <EOL> payload [ '<STR_LIT>' ] = self . user . id <EOL> encoded_payload = base64 . urlsafe_b64encode ( <EOL> json . dumps ( payload , separators = ( '<STR_LIT:U+002C>' , '<STR_LIT::>' ) ) . encode ( '<STR_LIT:utf-8>' ) <EOL> ) <EOL> encoded_signature = base64 . urlsafe_b64encode ( hmac . new ( <EOL> self . application_secret_key . encode ( '<STR_LIT:utf-8>' ) , <EOL> encoded_payload , <EOL> hashlib . sha256 <EOL> ) . digest ( ) ) <EOL> return '<STR_LIT>' % { <EOL> '<STR_LIT>' : encoded_signature , <EOL> '<STR_LIT>' : encoded_payload <EOL> } <EOL> class Page ( object ) : <EOL> """<STR_LIT>""" <EOL> id = None <EOL> """<STR_LIT>""" <EOL> is_liked = None <EOL> """<STR_LIT>""" <EOL> is_admin = None <EOL> """<STR_LIT>""" <EOL> url = None <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , id , is_liked = False , is_admin = False ) : <EOL> self . id , self . is_liked , self . is_admin = id , is_liked , is_admin <EOL> @ property <EOL> def url ( self ) : <EOL> return '<STR_LIT>' % self . id <EOL> class User ( object ) : <EOL> """<STR_LIT>""" <EOL> id = None <EOL> """<STR_LIT>""" <EOL> age = None <EOL> """<STR_LIT>""" <EOL> locale = None <EOL> """<STR_LIT>""" <EOL> country = None <EOL> """<STR_LIT>""" <EOL> oauth_token = None <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , id , age = None , locale = None , country = None , oauth_token = None ) : <EOL> self . id = id <EOL> self . locale = locale <EOL> self . country = country <EOL> self . age = age <EOL> self . oauth_token = oauth_token <EOL> @ property <EOL> def profile_url ( self ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' % self . id <EOL> @ property <EOL> def has_authorized_application ( self ) : <EOL> """<STR_LIT>""" <EOL> return bool ( self . oauth_token ) <EOL> class OAuthToken ( object ) : <EOL> """<STR_LIT>""" <EOL> token = None <EOL> """<STR_LIT>""" <EOL> issued_at = None <EOL> """<STR_LIT>""" <EOL> expires_at = None <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , token , issued_at , expires_at ) : <EOL> self . token , self . issued_at , self . expires_at = token , issued_at , expires_at <EOL> @ property <EOL> def has_expired ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . expires_at is None : <EOL> return False <EOL> else : <EOL> return self . expires_at < datetime . now ( ) <EOL> Error = SignedRequestError </s>
