Projet

Général

Profil

0001-auth_saml-raise-error-when-no-saml-attribute-value-r.patch

Serghei Mihai (congés, retour 15/05), 15 octobre 2020 10:46

Télécharger (2,14 ko)

Voir les différences:

Subject: [PATCH] auth_saml: raise error when no saml attribute value received
 (#47706)

 src/authentic2_auth_saml/adapters.py |  4 +++-
 tests/test_auth_saml.py              | 12 ++++++++++++
 2 files changed, 15 insertions(+), 1 deletion(-)
src/authentic2_auth_saml/adapters.py
158 158

  
159 159
    def set_user_attribute(self, user, attribute, value):
160 160
        if isinstance(value, list):
161
            if len(value) < 1:
162
                raise MappingError('no value for %s' % attribute)
161 163
            if len(value) > 1:
162
                raise MappingError('too much values')
164
                raise MappingError('too many values')
163 165
            value = value[0]
164 166
        if attribute in ('first_name', 'last_name', 'email', 'username'):
165 167
            if getattr(user, attribute) != value:
tests/test_auth_saml.py
23 23

  
24 24
from django.contrib.auth import get_user_model
25 25
from authentic2.models import Attribute
26
from authentic2_auth_saml.adapters import MappingError
27

  
26 28

  
27 29
def test_providers_on_login_page(db, app, settings):
28 30
    settings.A2_AUTH_SAML_ENABLE = True
......
134 136
    del saml_attributes['mail']
135 137
    assert adapter.lookup_user(idp, saml_attributes) is None
136 138

  
139
    # simulate no attribute value
140
    saml_attributes['first_name'] = []
141
    mapping = {
142
        'attribute': 'first_name',
143
        'saml_attribute': 'first_name',
144
    }
145
    with pytest.raises(MappingError, match='no value for first_name'):
146
        adapter.action_set_attribute(user, idp, saml_attributes, mapping)
147

  
148

  
137 149

  
138 150
def test_login_with_conditionnal_authenticators(db, app, settings, caplog):
139 151

  
140
-