|
4 | 4 | import swapper |
5 | 5 | from allauth.account.models import EmailAddress |
6 | 6 | from allauth.account.utils import send_email_confirmation |
7 | | -from allauth.utils import ValidationError |
| 7 | +from allauth.utils import valid_email_or_none |
8 | 8 | from django import forms |
9 | 9 | from django.conf import settings |
10 | 10 | from django.contrib.auth import get_user_model, logout |
11 | 11 | from django.contrib.auth.mixins import LoginRequiredMixin |
12 | | -from django.core.exceptions import ObjectDoesNotExist, PermissionDenied |
| 12 | +from django.core.exceptions import ObjectDoesNotExist, PermissionDenied, ValidationError |
13 | 13 | from django.shortcuts import get_object_or_404, redirect, render |
14 | 14 | from django.urls import reverse |
15 | 15 | from django.views.generic import UpdateView |
|
24 | 24 | from .. import settings as app_settings |
25 | 25 | from ..api.views import RadiusTokenMixin |
26 | 26 | from ..utils import get_organization_radius_settings, load_model |
27 | | -from .utils import get_email_from_ava, get_url_or_path |
| 27 | +from .utils import get_url_or_path |
28 | 28 |
|
29 | 29 | logger = logging.getLogger(__name__) |
30 | 30 |
|
@@ -75,42 +75,24 @@ def post_login_hook(self, request, user, session_info): |
75 | 75 | try: |
76 | 76 | user.registered_user |
77 | 77 | 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 | 78 | registered_user = RegisteredUser( |
110 | 79 | user=user, method='saml', is_verified=app_settings.SAML_IS_VERIFIED |
111 | 80 | ) |
112 | 81 | registered_user.full_clean() |
113 | 82 | registered_user.save() |
| 83 | + # The user is just created, it will not have an email address |
| 84 | + if user.email: |
| 85 | + try: |
| 86 | + email_address = EmailAddress( |
| 87 | + user=user, email=user.email, primary=True, verified=True |
| 88 | + ) |
| 89 | + email_address.full_clean() |
| 90 | + email_address.save() |
| 91 | + except ValidationError: |
| 92 | + logger.exception( |
| 93 | + f'Failed email validation for "{user}"' |
| 94 | + ' during SAML user creation' |
| 95 | + ) |
114 | 96 |
|
115 | 97 | def customize_relay_state(self, relay_state): |
116 | 98 | """ |
|
0 commit comments