44from hashlib import md5 , sha256
55import json
66
7+ from django .apps import apps
78from django .db import models
89from django .utils import timezone
910from django .utils .translation import ugettext_lazy as _
1011from django .conf import settings
1112
13+ from oidc_provider import settings as oidc_settings
14+
1215
1316CLIENT_TYPE_CHOICES = [
1417 ('confidential' , 'Confidential' ),
@@ -54,12 +57,13 @@ def __str__(self):
5457 return u'{0}' .format (self .description )
5558
5659
57- class Client (models .Model ):
60+ class AbstractClient (models .Model ):
5861
5962 name = models .CharField (max_length = 100 , default = '' , verbose_name = _ (u'Name' ))
6063 owner = models .ForeignKey (
6164 settings .AUTH_USER_MODEL , verbose_name = _ (u'Owner' ), blank = True ,
62- null = True , default = None , on_delete = models .SET_NULL , related_name = 'oidc_clients_set' )
65+ null = True , default = None , on_delete = models .SET_NULL ,
66+ related_name = '%(app_label)s_%(class)s_set' )
6367 client_type = models .CharField (
6468 max_length = 30 ,
6569 choices = CLIENT_TYPE_CHOICES ,
@@ -69,7 +73,8 @@ class Client(models.Model):
6973 u' of their credentials. <b>Public</b> clients are incapable.' ))
7074 client_id = models .CharField (max_length = 255 , unique = True , verbose_name = _ (u'Client ID' ))
7175 client_secret = models .CharField (max_length = 255 , blank = True , verbose_name = _ (u'Client SECRET' ))
72- response_types = models .ManyToManyField (ResponseType )
76+ response_types = models .ManyToManyField (
77+ ResponseType , related_name = '%(app_label)s_%(class)s_set' )
7378 jwt_alg = models .CharField (
7479 max_length = 10 ,
7580 choices = JWT_ALGS ,
@@ -115,6 +120,7 @@ class Client(models.Model):
115120 class Meta :
116121 verbose_name = _ (u'Client' )
117122 verbose_name_plural = _ (u'Clients' )
123+ abstract = True
118124
119125 def __str__ (self ):
120126 return u'{0}' .format (self .name )
@@ -158,9 +164,21 @@ def default_redirect_uri(self):
158164 return self .redirect_uris [0 ] if self .redirect_uris else ''
159165
160166
167+ class Client (AbstractClient ):
168+ class Meta (AbstractClient .Meta ):
169+ swappable = 'OIDC_CLIENT_MODEL'
170+
171+
172+ def get_client_model ():
173+ """ Return the Application model that is active in this project. """
174+ return apps .get_model (oidc_settings .get ('OIDC_CLIENT_MODEL' ))
175+
176+
161177class BaseCodeTokenModel (models .Model ):
162178
163- client = models .ForeignKey (Client , verbose_name = _ (u'Client' ), on_delete = models .CASCADE )
179+ client = models .ForeignKey (
180+ oidc_settings .get ('OIDC_CLIENT_MODEL' ), verbose_name = _ (u'Client' ),
181+ on_delete = models .CASCADE )
164182 expires_at = models .DateTimeField (verbose_name = _ (u'Expiration Date' ))
165183 _scope = models .TextField (default = '' , verbose_name = _ (u'Scopes' ))
166184
0 commit comments