Projet

Général

Profil

0001-authenticators-use-validator-instead-of-form-mixin-6.patch

Valentin Deniaud, 16 août 2022 12:23

Télécharger (6,04 ko)

Voir les différences:

Subject: [PATCH] authenticators: use validator instead of form mixin (#68177)

 src/authentic2/apps/authenticators/forms.py        | 14 +-------------
 .../apps/authenticators/migrations/0001_initial.py |  3 +++
 src/authentic2/apps/authenticators/models.py       |  3 ++-
 src/authentic2/utils/evaluate.py                   |  8 ++++++++
 src/authentic2_auth_fc/forms.py                    |  4 +---
 src/authentic2_auth_oidc/forms.py                  |  4 +---
 src/authentic2_auth_saml/forms.py                  |  4 +---
 7 files changed, 17 insertions(+), 23 deletions(-)
src/authentic2/apps/authenticators/forms.py
20 20
from django.utils.translation import ugettext as _
21 21

  
22 22
from authentic2.forms.mixins import SlugMixin
23
from authentic2.utils.evaluate import ExpressionError, validate_condition
24 23

  
25 24
from .models import BaseAuthenticator, LoginPasswordAuthenticator
26 25

  
27 26

  
28
class AuthenticatorFormMixin:
29
    def clean_show_condition(self):
30
        condition = self.cleaned_data['show_condition']
31
        if condition:
32
            try:
33
                validate_condition(condition)
34
            except ExpressionError as e:
35
                raise ValidationError(e.message)
36
        return condition
37

  
38

  
39 27
class AuthenticatorsOrderForm(forms.Form):
40 28
    order = forms.CharField(widget=forms.HiddenInput)
41 29

  
......
67 55
        return super().save()
68 56

  
69 57

  
70
class LoginPasswordAuthenticatorEditForm(AuthenticatorFormMixin, forms.ModelForm):
58
class LoginPasswordAuthenticatorEditForm(forms.ModelForm):
71 59
    class Meta:
72 60
        model = LoginPasswordAuthenticator
73 61
        exclude = ('name', 'slug', 'ou', 'button_label')
src/authentic2/apps/authenticators/migrations/0001_initial.py
6 6
from django.conf import settings
7 7
from django.db import migrations, models
8 8

  
9
import authentic2.utils.evaluate
10

  
9 11

  
10 12
class Migration(migrations.Migration):
11 13

  
......
42 44
                        ),
43 45
                        max_length=1024,
44 46
                        verbose_name='Show condition',
47
                        validators=[authentic2.utils.evaluate.condition_validator],
45 48
                    ),
46 49
                ),
47 50
                (
src/authentic2/apps/authenticators/models.py
26 26
from django.utils.translation import ugettext_lazy as _
27 27

  
28 28
from authentic2 import views
29
from authentic2.utils.evaluate import evaluate_condition
29
from authentic2.utils.evaluate import condition_validator, evaluate_condition
30 30

  
31 31
from .query import AuthenticatorManager
32 32

  
......
57 57
            'except if they come from the specified IP address. Available variables include '
58 58
            'service_ou_slug, service_slug, remote_addr, login_hint and headers.'
59 59
        ),
60
        validators=[condition_validator],
60 61
    )
61 62
    button_description = models.CharField(
62 63
        _('Login button description'),
src/authentic2/utils/evaluate.py
281 281

  
282 282
validate_condition = ConditionValidator()
283 283

  
284

  
285
def condition_validator(value):
286
    try:
287
        validate_condition(value)
288
    except ExpressionError as e:
289
        raise ValidationError(e.message)
290

  
291

  
284 292
condition_safe_globals = {
285 293
    '__builtins__': {
286 294
        'True': True,
src/authentic2_auth_fc/forms.py
17 17
from django import forms
18 18
from django.utils.translation import ugettext_lazy as _
19 19

  
20
from authentic2.apps.authenticators.forms import AuthenticatorFormMixin
21

  
22 20
from .models import SCOPE_CHOICES, FcAuthenticator
23 21

  
24 22

  
25
class FcAuthenticatorForm(AuthenticatorFormMixin, forms.ModelForm):
23
class FcAuthenticatorForm(forms.ModelForm):
26 24
    scopes = forms.MultipleChoiceField(
27 25
        label=_('Scopes'),
28 26
        choices=SCOPE_CHOICES,
src/authentic2_auth_oidc/forms.py
16 16

  
17 17
from django import forms
18 18

  
19
from authentic2.apps.authenticators.forms import AuthenticatorFormMixin
20

  
21 19
from .models import OIDCProvider
22 20

  
23 21

  
24
class OIDCProviderEditForm(AuthenticatorFormMixin, forms.ModelForm):
22
class OIDCProviderEditForm(forms.ModelForm):
25 23
    class Meta:
26 24
        model = OIDCProvider
27 25
        fields = '__all__'
src/authentic2_auth_saml/forms.py
16 16

  
17 17
from django import forms
18 18

  
19
from authentic2.apps.authenticators.forms import AuthenticatorFormMixin
20

  
21 19
from .models import SAMLAuthenticator
22 20

  
23 21

  
24
class SAMLAuthenticatorForm(AuthenticatorFormMixin, forms.ModelForm):
22
class SAMLAuthenticatorForm(forms.ModelForm):
25 23
    class Meta:
26 24
        model = SAMLAuthenticator
27 25
        exclude = ('ou',)
28
-