|
2 | 2 | from urllib.parse import parse_qs, quote, urlencode, urlparse |
3 | 3 |
|
4 | 4 | import swapper |
5 | | -from allauth.account.models import EmailAddress |
6 | 5 | from allauth.account.utils import send_email_confirmation |
7 | | -from allauth.utils import ValidationError |
| 6 | +from allauth.utils import valid_email_or_none |
8 | 7 | from django import forms |
9 | 8 | from django.conf import settings |
10 | 9 | from django.contrib.auth import get_user_model, logout |
|
24 | 23 | from .. import settings as app_settings |
25 | 24 | from ..api.views import RadiusTokenMixin |
26 | 25 | from ..utils import get_organization_radius_settings, load_model |
27 | | -from .utils import get_email_from_ava, get_url_or_path |
| 26 | +from .utils import get_url_or_path |
28 | 27 |
|
29 | 28 | logger = logging.getLogger(__name__) |
30 | 29 |
|
@@ -75,42 +74,20 @@ def post_login_hook(self, request, user, session_info): |
75 | 74 | try: |
76 | 75 | user.registered_user |
77 | 76 | except ObjectDoesNotExist: |
78 | | - email = None |
79 | | - uid_is_email = 'email' in getattr( |
80 | | - settings, 'SAML_ATTRIBUTE_MAPPING', {} |
81 | | - ).get('uid', ()) |
82 | | - if uid_is_email: |
83 | | - email = session_info['name_id'].text |
84 | | - if email is None: |
85 | | - email = get_email_from_ava(session_info['ava']) |
86 | | - if email: |
87 | | - user.email = email |
88 | | - try: |
89 | | - user.full_clean() |
90 | | - user.save() |
91 | | - EmailAddress.objects.create( |
92 | | - user=user, email=email, verified=True, primary=True |
93 | | - ) |
94 | | - except ValidationError: |
95 | | - assertion_email = get_email_from_ava(session_info['ava']) |
96 | | - if assertion_email and assertion_email != email: |
97 | | - user.email = assertion_email |
98 | | - try: |
99 | | - user.full_clean() |
100 | | - user.save() |
101 | | - EmailAddress.objects.create( |
102 | | - user=user, |
103 | | - email=assertion_email, |
104 | | - verified=True, |
105 | | - primary=True, |
106 | | - ) |
107 | | - except ValidationError: |
108 | | - raise ValidationError('Email Verification Failed') |
109 | 77 | registered_user = RegisteredUser( |
110 | 78 | user=user, method='saml', is_verified=app_settings.SAML_IS_VERIFIED |
111 | 79 | ) |
112 | 80 | registered_user.full_clean() |
113 | 81 | registered_user.save() |
| 82 | + # The user is just created, it will not have an email address |
| 83 | + if user.email: |
| 84 | + email = valid_email_or_none(user.email) |
| 85 | + if not email: |
| 86 | + logger.exception( |
| 87 | + f'Failed email validation for "{user}"' |
| 88 | + ' during SAML user creation' |
| 89 | + ) |
| 90 | + send_email_confirmation(request, user, signup=True, email=user.email) |
114 | 91 |
|
115 | 92 | def customize_relay_state(self, relay_state): |
116 | 93 | """ |
|
0 commit comments