From 7633053490eff99945bddda556aa5adccbb7a1e3 Mon Sep 17 00:00:00 2001 From: aronwk-aaron Date: Fri, 17 Nov 2023 16:38:01 -0600 Subject: [PATCH] it works --- app/__init__.py | 29 ++++---- app/forms.py | 68 ++++++------------- app/templates/flask_user/login.html | 7 +- .../flask_user/login_or_register.html | 10 +++ app/templates/flask_user/register.html | 5 ++ 5 files changed, 56 insertions(+), 63 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 5ba9792..756b8ed 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -328,20 +328,21 @@ def register_settings(app): app.config['RECAPTCHA_PRIVATE_KEY'] ) # Optional - app.config['RECAPTCHA_API_SERVER'] = os.getenv( - 'RECAPTCHA_API_SERVER', - app.config['RECAPTCHA_API_SERVER'] - ) - app.config['RECAPTCHA_PARAMETERS'] = os.getenv( - 'RECAPTCHA_PARAMETERS', - app.config['RECAPTCHA_PARAMETERS'] - ) - if "RECAPTCHA_DATA_ATTRS" not in app.config: - app.config['RECAPTCHA_DATA_ATTRS'] = {'theme': 'white', 'size': 'invisible'} - app.config['RECAPTCHA_DATA_ATTRS'] = os.getenv( - 'RECAPTCHA_DATA_ATTRS', - app.config['RECAPTCHA_DATA_ATTRS'] - ) + if "RECAPTCHA_API_SERVER" in app.config: + app.config['RECAPTCHA_API_SERVER'] = os.getenv( + 'RECAPTCHA_API_SERVER', + app.config['RECAPTCHA_API_SERVER'] + ) + if "RECAPTCHA_PARAMETERS" in app.config: + app.config['RECAPTCHA_PARAMETERS'] = os.getenv( + 'RECAPTCHA_PARAMETERS', + app.config['RECAPTCHA_PARAMETERS'] + ) + if "RECAPTCHA_DATA_ATTRS" in app.config: + app.config['RECAPTCHA_DATA_ATTRS'] = os.getenv( + 'RECAPTCHA_DATA_ATTRS', + app.config['RECAPTCHA_DATA_ATTRS'] + ) diff --git a/app/forms.py b/app/forms.py index 5898611..30d9be3 100644 --- a/app/forms.py +++ b/app/forms.py @@ -1,18 +1,15 @@ -from flask_wtf import FlaskForm -from flask_wtf.recaptcha import RecaptchaField +from flask_wtf import FlaskForm, Recaptcha, RecaptchaField from flask import current_app from flask_user.forms import ( unique_email_validator, - password_validator, - unique_username_validator + LoginForm, + RegisterForm ) from flask_user import UserManager from wtforms.widgets import TextArea, NumberInput from wtforms import ( StringField, - HiddenField, - PasswordField, BooleanField, SubmitField, validators, @@ -37,35 +34,21 @@ def validate_play_key(form, field): field.data = PlayKey.key_is_valid(key_string=field.data) 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): def customize(self, app): self.RegisterFormClass = CustomRegisterForm + self.LoginFormClass = CustomLoginForm - -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, - ] - ) - +class CustomRegisterForm(RegisterForm): play_key_id = StringField( 'Play Key', validators=[ @@ -73,25 +56,14 @@ class CustomRegisterForm(FlaskForm): validate_play_key, ] ) + recaptcha = RecaptchaField( + validators=[validate_recaptcha] + ) - password = PasswordField('Password', validators=[ - DataRequired(), - password_validator, - 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 CustomLoginForm(LoginForm): + recaptcha = RecaptchaField( + validators=[validate_recaptcha] + ) class CreatePlayKeyForm(FlaskForm): diff --git a/app/templates/flask_user/login.html b/app/templates/flask_user/login.html index c8d7e3d..2353edd 100644 --- a/app/templates/flask_user/login.html +++ b/app/templates/flask_user/login.html @@ -60,7 +60,12 @@ {# 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 %} {# Submit button #} diff --git a/app/templates/flask_user/login_or_register.html b/app/templates/flask_user/login_or_register.html index e51a6f1..59fd755 100644 --- a/app/templates/flask_user/login_or_register.html +++ b/app/templates/flask_user/login_or_register.html @@ -29,6 +29,11 @@ {{ render_checkbox_field(login_form.remember_me, tabindex=130) }} {% endif %} + {# recaptcha #} + {% if config.RECAPTCHA_ENABLE %} + {{ form.recaptcha }} + {% endif %} + {# Submit button #} {{ render_submit_field(login_form.submit, tabindex=180) }} @@ -63,6 +68,11 @@ {{ render_field(register_form.retype_password, tabindex=240) }} {% endif %} + {# recaptcha #} + {% if config.RECAPTCHA_ENABLE %} + {{ register_form.recaptcha }} + {% endif %} + {{ render_submit_field(register_form.submit, tabindex=280) }} diff --git a/app/templates/flask_user/register.html b/app/templates/flask_user/register.html index f6a6e28..99bc9d3 100644 --- a/app/templates/flask_user/register.html +++ b/app/templates/flask_user/register.html @@ -47,6 +47,11 @@ {{ render_field(form.retype_password, tabindex=240) }} {% endif %} + {# recaptcha #} + {% if config.RECAPTCHA_ENABLE %} + {{ render_field(form.recaptcha, tabindex=250) }} + {% endif %} + {{ render_submit_field(form.submit, tabindex=280) }}