This commit is contained in:
aronwk-aaron 2023-11-17 16:38:01 -06:00
parent e6c452d000
commit 7633053490
5 changed files with 56 additions and 63 deletions

View File

@ -328,20 +328,21 @@ def register_settings(app):
app.config['RECAPTCHA_PRIVATE_KEY'] app.config['RECAPTCHA_PRIVATE_KEY']
) )
# Optional # Optional
app.config['RECAPTCHA_API_SERVER'] = os.getenv( if "RECAPTCHA_API_SERVER" in app.config:
'RECAPTCHA_API_SERVER', app.config['RECAPTCHA_API_SERVER'] = os.getenv(
app.config['RECAPTCHA_API_SERVER'] 'RECAPTCHA_API_SERVER',
) app.config['RECAPTCHA_API_SERVER']
app.config['RECAPTCHA_PARAMETERS'] = os.getenv( )
'RECAPTCHA_PARAMETERS', if "RECAPTCHA_PARAMETERS" in app.config:
app.config['RECAPTCHA_PARAMETERS'] app.config['RECAPTCHA_PARAMETERS'] = os.getenv(
) 'RECAPTCHA_PARAMETERS',
if "RECAPTCHA_DATA_ATTRS" not in app.config: app.config['RECAPTCHA_PARAMETERS']
app.config['RECAPTCHA_DATA_ATTRS'] = {'theme': 'white', 'size': 'invisible'} )
app.config['RECAPTCHA_DATA_ATTRS'] = os.getenv( if "RECAPTCHA_DATA_ATTRS" in app.config:
'RECAPTCHA_DATA_ATTRS', app.config['RECAPTCHA_DATA_ATTRS'] = os.getenv(
app.config['RECAPTCHA_DATA_ATTRS'] 'RECAPTCHA_DATA_ATTRS',
) app.config['RECAPTCHA_DATA_ATTRS']
)

View File

@ -1,18 +1,15 @@
from flask_wtf import FlaskForm from flask_wtf import FlaskForm, Recaptcha, RecaptchaField
from flask_wtf.recaptcha import RecaptchaField
from flask import current_app from flask import current_app
from flask_user.forms import ( from flask_user.forms import (
unique_email_validator, unique_email_validator,
password_validator, LoginForm,
unique_username_validator RegisterForm
) )
from flask_user import UserManager from flask_user import UserManager
from wtforms.widgets import TextArea, NumberInput from wtforms.widgets import TextArea, NumberInput
from wtforms import ( from wtforms import (
StringField, StringField,
HiddenField,
PasswordField,
BooleanField, BooleanField,
SubmitField, SubmitField,
validators, validators,
@ -37,35 +34,21 @@ def validate_play_key(form, field):
field.data = PlayKey.key_is_valid(key_string=field.data) field.data = PlayKey.key_is_valid(key_string=field.data)
return return
def validate_recaptcha(form, field):
current_app.logger.info("start validating with recaptcha")
if current_app.config["RECAPTCHA_ENABLE"]:
current_app.logger.info("validating with recaptcha")
return Recaptcha()
current_app.logger.info("skipping validating with recaptcha")
return True
class CustomUserManager(UserManager): class CustomUserManager(UserManager):
def customize(self, app): def customize(self, app):
self.RegisterFormClass = CustomRegisterForm self.RegisterFormClass = CustomRegisterForm
self.LoginFormClass = CustomLoginForm
class CustomRegisterForm(RegisterForm):
class CustomRegisterForm(FlaskForm):
"""Registration form"""
next = HiddenField()
reg_next = HiddenField()
# Login Info
email = StringField(
'E-Mail',
validators=[
Optional(),
validators.Email('Invalid email address'),
unique_email_validator,
]
)
username = StringField(
'Username',
validators=[
DataRequired(),
unique_username_validator,
]
)
play_key_id = StringField( play_key_id = StringField(
'Play Key', 'Play Key',
validators=[ validators=[
@ -73,25 +56,14 @@ class CustomRegisterForm(FlaskForm):
validate_play_key, validate_play_key,
] ]
) )
recaptcha = RecaptchaField(
validators=[validate_recaptcha]
)
password = PasswordField('Password', validators=[ class CustomLoginForm(LoginForm):
DataRequired(), recaptcha = RecaptchaField(
password_validator, validators=[validate_recaptcha]
validators.length(max=40, message="The maximum length of the password is 40 characters due to game client limitations") )
])
retype_password = PasswordField('Retype Password', validators=[
validators.EqualTo('password', message='Passwords did not match'),
validators.length(max=40, message="The maximum length of the password is 40 characters due to game client limitations")
])
invite_token = HiddenField('Token')
# Use recaptcha if config enables recaptcha
if current_app.config["ENABLE_RECAPTCHA"]:
recaptcha = RecaptchaField()
submit = SubmitField('Register')
class CreatePlayKeyForm(FlaskForm): class CreatePlayKeyForm(FlaskForm):

View File

@ -60,7 +60,12 @@
{# Remember me #} {# Remember me #}
{% if user_manager.USER_ENABLE_REMEMBER_ME %} {% if user_manager.USER_ENABLE_REMEMBER_ME %}
{{ render_checkbox_field(login_form.remember_me, tabindex=130) }} {{ render_checkbox_field(login_form.remember_me, tabindex=130) }}
{% endif %}
{# recaptcha #}
{% if config.RECAPTCHA_ENABLE %}
{{ render_field(form.recaptcha, tabindex=250) }}
{% endif %} {% endif %}
{# Submit button #} {# Submit button #}

View File

@ -29,6 +29,11 @@
{{ render_checkbox_field(login_form.remember_me, tabindex=130) }} {{ render_checkbox_field(login_form.remember_me, tabindex=130) }}
{% endif %} {% endif %}
{# recaptcha #}
{% if config.RECAPTCHA_ENABLE %}
{{ form.recaptcha }}
{% endif %}
{# Submit button #} {# Submit button #}
{{ render_submit_field(login_form.submit, tabindex=180) }} {{ render_submit_field(login_form.submit, tabindex=180) }}
</form> </form>
@ -63,6 +68,11 @@
{{ render_field(register_form.retype_password, tabindex=240) }} {{ render_field(register_form.retype_password, tabindex=240) }}
{% endif %} {% endif %}
{# recaptcha #}
{% if config.RECAPTCHA_ENABLE %}
{{ register_form.recaptcha }}
{% endif %}
{{ render_submit_field(register_form.submit, tabindex=280) }} {{ render_submit_field(register_form.submit, tabindex=280) }}
</form> </form>

View File

@ -47,6 +47,11 @@
{{ render_field(form.retype_password, tabindex=240) }} {{ render_field(form.retype_password, tabindex=240) }}
{% endif %} {% endif %}
{# recaptcha #}
{% if config.RECAPTCHA_ENABLE %}
{{ render_field(form.recaptcha, tabindex=250) }}
{% endif %}
{{ render_submit_field(form.submit, tabindex=280) }} {{ render_submit_field(form.submit, tabindex=280) }}
</form> </form>