2015-01-28 18:19:36 +00:00
|
|
|
import json
|
|
|
|
|
2014-12-19 15:27:43 +00:00
|
|
|
from django.db import models
|
|
|
|
from django.utils import timezone
|
2015-01-28 18:19:36 +00:00
|
|
|
from django.contrib.auth.models import User
|
2014-12-19 15:27:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Client(models.Model):
|
|
|
|
|
|
|
|
RESPONSE_TYPE_CHOICES = [
|
2015-01-08 20:55:24 +00:00
|
|
|
('code', 'code (Authorization Code Flow)'),
|
|
|
|
('id_token', 'id_token (Implicit Flow)'),
|
|
|
|
('id_token token', 'id_token token (Implicit Flow)'),
|
2014-12-19 15:27:43 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
name = models.CharField(max_length=100, default='')
|
|
|
|
client_id = models.CharField(max_length=255, unique=True)
|
|
|
|
client_secret = models.CharField(max_length=255, unique=True)
|
2015-02-06 17:26:34 +00:00
|
|
|
response_type = models.CharField(max_length=30,
|
|
|
|
choices=RESPONSE_TYPE_CHOICES)
|
2014-12-19 15:27:43 +00:00
|
|
|
|
2015-01-06 19:29:31 +00:00
|
|
|
_redirect_uris = models.TextField(default='')
|
2015-01-28 18:19:36 +00:00
|
|
|
|
2015-05-30 11:54:04 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.__str__()
|
|
|
|
|
2015-01-06 19:29:31 +00:00
|
|
|
def redirect_uris():
|
|
|
|
def fget(self):
|
|
|
|
return self._redirect_uris.splitlines()
|
|
|
|
def fset(self, value):
|
|
|
|
self._redirect_uris = '\n'.join(value)
|
|
|
|
return locals()
|
|
|
|
redirect_uris = property(**redirect_uris())
|
2014-12-19 15:27:43 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def default_redirect_uri(self):
|
2015-01-16 18:03:41 +00:00
|
|
|
return self.redirect_uris[0] if self.redirect_uris else ''
|
2014-12-19 15:27:43 +00:00
|
|
|
|
2015-01-28 18:19:36 +00:00
|
|
|
|
2015-04-21 18:19:43 +00:00
|
|
|
class BaseCodeTokenModel(models.Model):
|
2014-12-19 15:27:43 +00:00
|
|
|
|
|
|
|
user = models.ForeignKey(User)
|
|
|
|
client = models.ForeignKey(Client)
|
|
|
|
expires_at = models.DateTimeField()
|
2015-01-12 22:13:48 +00:00
|
|
|
_scope = models.TextField(default='')
|
2015-05-30 11:54:04 +00:00
|
|
|
|
2015-04-21 18:19:43 +00:00
|
|
|
def scope():
|
|
|
|
def fget(self):
|
|
|
|
return self._scope.split()
|
|
|
|
def fset(self, value):
|
|
|
|
self._scope = ' '.join(value)
|
|
|
|
return locals()
|
|
|
|
scope = property(**scope())
|
2014-12-19 15:27:43 +00:00
|
|
|
|
|
|
|
def has_expired(self):
|
|
|
|
return timezone.now() >= self.expires_at
|
|
|
|
|
2015-05-30 11:54:04 +00:00
|
|
|
def __str__(self):
|
2015-06-22 21:42:04 +00:00
|
|
|
return "%s - %s (%s)" % (self.client, self.user.email, self.expires_at)
|
2015-05-30 11:54:04 +00:00
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.__str__()
|
|
|
|
|
2015-04-21 18:19:43 +00:00
|
|
|
class Meta:
|
|
|
|
abstract = True
|
2015-01-28 18:19:36 +00:00
|
|
|
|
2014-12-19 15:27:43 +00:00
|
|
|
|
2015-04-21 18:19:43 +00:00
|
|
|
class Code(BaseCodeTokenModel):
|
2015-02-06 17:26:34 +00:00
|
|
|
|
2015-04-21 18:19:43 +00:00
|
|
|
code = models.CharField(max_length=255, unique=True)
|
2014-12-19 15:27:43 +00:00
|
|
|
|
2015-01-28 18:19:36 +00:00
|
|
|
|
2015-04-21 18:19:43 +00:00
|
|
|
class Token(BaseCodeTokenModel):
|
|
|
|
|
|
|
|
access_token = models.CharField(max_length=255, unique=True)
|
|
|
|
_id_token = models.TextField()
|
2014-12-19 15:27:43 +00:00
|
|
|
def id_token():
|
|
|
|
def fget(self):
|
|
|
|
return json.loads(self._id_token)
|
|
|
|
def fset(self, value):
|
|
|
|
self._id_token = json.dumps(value)
|
|
|
|
return locals()
|
|
|
|
id_token = property(**id_token())
|
|
|
|
|
2015-01-28 18:19:36 +00:00
|
|
|
|
2014-12-19 15:27:43 +00:00
|
|
|
class UserInfo(models.Model):
|
|
|
|
|
2015-03-27 17:42:41 +00:00
|
|
|
GENDER_CHOICES = [
|
|
|
|
('F', 'Female'),
|
|
|
|
('M', 'Male'),
|
|
|
|
]
|
|
|
|
|
2014-12-19 15:27:43 +00:00
|
|
|
user = models.OneToOneField(User, primary_key=True)
|
2015-02-18 19:23:00 +00:00
|
|
|
given_name = models.CharField(max_length=255, blank=True, null=True)
|
|
|
|
family_name = models.CharField(max_length=255, blank=True, null=True)
|
|
|
|
middle_name = models.CharField(max_length=255, blank=True, null=True)
|
|
|
|
nickname = models.CharField(max_length=255, blank=True, null=True)
|
2015-03-27 17:42:41 +00:00
|
|
|
gender = models.CharField(max_length=100, choices=GENDER_CHOICES, null=True)
|
2015-02-23 18:02:26 +00:00
|
|
|
birthdate = models.DateField(null=True)
|
|
|
|
zoneinfo = models.CharField(max_length=100, default='', blank=True,
|
|
|
|
null=True)
|
|
|
|
locale = models.CharField(max_length=100, default='', blank=True, null=True)
|
2015-02-18 19:23:00 +00:00
|
|
|
preferred_username = models.CharField(max_length=255, blank=True, null=True)
|
|
|
|
profile = models.URLField(default='', null=True, blank=True)
|
|
|
|
picture = models.URLField(default='', null=True, blank=True)
|
|
|
|
website = models.URLField(default='', null=True, blank=True)
|
|
|
|
email_verified = models.NullBooleanField(default=False)
|
|
|
|
locale = models.CharField(max_length=100, blank=True, null=True)
|
|
|
|
phone_number = models.CharField(max_length=255, blank=True, null=True)
|
|
|
|
phone_number_verified = models.NullBooleanField(default=False)
|
|
|
|
address_street_address = models.CharField(max_length=255, blank=True,
|
|
|
|
null=True)
|
|
|
|
address_locality = models.CharField(max_length=255, blank=True, null=True)
|
|
|
|
address_region = models.CharField(max_length=255, blank=True, null=True)
|
|
|
|
address_postal_code = models.CharField(max_length=255, blank=True,
|
|
|
|
null=True)
|
|
|
|
address_country = models.CharField(max_length=255, blank=True, null=True)
|
2015-02-13 17:55:05 +00:00
|
|
|
updated_at = models.DateTimeField(auto_now=True, null=True)
|
2014-12-19 15:27:43 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
name = ''
|
|
|
|
if self.given_name:
|
|
|
|
name = self.given_name
|
|
|
|
if self.family_name:
|
|
|
|
name = name + ' ' + self.family_name
|
|
|
|
|
2015-02-06 17:26:34 +00:00
|
|
|
return name
|
2015-04-15 17:54:42 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def address_formatted(self):
|
|
|
|
formatted = ', '.join([
|
2015-06-16 17:35:07 +00:00
|
|
|
self.address_street_address or '',
|
|
|
|
self.address_locality or '',
|
|
|
|
self.address_country or ''])
|
2015-04-15 17:54:42 +00:00
|
|
|
|
|
|
|
if formatted.startswith(', '):
|
|
|
|
formatted = formatted[2:]
|
|
|
|
if formatted.endswith(', '):
|
2015-05-30 11:54:04 +00:00
|
|
|
formatted = formatted[:-2]
|
2015-06-22 21:42:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UserConsent(BaseCodeTokenModel):
|
|
|
|
|
|
|
|
pass
|