<s> from uuid import uuid4 <EOL> from random import randrange <EOL> from django . conf import settings <EOL> from django . db import models , IntegrityError <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from django . utils . encoding import python_2_unicode_compatible <EOL> from . fields import URLSchemeField <EOL> from . settings import REGISTRY_TOKEN_TYPE , TOKEN_TYPE_BEARER <EOL> USER_MODEL = getattr ( settings , '<STR_LIT>' , '<STR_LIT>' ) <EOL> @ python_2_unicode_compatible <EOL> class Scope ( models . Model ) : <EOL> STATUS_ENABLED = <NUM_LIT:1> <EOL> STATUS_DISABLED = <NUM_LIT:2> <EOL> STATUS_CHOICES = ( <EOL> ( STATUS_ENABLED , _ ( '<STR_LIT>' ) ) , <EOL> ( STATUS_DISABLED , _ ( '<STR_LIT>' ) ) , <EOL> ) <EOL> identifier = models . CharField ( <EOL> _ ( '<STR_LIT>' ) , max_length = <NUM_LIT:100> , help_text = _ ( '<STR_LIT>' ) , <EOL> unique = True ) <EOL> title = models . CharField ( _ ( '<STR_LIT>' ) , max_length = <NUM_LIT> , help_text = _ ( '<STR_LIT>' ) ) <EOL> status = models . PositiveIntegerField ( _ ( '<STR_LIT>' ) , db_index = True , choices = STATUS_CHOICES , default = STATUS_ENABLED ) <EOL> class Meta ( object ) : <EOL> verbose_name = _ ( '<STR_LIT>' ) <EOL> verbose_name_plural = _ ( '<STR_LIT>' ) <EOL> ordering = [ '<STR_LIT:title>' ] <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT:%s>' % self . identifier <EOL> @ python_2_unicode_compatible <EOL> class Client ( models . Model ) : <EOL> TYPE_CONFIDENTIAL = <NUM_LIT:1> <EOL> '''<STR_LIT>''' <EOL> TYPE_PUBLIC = <NUM_LIT:2> <EOL> '''<STR_LIT>''' <EOL> TYPE_CHOICES = ( <EOL> ( TYPE_CONFIDENTIAL , _ ( '<STR_LIT>' ) ) , <EOL> ( TYPE_PUBLIC , _ ( '<STR_LIT>' ) ) , <EOL> ) <EOL> date_registered = models . DateTimeField ( _ ( '<STR_LIT>' ) , auto_now_add = True ) <EOL> title = models . CharField ( _ ( '<STR_LIT>' ) , max_length = <NUM_LIT:100> , unique = True ) <EOL> user = models . ForeignKey ( USER_MODEL , verbose_name = _ ( '<STR_LIT>' ) , help_text = _ ( '<STR_LIT>' ) ) <EOL> description = models . TextField ( _ ( '<STR_LIT>' ) , max_length = <NUM_LIT:100> ) <EOL> link = models . URLField ( _ ( '<STR_LIT>' ) , help_text = _ ( '<STR_LIT>' ) , null = True , blank = True ) <EOL> identifier = models . CharField ( <EOL> _ ( '<STR_LIT>' ) , max_length = <NUM_LIT> , <EOL> help_text = _ ( '<STR_LIT>' ) , unique = True , blank = True ) <EOL> token_lifetime = models . IntegerField ( <EOL> _ ( '<STR_LIT>' ) , help_text = _ ( '<STR_LIT>' ) , <EOL> null = True , blank = True ) <EOL> password = models . CharField ( <EOL> _ ( '<STR_LIT>' ) , max_length = <NUM_LIT> , <EOL> help_text = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> blank = True ) <EOL> type = models . IntegerField ( <EOL> _ ( '<STR_LIT>' ) , <EOL> help_text = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> choices = TYPE_CHOICES , default = TYPE_CONFIDENTIAL ) <EOL> scopes = models . ManyToManyField ( <EOL> Scope , verbose_name = _ ( '<STR_LIT>' ) , <EOL> help_text = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> blank = True ) <EOL> hash_sign_supported = models . BooleanField ( <EOL> _ ( '<STR_LIT>' ) , <EOL> help_text = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> default = True ) <EOL> class Meta ( object ) : <EOL> verbose_name = _ ( '<STR_LIT>' ) <EOL> verbose_name_plural = _ ( '<STR_LIT>' ) <EOL> def is_public ( self ) : <EOL> return self . type == self . TYPE_PUBLIC <EOL> def is_confidential ( self ) : <EOL> return self . type == self . TYPE_CONFIDENTIAL <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT:%s>' % self . title <EOL> @ classmethod <EOL> def generate_indentifier ( cls ) : <EOL> """<STR_LIT>""" <EOL> return str ( uuid4 ( ) ) . replace ( '<STR_LIT:->' , '<STR_LIT>' ) <EOL> def save ( self , force_insert = False , force_update = False , ** kwargs ) : <EOL> if not self . identifier : <EOL> while True : <EOL> self . identifier = self . generate_indentifier ( ) <EOL> try : <EOL> super ( Client , self ) . save ( force_insert , force_update , ** kwargs ) <EOL> except IntegrityError : <EOL> pass <EOL> else : <EOL> break <EOL> else : <EOL> super ( Client , self ) . save ( force_insert , force_update , ** kwargs ) <EOL> @ python_2_unicode_compatible <EOL> class RedirectionEndpoint ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> client = models . ForeignKey ( Client , verbose_name = _ ( '<STR_LIT>' ) , related_name = '<STR_LIT>' ) <EOL> '''<STR_LIT>''' <EOL> uri = URLSchemeField ( <EOL> _ ( '<STR_LIT>' ) , <EOL> help_text = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> ) <EOL> class Meta ( object ) : <EOL> verbose_name = _ ( '<STR_LIT>' ) <EOL> verbose_name_plural = _ ( '<STR_LIT>' ) <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT:%s>' % self . uri <EOL> @ python_2_unicode_compatible <EOL> class AuthorizationCode ( models . Model ) : <EOL> date_issued = models . DateTimeField ( _ ( '<STR_LIT>' ) , auto_now_add = True ) <EOL> code = models . CharField ( <EOL> _ ( '<STR_LIT>' ) , max_length = <NUM_LIT:7> , unique = True , blank = True , <EOL> help_text = _ ( '<STR_LIT>' ) ) <EOL> user = models . ForeignKey ( USER_MODEL , verbose_name = _ ( '<STR_LIT>' ) , help_text = _ ( '<STR_LIT>' ) ) <EOL> client = models . ForeignKey ( <EOL> Client , verbose_name = _ ( '<STR_LIT>' ) , help_text = _ ( '<STR_LIT>' ) ) <EOL> uri = URLSchemeField ( _ ( '<STR_LIT>' ) , help_text = _ ( '<STR_LIT>' ) ) <EOL> scopes = models . ManyToManyField ( <EOL> Scope , verbose_name = _ ( '<STR_LIT>' ) , <EOL> help_text = _ ( '<STR_LIT>' ) , blank = True ) <EOL> class Meta ( object ) : <EOL> verbose_name = _ ( '<STR_LIT>' ) <EOL> verbose_name_plural = _ ( '<STR_LIT>' ) <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT:%s>' % self . code <EOL> @ classmethod <EOL> def generate_code ( cls ) : <EOL> """<STR_LIT>""" <EOL> return randrange ( <NUM_LIT> , <NUM_LIT> ) <EOL> def save ( self , force_insert = False , force_update = False , ** kwargs ) : <EOL> if not self . code : <EOL> while True : <EOL> self . code = self . generate_code ( ) <EOL> try : <EOL> super ( AuthorizationCode , self ) . save ( force_insert , force_update , ** kwargs ) <EOL> except IntegrityError : <EOL> pass <EOL> else : <EOL> break <EOL> else : <EOL> super ( AuthorizationCode , self ) . save ( force_insert , force_update , ** kwargs ) <EOL> @ python_2_unicode_compatible <EOL> class Token ( models . Model ) : <EOL> date_issued = models . DateTimeField ( _ ( '<STR_LIT>' ) , auto_now_add = True ) <EOL> expires_at = models . DateTimeField ( _ ( '<STR_LIT>' ) , null = True , blank = True ) <EOL> access_token = models . CharField ( <EOL> _ ( '<STR_LIT>' ) , max_length = <NUM_LIT:32> , help_text = _ ( '<STR_LIT>' ) , unique = True , blank = True ) <EOL> refresh_token = models . CharField ( <EOL> _ ( '<STR_LIT>' ) , max_length = <NUM_LIT:32> , help_text = _ ( '<STR_LIT>' ) , <EOL> unique = True , null = True , blank = True ) <EOL> access_token_type = models . CharField ( <EOL> _ ( '<STR_LIT>' ) , max_length = <NUM_LIT:100> , <EOL> help_text = _ ( '<STR_LIT>' ) , <EOL> choices = [ ( t [ <NUM_LIT:0> ] , t [ <NUM_LIT:1> ] ) for t in REGISTRY_TOKEN_TYPE ] , default = TOKEN_TYPE_BEARER ) <EOL> user = models . ForeignKey ( <EOL> USER_MODEL , verbose_name = _ ( '<STR_LIT>' ) , help_text = _ ( '<STR_LIT>' ) , null = True , blank = True ) <EOL> client = models . ForeignKey ( <EOL> Client , verbose_name = _ ( '<STR_LIT>' ) , help_text = _ ( '<STR_LIT>' ) ) <EOL> code = models . ForeignKey ( <EOL> AuthorizationCode , verbose_name = _ ( '<STR_LIT>' ) , help_text = _ ( '<STR_LIT>' ) , <EOL> null = True , blank = True ) <EOL> scopes = models . ManyToManyField ( <EOL> Scope , verbose_name = _ ( '<STR_LIT>' ) , help_text = _ ( '<STR_LIT>' ) , blank = True ) <EOL> class Meta ( object ) : <EOL> verbose_name = _ ( '<STR_LIT>' ) <EOL> verbose_name_plural = _ ( '<STR_LIT>' ) <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT:%s>' % self . code <EOL> @ classmethod <EOL> def generate_token ( cls ) : <EOL> """<STR_LIT>""" <EOL> return str ( uuid4 ( ) ) . replace ( '<STR_LIT:->' , '<STR_LIT>' ) <EOL> def save ( self , force_insert = False , force_update = False , ** kwargs ) : <EOL> if not self . access_token : <EOL> while True : <EOL> self . access_token = self . generate_token ( ) <EOL> self . refresh_token = self . generate_token ( ) <EOL> try : <EOL> super ( Token , self ) . save ( force_insert , force_update , ** kwargs ) <EOL> except IntegrityError : <EOL> pass <EOL> else : <EOL> break <EOL> else : <EOL> super ( Token , self ) . save ( force_insert , force_update , ** kwargs ) </s>
