<s> from django import forms <EOL> from django . contrib . auth import authenticate <EOL> from django . utils . encoding import smart_unicode <EOL> from django . utils . translation import ugettext as _ <EOL> from . . import scope <EOL> from . . constants import RESPONSE_TYPE_CHOICES , SCOPES <EOL> from . . forms import OAuthForm , OAuthValidationError <EOL> from . . scope import SCOPE_NAMES <EOL> from . . utils import now <EOL> from . models import Client , Grant , RefreshToken <EOL> class ClientForm ( forms . ModelForm ) : <EOL> """<STR_LIT>""" <EOL> class Meta : <EOL> model = Client <EOL> fields = ( '<STR_LIT:name>' , '<STR_LIT:url>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def save ( self , user = None , ** kwargs ) : <EOL> self . instance . user = user <EOL> return super ( ClientForm , self ) . save ( ** kwargs ) <EOL> class ClientAuthForm ( forms . Form ) : <EOL> """<STR_LIT>""" <EOL> client_id = forms . CharField ( ) <EOL> client_secret = forms . CharField ( ) <EOL> def clean ( self ) : <EOL> data = self . cleaned_data <EOL> try : <EOL> client = Client . objects . get ( client_id = data . get ( '<STR_LIT>' ) , <EOL> client_secret = data . get ( '<STR_LIT>' ) ) <EOL> except Client . DoesNotExist : <EOL> raise forms . ValidationError ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> data [ '<STR_LIT>' ] = client <EOL> return data <EOL> class ScopeChoiceField ( forms . ChoiceField ) : <EOL> """<STR_LIT>""" <EOL> widget = forms . SelectMultiple <EOL> def to_python ( self , value ) : <EOL> if not value : <EOL> return [ ] <EOL> if not isinstance ( value , ( list , tuple ) ) : <EOL> value = value . split ( '<STR_LIT:U+0020>' ) <EOL> return u'<STR_LIT:U+0020>' . join ( [ smart_unicode ( val ) for val in value ] ) . split ( u'<STR_LIT:U+0020>' ) <EOL> def validate ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if self . required and not value : <EOL> raise OAuthValidationError ( { '<STR_LIT:error>' : '<STR_LIT>' } ) <EOL> for val in value : <EOL> if not self . valid_value ( val ) : <EOL> raise OAuthValidationError ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : _ ( "<STR_LIT>" ) % val } ) <EOL> class ScopeMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> def clean_scope ( self ) : <EOL> """<STR_LIT>""" <EOL> default = SCOPES [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> flags = self . cleaned_data . get ( '<STR_LIT>' , [ ] ) <EOL> return scope . to_int ( default = default , * flags ) <EOL> class AuthorizationRequestForm ( ScopeMixin , OAuthForm ) : <EOL> """<STR_LIT>""" <EOL> response_type = forms . CharField ( required = False ) <EOL> """<STR_LIT>""" <EOL> redirect_uri = forms . URLField ( required = False ) <EOL> """<STR_LIT>""" <EOL> state = forms . CharField ( required = False ) <EOL> """<STR_LIT>""" <EOL> scope = ScopeChoiceField ( choices = SCOPE_NAMES , required = False ) <EOL> """<STR_LIT>""" <EOL> def clean_response_type ( self ) : <EOL> """<STR_LIT>""" <EOL> response_type = self . cleaned_data . get ( '<STR_LIT>' ) <EOL> if not response_type : <EOL> raise OAuthValidationError ( { '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : "<STR_LIT>" } ) <EOL> types = response_type . split ( "<STR_LIT:U+0020>" ) <EOL> for type in types : <EOL> if type not in RESPONSE_TYPE_CHOICES : <EOL> raise OAuthValidationError ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : u"<STR_LIT>" <EOL> "<STR_LIT>" % type } ) <EOL> return response_type <EOL> def clean_redirect_uri ( self ) : <EOL> """<STR_LIT>""" <EOL> redirect_uri = self . cleaned_data . get ( '<STR_LIT>' ) <EOL> if redirect_uri : <EOL> if not redirect_uri == self . client . redirect_uri : <EOL> raise OAuthValidationError ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) } ) <EOL> return redirect_uri <EOL> class AuthorizationForm ( ScopeMixin , OAuthForm ) : <EOL> """<STR_LIT>""" <EOL> authorize = forms . BooleanField ( required = False ) <EOL> scope = ScopeChoiceField ( choices = SCOPE_NAMES , required = False ) <EOL> def save ( self , ** kwargs ) : <EOL> authorize = self . cleaned_data . get ( '<STR_LIT>' ) <EOL> if not authorize : <EOL> return None <EOL> grant = Grant ( ) <EOL> grant . scope = self . cleaned_data . get ( '<STR_LIT>' ) <EOL> return grant <EOL> class RefreshTokenGrantForm ( ScopeMixin , OAuthForm ) : <EOL> """<STR_LIT>""" <EOL> refresh_token = forms . CharField ( required = False ) <EOL> scope = ScopeChoiceField ( choices = SCOPE_NAMES , required = False ) <EOL> def clean_refresh_token ( self ) : <EOL> token = self . cleaned_data . get ( '<STR_LIT>' ) <EOL> if not token : <EOL> raise OAuthValidationError ( { '<STR_LIT:error>' : '<STR_LIT>' } ) <EOL> try : <EOL> token = RefreshToken . objects . get ( token = token , <EOL> expired = False , client = self . client ) <EOL> except RefreshToken . DoesNotExist : <EOL> raise OAuthValidationError ( { '<STR_LIT:error>' : '<STR_LIT>' } ) <EOL> return token <EOL> def clean ( self ) : <EOL> """<STR_LIT>""" <EOL> data = self . cleaned_data <EOL> want_scope = data . get ( '<STR_LIT>' ) or <NUM_LIT:0> <EOL> refresh_token = data . get ( '<STR_LIT>' ) <EOL> access_token = getattr ( refresh_token , '<STR_LIT>' , None ) if refresh_token else None <EOL> has_scope = access_token . scope if access_token else <NUM_LIT:0> <EOL> if want_scope is not <NUM_LIT:0> and not scope . check ( want_scope , has_scope ) : <EOL> raise OAuthValidationError ( { '<STR_LIT:error>' : '<STR_LIT>' } ) <EOL> return data <EOL> class AuthorizationCodeGrantForm ( ScopeMixin , OAuthForm ) : <EOL> """<STR_LIT>""" <EOL> code = forms . CharField ( required = False ) <EOL> scope = ScopeChoiceField ( choices = SCOPE_NAMES , required = False ) <EOL> def clean_code ( self ) : <EOL> code = self . cleaned_data . get ( '<STR_LIT:code>' ) <EOL> if not code : <EOL> raise OAuthValidationError ( { '<STR_LIT:error>' : '<STR_LIT>' } ) <EOL> try : <EOL> self . cleaned_data [ '<STR_LIT>' ] = Grant . objects . get ( <EOL> code = code , client = self . client , expires__gt = now ( ) ) <EOL> except Grant . DoesNotExist : <EOL> raise OAuthValidationError ( { '<STR_LIT:error>' : '<STR_LIT>' } ) <EOL> return code <EOL> def clean ( self ) : <EOL> """<STR_LIT>""" <EOL> data = self . cleaned_data <EOL> want_scope = data . get ( '<STR_LIT>' ) or <NUM_LIT:0> <EOL> grant = data . get ( '<STR_LIT>' ) <EOL> has_scope = grant . scope if grant else <NUM_LIT:0> <EOL> if want_scope is not <NUM_LIT:0> and not scope . check ( want_scope , has_scope ) : <EOL> raise OAuthValidationError ( { '<STR_LIT:error>' : '<STR_LIT>' } ) <EOL> return data <EOL> class PasswordGrantForm ( ScopeMixin , OAuthForm ) : <EOL> """<STR_LIT>""" <EOL> username = forms . CharField ( required = False ) <EOL> password = forms . CharField ( required = False ) <EOL> scope = ScopeChoiceField ( choices = SCOPE_NAMES , required = False ) <EOL> def clean_username ( self ) : <EOL> username = self . cleaned_data . get ( '<STR_LIT:username>' ) <EOL> if not username : <EOL> raise OAuthValidationError ( { '<STR_LIT:error>' : '<STR_LIT>' } ) <EOL> return username <EOL> def clean_password ( self ) : <EOL> password = self . cleaned_data . get ( '<STR_LIT:password>' ) <EOL> if not password : <EOL> raise OAuthValidationError ( { '<STR_LIT:error>' : '<STR_LIT>' } ) <EOL> return password <EOL> def clean ( self ) : <EOL> data = self . cleaned_data <EOL> user = authenticate ( username = data . get ( '<STR_LIT:username>' ) , <EOL> password = data . get ( '<STR_LIT:password>' ) ) <EOL> if user is None : <EOL> raise OAuthValidationError ( { '<STR_LIT:error>' : '<STR_LIT>' } ) <EOL> data [ '<STR_LIT:user>' ] = user <EOL> return data <EOL> class PublicPasswordGrantForm ( PasswordGrantForm ) : <EOL> client_id = forms . CharField ( required = True ) <EOL> grant_type = forms . CharField ( required = True ) <EOL> def clean_grant_type ( self ) : <EOL> grant_type = self . cleaned_data . get ( '<STR_LIT>' ) <EOL> if grant_type != '<STR_LIT:password>' : <EOL> raise OAuthValidationError ( { '<STR_LIT:error>' : '<STR_LIT>' } ) <EOL> return grant_type <EOL> def clean ( self ) : <EOL> data = super ( PublicPasswordGrantForm , self ) . clean ( ) <EOL> try : <EOL> client = Client . objects . get ( client_id = data . get ( '<STR_LIT>' ) ) <EOL> except Client . DoesNotExist : <EOL> raise OAuthValidationError ( { '<STR_LIT:error>' : '<STR_LIT>' } ) <EOL> if client . client_type != <NUM_LIT:1> : <EOL> raise OAuthValidationError ( { '<STR_LIT:error>' : '<STR_LIT>' } ) <EOL> data [ '<STR_LIT>' ] = client <EOL> return data </s>
