Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add recaptcha support #78

Merged
merged 3 commits into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 44 additions & 12 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,19 +204,23 @@ def register_settings(app):
# Load environment specific settings
app.config['TESTING'] = False
app.config['DEBUG'] = False
app.config['SQLALCHEMY_ENGINE_OPTIONS'] = {
"pool_pre_ping": True,
"pool_size": 10,
"max_overflow": 2,
"pool_recycle": 300,
"pool_pre_ping": True,
"pool_use_lifo": True
}

# always pull these two from the env
app.config['SECRET_KEY'] = os.getenv(
'APP_SECRET_KEY',
app.config['APP_SECRET_KEY']

)
app.config['SQLALCHEMY_DATABASE_URI'] = os.getenv(
'APP_DATABASE_URI',
app.config['APP_DATABASE_URI']
)

# try to get overides, otherwise just use what we have already
app.config['USER_ENABLE_REGISTER'] = os.getenv(
'USER_ENABLE_REGISTER',
app.config['USER_ENABLE_REGISTER']
Expand All @@ -241,14 +245,6 @@ def register_settings(app):
'USER_REQUIRE_INVITATION',
app.config['USER_REQUIRE_INVITATION']
)
app.config['SQLALCHEMY_ENGINE_OPTIONS'] = {
"pool_pre_ping": True,
"pool_size": 10,
"max_overflow": 2,
"pool_recycle": 300,
"pool_pre_ping": True,
"pool_use_lifo": True
}
app.config['MAIL_SERVER'] = os.getenv(
'MAIL_SERVER',
app.config['MAIL_SERVER']
Expand Down Expand Up @@ -312,6 +308,42 @@ def register_settings(app):
app.config['CACHE_LOCATION']
)

# Recaptcha settings
if "RECAPTCHA_ENABLE" not in app.config:
app.config['RECAPTCHA_ENABLE'] = False
app.config['RECAPTCHA_ENABLE'] = os.getenv(
'RECAPTCHA_ENABLE',
app.config['RECAPTCHA_ENABLE']
)
if "RECAPTCHA_PUBLIC_KEY" not in app.config:
app.config['RECAPTCHA_PUBLIC_KEY'] = ''
app.config['RECAPTCHA_PUBLIC_KEY'] = os.getenv(
'RECAPTCHA_PUBLIC_KEY',
app.config['RECAPTCHA_PUBLIC_KEY']
)
if "RECAPTCHA_PRIVATE_KEY" not in app.config:
app.config['RECAPTCHA_PRIVATE_KEY'] = ''
app.config['RECAPTCHA_PRIVATE_KEY'] = os.getenv(
'RECAPTCHA_PRIVATE_KEY',
app.config['RECAPTCHA_PRIVATE_KEY']
)
# Optional
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']
)



def gm_level(gm_level):
Expand Down
61 changes: 18 additions & 43 deletions app/forms.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
from flask_wtf import FlaskForm
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,
Expand All @@ -36,57 +34,34 @@ def validate_play_key(form, field):
field.data = PlayKey.key_is_valid(key_string=field.data)
return

class CustomRecaptcha(Recaptcha):
def __call__(self, form, field):
if not current_app.config.get("RECAPTCHA_ENABLE", False):
return True
return super(CustomRecaptcha, self).__call__(form, field)


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=[
Optional(),
validate_play_key,
]
)
recaptcha = RecaptchaField(
validators=[CustomRecaptcha()]
)

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')

submit = SubmitField('Register')

class CustomLoginForm(LoginForm):
recaptcha = RecaptchaField(
validators=[CustomRecaptcha()]
)

class CreatePlayKeyForm(FlaskForm):

Expand Down
10 changes: 10 additions & 0 deletions app/settings_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,13 @@

# Option will be removed once this feature is full implemeted
ENABLE_CHAR_XML_UPLOAD = False

# Recaptcha settings
# See: https://flask-wtf.readthedocs.io/en/1.2.x/form/#recaptcha
RECAPTCHA_ENABLE = False
RECAPTCHA_PUBLIC_KEY = ''
RECAPTCHA_PRIVATE_KEY = ''
# Optional
# RECAPTCHA_API_SERVER = ''
# RECAPTCHA_PARAMETERS = ''
RECAPTCHA_DATA_ATTRS = {'theme': 'white', 'size': 'invisible'}
188 changes: 0 additions & 188 deletions app/templates/admin/dashboard.html.j2

This file was deleted.

7 changes: 6 additions & 1 deletion app/templates/flask_user/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,12 @@ <h1>{%trans%}Sign in{%endtrans%}</h1>

{# 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 #}
Expand Down
Loading
Loading