| 1 |
""" |
| 2 |
flask_security.registerable |
| 3 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 4 |
|
| 5 |
Flask-Security registerable module |
| 6 |
|
| 7 |
:copyright: (c) 2012 by Matt Wright. |
| 8 |
:copyright: (c) 2019-2026 by J. Christopher Wagner (jwag). |
| 9 |
:license: MIT, see LICENSE for more details. |
| 10 |
""" |
| 11 |
|
| 12 |
from __future__ import annotations |
| 13 |
|
| 14 |
import typing as t |
| 15 |
|
| 16 |
from flask import current_app |
| 17 |
|
| 18 |
from .confirmable import generate_confirmation_link, requires_confirmation |
| 19 |
from .forms import form_errors_munge |
| 20 |
from .proxies import _security, _datastore |
| 21 |
from .recoverable import generate_reset_link |
| 22 |
from .signals import user_registered, user_not_registered |
| 23 |
from .utils import ( |
| 24 |
_config_value as cv, |
| 25 |
do_flash, |
| 26 |
get_message, |
| 27 |
hash_password, |
| 28 |
send_mail, |
| 29 |
url_for_security, |
| 30 |
) |
| 31 |
|
| 32 |
if t.TYPE_CHECKING: # pragma: no cover |
| 33 |
from .forms import ConfirmRegisterForm, RegisterForm, RegisterFormV2 |
| 34 |
|
| 35 |
|
| 36 |
def register_user(registration_form): |
| 37 |
""" |
| 38 |
Calls datastore to create user, triggers post-registration logic |
| 39 |
(e.g. sending confirmation link, sending registration mail) |
| 40 |
:param registration_form: form with user registration data |
| 41 |
:return: user instance |
| 42 |
""" |
| 43 |
|
| 44 |
user_model_kwargs = registration_form.to_dict(only_user=True) |
| 45 |
|
| 46 |
# passwords are not always required - |
| 47 |
# with UNIFIED_SIGNIN and PASSWORD_REQUIRED=False |
| 48 |
if user_model_kwargs["password"]: |
| 49 |
user_model_kwargs["password"] = hash_password(user_model_kwargs["password"]) |
| 50 |
user = _datastore.create_user(**user_model_kwargs) |
| 51 |
|
| 52 |
# if they didn't give a password - auto-setup email magic links (if UNIFIED SIGNIN) |
| 53 |
if not user_model_kwargs["password"] and cv("UNIFIED_SIGNIN"): |
| 54 |
_datastore.us_setup_email(user) |
| 55 |
|
| 56 |
confirmation_link, token = None, None |
| 57 |
if _security.confirmable: |
| 58 |
confirmation_link, token = generate_confirmation_link(user) |
| 59 |
do_flash(*get_message("CONFIRM_REGISTRATION", email=user.email)) |
| 60 |
|
| 61 |
user_registered.send( |
| 62 |
current_app._get_current_object(), |
| 63 |
_async_wrapper=current_app.ensure_sync, |
| 64 |
user=user, |
| 65 |
confirm_token=token, |
| 66 |
confirmation_token=token, |
| 67 |
form_data=registration_form.to_dict(only_user=False), |
| 68 |
) |
| 69 |
|
| 70 |
if cv("SEND_REGISTER_EMAIL"): |
| 71 |
send_mail( |
| 72 |
cv("EMAIL_SUBJECT_REGISTER"), |
| 73 |
user.email, |
| 74 |
cv("REGISTER_EMAIL_TEMPLATE"), |
| 75 |
user=user, |
| 76 |
confirmation_link=confirmation_link, |
| 77 |
confirmation_token=token, |
| 78 |
) |
| 79 |
|
| 80 |
return user |
| 81 |
|
| 82 |
|
| 83 |
def register_existing( |
| 84 |
form: ConfirmRegisterForm | RegisterForm | RegisterFormV2, |
| 85 |
) -> bool: |
| 86 |
""" |
| 87 |
In the case of generic responses we want to mitigate any possible |
| 88 |
email/username enumeration. |
| 89 |
For an existing email we send an email to that address and tell them they |
| 90 |
are already registered (and provide their username if any). |
| 91 |
|
| 92 |
N.B. This (and forgot and confirm) could be used to DDOS an email by constantly |
| 93 |
issuing requests. One way to mitigate that is to use signals and add specific |
| 94 |
application code. |
| 95 |
|
| 96 |
Returning False means to return normal error messages. |
| 97 |
Returns True if the only 'error' is an existing email/user. In this case we |
| 98 |
simulate a normal registration and email the existing account to inform. |
| 99 |
|
| 100 |
""" |
| 101 |
|
| 102 |
if not ( |
| 103 |
cv("RETURN_GENERIC_RESPONSES") |
| 104 |
or form.existing_username_user |
| 105 |
or form.existing_email_user |
| 106 |
): # pragma: no cover |
| 107 |
return False |
| 108 |
|
| 109 |
# There are 2 classes of error - an existing email/username and non-compliant |
| 110 |
# email/username/password. We want to give the user feedback on a non-compliant |
| 111 |
# input - but not give away whether the email/username is already taken. |
| 112 |
# Since in this case we have an 'existing' entry - we simply Null out those |
| 113 |
# errors. |
| 114 |
# This also means for JSON there is no way to tell if things worked or not. |
| 115 |
fields_to_squash: dict[str, dict[str, str]] = dict() |
| 116 |
if form.existing_email_user: |
| 117 |
fields_to_squash["email"] = dict() |
| 118 |
if hasattr(form, "username") and form.existing_username_user: |
| 119 |
fields_to_squash["username"] = dict() |
| 120 |
form_errors_munge(form, fields_to_squash) |
| 121 |
if form.errors: |
| 122 |
# some other illegal password/username - return an error |
| 123 |
return False |
| 124 |
|
| 125 |
# only errors were existing email/username |
| 126 |
hash_password("not-a-password") # reduce timing between successful and not. |
| 127 |
|
| 128 |
# Same as is done in register_user() |
| 129 |
if _security.confirmable: |
| 130 |
do_flash(*get_message("CONFIRM_REGISTRATION", email=form.email.data)) |
| 131 |
|
| 132 |
# 2 cases: |
| 133 |
# 1) existing email (an already registered account) with an empty or same username |
| 134 |
# 2) new email with existing username (which corresponds to some OTHER account) |
| 135 |
|
| 136 |
if form.existing_email_user: |
| 137 |
user_not_registered.send( |
| 138 |
current_app._get_current_object(), # type: ignore |
| 139 |
_async_wrapper=current_app.ensure_sync, # type: ignore[arg-type] |
| 140 |
user=form.existing_email_user, |
| 141 |
existing_email=True, |
| 142 |
existing_username=form.existing_username_user is not None, |
| 143 |
form_data=form.to_dict(only_user=False), |
| 144 |
) |
| 145 |
if cv("SEND_REGISTER_EMAIL"): |
| 146 |
# Send a nice email saying they are already registered - |
| 147 |
# - tell them their existing username if they have one |
| 148 |
# - suggest how to reset password and send reset link/token |
| 149 |
# - if they haven't confirmed - send them confirm link and token |
| 150 |
recovery_link = "" |
| 151 |
reset_link = "" |
| 152 |
reset_token = "" |
| 153 |
if _security.recoverable: |
| 154 |
recovery_link = url_for_security("forgot_password", _external=True) |
| 155 |
reset_link, reset_token = generate_reset_link(form.existing_email_user) |
| 156 |
confirmation_link = "" |
| 157 |
confirmation_token = "" |
| 158 |
if requires_confirmation(form.existing_email_user): |
| 159 |
confirmation_link, confirmation_token = generate_confirmation_link( |
| 160 |
form.existing_email_user |
| 161 |
) |
| 162 |
|
| 163 |
send_mail( |
| 164 |
cv("EMAIL_SUBJECT_REGISTER"), |
| 165 |
form.existing_email_user.email, |
| 166 |
cv("REGISTER_EMAIL_EXISTING_TEMPLATE"), |
| 167 |
user=form.existing_email_user, |
| 168 |
recovery_link=recovery_link, |
| 169 |
reset_link=reset_link, |
| 170 |
reset_token=reset_token, |
| 171 |
confirmation_link=confirmation_link, |
| 172 |
confirmation_token=confirmation_token, |
| 173 |
) |
| 174 |
elif form.existing_username_user: |
| 175 |
# New email, already taken username. |
| 176 |
# Note that we send email to NEW email - so it is possible for a bad-actor |
| 177 |
# to enumerate usernames (slowly). |
| 178 |
user_not_registered.send( |
| 179 |
current_app._get_current_object(), # type: ignore[attr-defined] |
| 180 |
_async_wrapper=current_app.ensure_sync, # type: ignore[arg-type] |
| 181 |
user=None, |
| 182 |
existing_email=False, |
| 183 |
existing_username=True, |
| 184 |
form_data=form.to_dict(only_user=False), |
| 185 |
) |
| 186 |
assert form.email.data |
| 187 |
if cv("SEND_REGISTER_EMAIL"): |
| 188 |
send_mail( |
| 189 |
cv("EMAIL_SUBJECT_REGISTER"), |
| 190 |
form.email.data, |
| 191 |
cv("REGISTER_EMAIL_EXISTING_USERNAME_TEMPLATE"), |
| 192 |
email=form.email.data, |
| 193 |
username=form.username.data if hasattr(form, "username") else None, |
| 194 |
) |
| 195 |
|
| 196 |
return True |