2017-05-22 18:12:45 +02:00
|
|
|
# fargo - document box
|
2019-05-15 18:23:48 +02:00
|
|
|
# Copyright (C) 2016-2019 Entr'ouvert
|
2017-05-22 18:12:45 +02:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify it
|
|
|
|
# under the terms of the GNU Affero General Public License as published
|
|
|
|
# by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import uuid
|
2018-03-23 16:39:52 +01:00
|
|
|
import datetime
|
2017-05-22 18:12:45 +02:00
|
|
|
|
2018-03-23 16:39:52 +01:00
|
|
|
from django.conf import settings
|
2017-05-22 18:12:45 +02:00
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from django.core.validators import URLValidator
|
|
|
|
from django.db import models
|
2018-04-05 22:21:04 +02:00
|
|
|
from django.utils.encoding import python_2_unicode_compatible
|
2017-05-22 18:12:45 +02:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2018-03-23 16:39:52 +01:00
|
|
|
from django.db.models.query import QuerySet
|
|
|
|
from django.utils.timezone import now
|
2017-05-22 18:12:45 +02:00
|
|
|
|
|
|
|
from fargo.fargo.models import Document, UserDocument
|
|
|
|
|
|
|
|
|
|
|
|
def generate_uuid():
|
|
|
|
return uuid.uuid4().hex
|
|
|
|
|
|
|
|
|
|
|
|
def validate_https_url(data):
|
|
|
|
errors = []
|
|
|
|
data = data.strip()
|
|
|
|
if not data:
|
|
|
|
return
|
|
|
|
for url in data.split():
|
|
|
|
try:
|
|
|
|
URLValidator(schemes=['http', 'https'])(url)
|
|
|
|
except ValidationError as e:
|
|
|
|
errors.append(e)
|
|
|
|
if errors:
|
|
|
|
raise ValidationError(errors)
|
|
|
|
|
|
|
|
|
2018-04-05 22:21:04 +02:00
|
|
|
@python_2_unicode_compatible
|
2017-05-22 18:12:45 +02:00
|
|
|
class OAuth2Client(models.Model):
|
|
|
|
client_name = models.CharField(max_length=255)
|
|
|
|
redirect_uris = models.TextField(
|
|
|
|
verbose_name=_('redirect URIs'),
|
|
|
|
validators=[validate_https_url])
|
2018-03-31 15:32:19 +02:00
|
|
|
client_id = models.CharField(max_length=255, default=generate_uuid)
|
|
|
|
client_secret = models.CharField(max_length=255, default=generate_uuid)
|
2017-05-22 18:12:45 +02:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return 'OAuth2Client name: %s with id: %s' % (self.client_name, self.client_id)
|
|
|
|
|
|
|
|
def get_redirect_uris(self):
|
|
|
|
return self.redirect_uris.split()
|
|
|
|
|
|
|
|
def check_redirect_uri(self, redirect_uri):
|
|
|
|
return redirect_uri in self.redirect_uris.strip().split()
|
|
|
|
|
2018-04-05 22:21:04 +02:00
|
|
|
def __str__(self):
|
2018-03-22 00:51:46 +01:00
|
|
|
return self.client_name
|
|
|
|
|
2018-03-31 15:32:19 +02:00
|
|
|
class Meta:
|
|
|
|
ordering = ('client_name',)
|
|
|
|
verbose_name = _('OAUTH2 client')
|
|
|
|
verbose_name_plural = _('OAUTH2 clients')
|
|
|
|
|
2017-05-22 18:12:45 +02:00
|
|
|
|
2018-03-23 16:39:52 +01:00
|
|
|
class CleanupQuerySet(QuerySet):
|
|
|
|
def cleanup(self, n=None):
|
|
|
|
n = n or now()
|
|
|
|
threshold = n - datetime.timedelta(seconds=2 * self.model.get_lifetime())
|
|
|
|
self.filter(creation_date__lt=threshold).delete()
|
|
|
|
|
|
|
|
|
2018-03-22 00:48:00 +01:00
|
|
|
class OAuth2Authorize(models.Model):
|
|
|
|
client = models.ForeignKey(OAuth2Client)
|
|
|
|
user_document = models.ForeignKey(UserDocument)
|
|
|
|
access_token = models.CharField(max_length=255, default=generate_uuid)
|
|
|
|
code = models.CharField(max_length=255, default=generate_uuid)
|
2018-03-26 15:25:54 +02:00
|
|
|
creation_date = models.DateTimeField(auto_now_add=True)
|
2018-03-22 00:48:00 +01:00
|
|
|
|
2018-03-23 16:39:52 +01:00
|
|
|
objects = CleanupQuerySet.as_manager()
|
|
|
|
|
2018-03-31 15:32:19 +02:00
|
|
|
class Meta:
|
|
|
|
ordering = ('creation_date',)
|
|
|
|
verbose_name = _('OAUTH2 authorization')
|
|
|
|
verbose_name_plural = _('OAUTH2 authorizations')
|
|
|
|
|
2018-03-23 16:39:52 +01:00
|
|
|
@classmethod
|
|
|
|
def get_lifetime(cls):
|
|
|
|
return max(
|
|
|
|
settings.FARGO_CODE_LIFETIME,
|
|
|
|
settings.FARGO_ACCESS_TOKEN_LIFETIME)
|
|
|
|
|
2018-03-22 00:48:00 +01:00
|
|
|
def __repr__(self):
|
|
|
|
return 'OAuth2Authorize for document %r' % self.user_document
|
|
|
|
|
|
|
|
|
2017-05-22 18:12:45 +02:00
|
|
|
class OAuth2TempFile(models.Model):
|
2018-03-24 08:30:33 +01:00
|
|
|
uuid = models.CharField(max_length=32, default=generate_uuid, primary_key=True)
|
2018-03-22 00:48:00 +01:00
|
|
|
client = models.ForeignKey(OAuth2Client)
|
2018-03-23 16:39:52 +01:00
|
|
|
document = models.ForeignKey(Document, related_name='oauth2_tempfiles')
|
2017-05-22 18:12:45 +02:00
|
|
|
filename = models.CharField(max_length=512)
|
2018-03-26 15:25:54 +02:00
|
|
|
creation_date = models.DateTimeField(auto_now_add=True)
|
2018-03-23 16:39:52 +01:00
|
|
|
|
|
|
|
objects = CleanupQuerySet.as_manager()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_lifetime(cls):
|
|
|
|
return settings.FARGO_OAUTH2_TEMPFILE_LIFETIME
|
2018-03-31 15:32:19 +02:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('creation_date',)
|
|
|
|
verbose_name = _('OAUTH2 temporary file')
|
|
|
|
verbose_name_plural = _('OAUTH2 temporary files')
|