<s> from datetime import datetime <EOL> from datetime import timedelta <EOL> from django . db import models <EOL> from django . utils . timezone import now <EOL> from djoauth2 . conf import settings <EOL> from djoauth2 . helpers import make_authorization_code <EOL> from djoauth2 . helpers import make_bearer_token <EOL> from djoauth2 . helpers import make_client_key <EOL> from djoauth2 . helpers import make_client_secret <EOL> UserModel = getattr ( settings , '<STR_LIT>' , '<STR_LIT>' ) <EOL> class Client ( models . Model ) : <EOL> user = models . ForeignKey ( UserModel ) <EOL> name = models . CharField ( max_length = <NUM_LIT> ) <EOL> description = models . TextField ( null = True , blank = True ) <EOL> image_url = models . URLField ( null = True , blank = True ) <EOL> redirect_uri = models . URLField ( null = False , blank = False ) <EOL> key = models . CharField ( <EOL> db_index = True , <EOL> default = make_client_key ( settings . DJOAUTH2_CLIENT_KEY_LENGTH ) , <EOL> max_length = settings . DJOAUTH2_CLIENT_KEY_LENGTH , <EOL> unique = True , <EOL> ) <EOL> secret = models . CharField ( <EOL> db_index = True , <EOL> default = make_client_secret ( settings . DJOAUTH2_CLIENT_SECRET_LENGTH ) , <EOL> max_length = settings . DJOAUTH2_CLIENT_SECRET_LENGTH , <EOL> unique = True , <EOL> ) <EOL> def __unicode__ ( self ) : <EOL> return unicode ( self . name ) <EOL> def __str__ ( self ) : <EOL> return str ( self . name ) <EOL> class Scope ( models . Model ) : <EOL> name = models . CharField ( unique = True , max_length = <NUM_LIT> , db_index = True ) <EOL> description = models . TextField ( ) <EOL> def __unicode__ ( self ) : <EOL> return unicode ( self . name ) <EOL> def __str__ ( self ) : <EOL> return str ( self . name ) <EOL> class AuthorizationCode ( models . Model ) : <EOL> client = models . ForeignKey ( Client ) <EOL> user = models . ForeignKey ( UserModel ) <EOL> date_created = models . DateTimeField ( auto_now_add = True ) <EOL> lifetime = models . PositiveIntegerField ( <EOL> default = lambda : settings . DJOAUTH2_AUTHORIZATION_CODE_LIFETIME ) <EOL> invalidated = models . BooleanField ( default = False ) <EOL> redirect_uri = models . URLField ( null = True , blank = True ) <EOL> scopes = models . ManyToManyField ( Scope , related_name = '<STR_LIT>' ) <EOL> value = models . CharField ( <EOL> db_index = True , <EOL> default = make_authorization_code ( <EOL> settings . DJOAUTH2_AUTHORIZATION_CODE_LENGTH ) , <EOL> max_length = settings . DJOAUTH2_AUTHORIZATION_CODE_LENGTH , <EOL> unique = True , <EOL> ) <EOL> def get_scope_names_set ( self ) : <EOL> return { s . name for s in self . scopes . all ( ) } <EOL> def has_scope ( self , * scope_names ) : <EOL> return self . get_scope_names_set ( ) >= set ( scope_names ) <EOL> def invalidate ( self ) : <EOL> self . invalidated = True <EOL> self . save ( ) <EOL> return self . invalidated <EOL> def is_expired ( self ) : <EOL> return ( self . invalidated or <EOL> now ( ) >= ( self . date_created + timedelta ( seconds = self . lifetime ) ) ) <EOL> def __unicode__ ( self ) : <EOL> return unicode ( self . value ) <EOL> def __str__ ( self ) : <EOL> return str ( self . value ) <EOL> class AccessToken ( models . Model ) : <EOL> client = models . ForeignKey ( Client ) <EOL> date_created = models . DateTimeField ( auto_now_add = True ) <EOL> lifetime = models . PositiveIntegerField ( <EOL> default = lambda : settings . DJOAUTH2_ACCESS_TOKEN_LIFETIME ) <EOL> invalidated = models . BooleanField ( default = False ) <EOL> authorization_code = models . ForeignKey ( <EOL> AuthorizationCode , related_name = '<STR_LIT>' , blank = True , null = True ) <EOL> refreshable = models . BooleanField ( <EOL> default = lambda : settings . DJOAUTH2_ACCESS_TOKENS_REFRESHABLE ) <EOL> refresh_token = models . CharField ( <EOL> blank = True , <EOL> db_index = True , <EOL> default = make_bearer_token ( settings . DJOAUTH2_REFRESH_TOKEN_LENGTH ) , <EOL> max_length = settings . DJOAUTH2_REFRESH_TOKEN_LENGTH , <EOL> null = True , <EOL> unique = True , <EOL> ) <EOL> scopes = models . ManyToManyField ( Scope , related_name = '<STR_LIT>' ) <EOL> user = models . ForeignKey ( UserModel ) <EOL> value = models . CharField ( <EOL> db_index = True , <EOL> default = make_bearer_token ( settings . DJOAUTH2_ACCESS_TOKEN_LENGTH ) , <EOL> max_length = settings . DJOAUTH2_ACCESS_TOKEN_LENGTH , <EOL> unique = True , <EOL> ) <EOL> def get_scope_names_set ( self ) : <EOL> return { s . name for s in self . scopes . all ( ) } <EOL> def has_scope ( self , * scope_names ) : <EOL> return self . get_scope_names_set ( ) >= set ( scope_names ) <EOL> def invalidate ( self ) : <EOL> self . invalidated = True <EOL> self . save ( ) <EOL> return self . invalidated <EOL> def is_expired ( self ) : <EOL> return ( self . invalidated or <EOL> now ( ) >= ( self . date_created + timedelta ( seconds = self . lifetime ) ) ) <EOL> def __unicode__ ( self ) : <EOL> return unicode ( self . value ) <EOL> def __str__ ( self ) : <EOL> return str ( self . value ) </s>
