<s> from datetime import timedelta <EOL> from django . core . urlresolvers import reverse <EOL> from . . import constants <EOL> from . . views import Capture , Authorize , Redirect <EOL> from . . views import AccessToken as AccessTokenView , OAuthError <EOL> from . . utils import now <EOL> from . forms import AuthorizationRequestForm , AuthorizationForm <EOL> from . forms import PasswordGrantForm , RefreshTokenGrantForm <EOL> from . forms import AuthorizationCodeGrantForm <EOL> from . models import Client , RefreshToken , AccessToken <EOL> from . backends import BasicClientBackend , RequestParamsClientBackend , PublicPasswordBackend <EOL> class Capture ( Capture ) : <EOL> """<STR_LIT>""" <EOL> def get_redirect_url ( self , request ) : <EOL> return reverse ( '<STR_LIT>' ) <EOL> class Authorize ( Authorize ) : <EOL> """<STR_LIT>""" <EOL> def get_request_form ( self , client , data ) : <EOL> return AuthorizationRequestForm ( data , client = client ) <EOL> def get_authorization_form ( self , request , client , data , client_data ) : <EOL> return AuthorizationForm ( data ) <EOL> def get_client ( self , client_id ) : <EOL> try : <EOL> return Client . objects . get ( client_id = client_id ) <EOL> except Client . DoesNotExist : <EOL> return None <EOL> def get_redirect_url ( self , request ) : <EOL> return reverse ( '<STR_LIT>' ) <EOL> def save_authorization ( self , request , client , form , client_data ) : <EOL> grant = form . save ( commit = False ) <EOL> if grant is None : <EOL> return None <EOL> grant . user = request . user <EOL> grant . client = client <EOL> grant . redirect_uri = client_data . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> grant . save ( ) <EOL> return grant . code <EOL> class Redirect ( Redirect ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class AccessTokenView ( AccessTokenView ) : <EOL> """<STR_LIT>""" <EOL> authentication = ( <EOL> BasicClientBackend , <EOL> RequestParamsClientBackend , <EOL> PublicPasswordBackend , <EOL> ) <EOL> def get_authorization_code_grant ( self , request , data , client ) : <EOL> form = AuthorizationCodeGrantForm ( data , client = client ) <EOL> if not form . is_valid ( ) : <EOL> raise OAuthError ( form . errors ) <EOL> return form . cleaned_data . get ( '<STR_LIT>' ) <EOL> def get_refresh_token_grant ( self , request , data , client ) : <EOL> form = RefreshTokenGrantForm ( data , client = client ) <EOL> if not form . is_valid ( ) : <EOL> raise OAuthError ( form . errors ) <EOL> return form . cleaned_data . get ( '<STR_LIT>' ) <EOL> def get_password_grant ( self , request , data , client ) : <EOL> form = PasswordGrantForm ( data , client = client ) <EOL> if not form . is_valid ( ) : <EOL> raise OAuthError ( form . errors ) <EOL> return form . cleaned_data <EOL> def get_access_token ( self , request , user , scope , client ) : <EOL> try : <EOL> at = AccessToken . objects . get ( user = user , client = client , <EOL> scope = scope , expires__gt = now ( ) ) <EOL> except AccessToken . DoesNotExist : <EOL> at = self . create_access_token ( request , user , scope , client ) <EOL> self . create_refresh_token ( request , user , scope , at , client ) <EOL> return at <EOL> def create_access_token ( self , request , user , scope , client ) : <EOL> return AccessToken . objects . create ( <EOL> user = user , <EOL> client = client , <EOL> scope = scope <EOL> ) <EOL> def create_refresh_token ( self , request , user , scope , access_token , client ) : <EOL> return RefreshToken . objects . create ( <EOL> user = user , <EOL> access_token = access_token , <EOL> client = client <EOL> ) <EOL> def invalidate_grant ( self , grant ) : <EOL> if constants . DELETE_EXPIRED : <EOL> grant . delete ( ) <EOL> else : <EOL> grant . expires = now ( ) - timedelta ( days = <NUM_LIT:1> ) <EOL> grant . save ( ) <EOL> def invalidate_refresh_token ( self , rt ) : <EOL> if constants . DELETE_EXPIRED : <EOL> rt . delete ( ) <EOL> else : <EOL> rt . expired = True <EOL> rt . save ( ) <EOL> def invalidate_access_token ( self , at ) : <EOL> if constants . DELETE_EXPIRED : <EOL> at . delete ( ) <EOL> else : <EOL> at . expires = now ( ) - timedelta ( days = <NUM_LIT:1> ) <EOL> at . save ( ) </s>
