<s> import hmac , base64 <EOL> from django import forms <EOL> from django . conf import settings <EOL> class Form ( forms . Form ) : <EOL> pass <EOL> class ModelForm ( forms . ModelForm ) : <EOL> """<STR_LIT>""" <EOL> def merge_from_initial ( self ) : <EOL> self . data . _mutable = True <EOL> filt = lambda v : v not in self . data . keys ( ) <EOL> for field in filter ( filt , getattr ( self . Meta , '<STR_LIT>' , ( ) ) ) : <EOL> self . data [ field ] = self . initial . get ( field , None ) <EOL> class OAuthAuthenticationForm ( forms . Form ) : <EOL> oauth_token = forms . CharField ( widget = forms . HiddenInput ) <EOL> oauth_callback = forms . CharField ( widget = forms . HiddenInput , required = False ) <EOL> authorize_access = forms . BooleanField ( required = True ) <EOL> csrf_signature = forms . CharField ( widget = forms . HiddenInput ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> forms . Form . __init__ ( self , * args , ** kwargs ) <EOL> self . fields [ '<STR_LIT>' ] . initial = self . initial_csrf_signature <EOL> def clean_csrf_signature ( self ) : <EOL> sig = self . cleaned_data [ '<STR_LIT>' ] <EOL> token = self . cleaned_data [ '<STR_LIT>' ] <EOL> sig1 = OAuthAuthenticationForm . get_csrf_signature ( settings . SECRET_KEY , token ) <EOL> if sig != sig1 : <EOL> raise forms . ValidationError ( "<STR_LIT>" ) <EOL> return sig <EOL> def initial_csrf_signature ( self ) : <EOL> token = self . initial [ '<STR_LIT>' ] <EOL> return OAuthAuthenticationForm . get_csrf_signature ( settings . SECRET_KEY , token ) <EOL> @ staticmethod <EOL> def get_csrf_signature ( key , token ) : <EOL> try : <EOL> import hashlib <EOL> hashed = hmac . new ( key , token , hashlib . sha1 ) <EOL> except : <EOL> import sha <EOL> hashed = hmac . new ( key , token , sha ) <EOL> return base64 . b64encode ( hashed . digest ( ) ) </s>
