34 |
34 |
from django.contrib.auth import login as auth_login
|
35 |
35 |
from django.core.exceptions import FieldDoesNotExist, ImproperlyConfigured, ValidationError
|
36 |
36 |
from django.core.mail import EmailMessage, send_mail
|
|
37 |
from django.db import transaction
|
37 |
38 |
from django.forms.utils import ErrorList, to_current_timezone
|
38 |
39 |
from django.http import HttpResponse, HttpResponseRedirect
|
39 |
40 |
from django.http.request import QueryDict
|
... | ... | |
45 |
46 |
from django.utils.encoding import iri_to_uri, uri_to_iri
|
46 |
47 |
from django.utils.formats import localize
|
47 |
48 |
from django.utils.translation import ngettext
|
|
49 |
from requests import RequestException
|
48 |
50 |
|
49 |
51 |
from authentic2.saml.saml2utils import filter_attribute_private_key, filter_element_private_key
|
50 |
52 |
from authentic2.validators import EmailValidator
|
51 |
53 |
|
52 |
54 |
from .. import app_settings, constants, crypto, plugins
|
53 |
55 |
from .cache import GlobalCache
|
|
56 |
from .requests_wrapper import Requests
|
54 |
57 |
|
55 |
58 |
|
56 |
59 |
class CleanLogMessage(logging.Filter):
|
... | ... | |
792 |
795 |
return delete_url
|
793 |
796 |
|
794 |
797 |
|
|
798 |
def generate_code(phone_number):
|
|
799 |
from authentic2.models import SMSCode
|
|
800 |
|
|
801 |
return SMSCode.create(
|
|
802 |
phone_number,
|
|
803 |
kind='registration',
|
|
804 |
)
|
|
805 |
|
|
806 |
|
|
807 |
class SMSError(Exception):
|
|
808 |
pass
|
|
809 |
|
|
810 |
|
|
811 |
def send_registration_sms(request, phone_number, ou, template_names=None, context=None, **kwargs):
|
|
812 |
"""Sends a registration code sms to a user, the latter inputs the received code
|
|
813 |
in a dedicated form to validate their account creation.
|
|
814 |
"""
|
|
815 |
|
|
816 |
logger = logging.getLogger(__name__)
|
|
817 |
|
|
818 |
sender = settings.SMS_SENDER
|
|
819 |
url = settings.SMS_URL
|
|
820 |
message = render
|
|
821 |
requests = Requests() # Publik signature requests wrapper
|
|
822 |
|
|
823 |
if not sender:
|
|
824 |
logger.error('settings.SMS_SENDER is not set')
|
|
825 |
raise SMSError('SMS improperly configured')
|
|
826 |
if not url:
|
|
827 |
logger.error('settings.SMS_URL is not set')
|
|
828 |
raise SMSError('SMS improperly configured')
|
|
829 |
|
|
830 |
if not template_names:
|
|
831 |
template_names = ['registration/sms_code_registration.txt']
|
|
832 |
if not isinstance(context, dict):
|
|
833 |
context = {}
|
|
834 |
|
|
835 |
code = generate_code(phone_number)
|
|
836 |
context.update({'code': code})
|
|
837 |
|
|
838 |
message = render_plain_text_template_to_string(template_names, context)
|
|
839 |
|
|
840 |
payload = {
|
|
841 |
'message': message,
|
|
842 |
'from': sender,
|
|
843 |
'to': phone_number,
|
|
844 |
}
|
|
845 |
|
|
846 |
try:
|
|
847 |
with transaction.atomic():
|
|
848 |
response = requests.post(url, json=payload, timeout=10)
|
|
849 |
response.raise_for_status()
|
|
850 |
code.sent = True
|
|
851 |
code.save()
|
|
852 |
except RequestException as e:
|
|
853 |
logger.warning('sms registration to %s using %s failed: %s', phone_number, url, e)
|
|
854 |
raise SMSError(f'Error while contacting SMS service: {e}')
|
|
855 |
return code
|
|
856 |
|
|
857 |
|
795 |
858 |
def send_registration_mail(request, email, ou, template_names=None, next_url=None, context=None, **kwargs):
|
796 |
859 |
"""Send a registration mail to an user. All given kwargs will be used
|
797 |
860 |
to completed the user model.
|
798 |
|
-
|