2014-03-27 10:01:08 +01:00
|
|
|
from django.core.exceptions import ValidationError
|
2014-08-14 14:14:15 +02:00
|
|
|
from django.core.validators import RegexValidator
|
2014-03-27 10:01:08 +01:00
|
|
|
from django.db import models
|
2014-03-13 11:03:21 +01:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2014-03-27 10:01:08 +01:00
|
|
|
from django.template import Template
|
2014-03-13 11:03:21 +01:00
|
|
|
|
2014-03-07 17:39:50 +01:00
|
|
|
from provider.oauth2.models import Client
|
|
|
|
|
2014-03-10 09:25:03 +01:00
|
|
|
from authentic2.models import LogoutUrlAbstract
|
2014-03-27 10:01:08 +01:00
|
|
|
from authentic2.managers import GetBySlugManager
|
2014-03-10 09:25:03 +01:00
|
|
|
|
2014-08-14 14:14:15 +02:00
|
|
|
|
|
|
|
|
2014-03-10 09:25:03 +01:00
|
|
|
class A2Client(LogoutUrlAbstract, Client):
|
2014-08-01 11:13:17 +02:00
|
|
|
|
|
|
|
authorized_scopes = models.CharField('automatically granted scopes',
|
2014-08-14 14:14:15 +02:00
|
|
|
max_length=256, blank=True,
|
|
|
|
help_text=_('space separated scopes'),
|
|
|
|
validators = [RegexValidator(('^[a-z]+([ \+][a-z]+)+$'))]
|
|
|
|
)
|
2014-03-13 11:03:21 +01:00
|
|
|
class Meta:
|
|
|
|
verbose_name = _('client')
|
|
|
|
verbose_name_plural = _('clients')
|
2014-03-27 10:01:08 +01:00
|
|
|
|
2014-08-14 14:14:15 +02:00
|
|
|
def clean(self):
|
|
|
|
self.authorized_scopes = self.authorized_scopes.strip()
|
|
|
|
|
2014-07-03 15:57:03 +02:00
|
|
|
|
|
|
|
class AttributeRelease(models.Model):
|
|
|
|
client = models.ForeignKey(A2Client, verbose_name=_('client'))
|
|
|
|
name = models.CharField(verbose_name=_('name'), max_length=64)
|
|
|
|
attribute_name = models.CharField(max_length=64,
|
2014-07-16 14:16:19 +02:00
|
|
|
verbose_name=_('attribute name'))
|
2014-07-03 15:57:03 +02:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
verbose_name = _('OAuth2 attribute release')
|
|
|
|
verbose_name_plural = _('oauth2 attribute releases')
|
|
|
|
|
|
|
|
|
2014-03-27 10:01:08 +01:00
|
|
|
class WebService(models.Model):
|
|
|
|
AUTH_MECH = (
|
|
|
|
('', 'None'),
|
|
|
|
('hmac-sha256', 'HMAC-SHA-256'),
|
|
|
|
('hmac-sha1', 'HMAC-SHA-1'),
|
|
|
|
)
|
|
|
|
|
|
|
|
name = models.CharField(max_length=32)
|
|
|
|
slug = models.SlugField(max_length=32)
|
|
|
|
|
|
|
|
url = models.CharField(max_length=1024)
|
|
|
|
|
|
|
|
auth_mech = models.CharField(verbose_name=_('Authentication mechanism'),
|
|
|
|
max_length=16, choices=AUTH_MECH, default='', blank=True)
|
|
|
|
signature_key = models.CharField(verbose_name=_('Signature key'),
|
|
|
|
max_length=128, default='', blank=True)
|
|
|
|
verify_certificate = models.BooleanField(verbose_name=_('verify '
|
|
|
|
'certificate'), default=True, blank=True)
|
|
|
|
allow_redirects = models.BooleanField(verbose_name=_('allows HTTP redirections'),
|
|
|
|
help_text=_('it can improve latencies to forbid redirection follow'),
|
|
|
|
default=True)
|
|
|
|
timeout = models.IntegerField(verbose_name=_('timeout'),
|
|
|
|
default=10,
|
|
|
|
help_text=_('time in second to wait before '
|
|
|
|
'failing to download a datasource'))
|
|
|
|
|
|
|
|
objects = GetBySlugManager()
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
if self.signature_key and (not self.auth_mech or not self.auth_mech.startswith('hmac-')):
|
|
|
|
raise ValidationError(_('You must choose a hashing algorithm if '
|
|
|
|
'you set a signature key'))
|
|
|
|
|
|
|
|
def natural_key(self):
|
|
|
|
return (self.slug,)
|
|
|
|
|
|
|
|
def get_url(self, ctx=None):
|
|
|
|
if ctx is None:
|
|
|
|
ctx = {}
|
|
|
|
return Template(self.url).render(ctx)
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.name
|