Add config for enabling/disabling recaptcha

This commit is contained in:
Jesse 2020-07-08 04:38:47 -04:00
parent a36e228fc3
commit b771dd6791
No known key found for this signature in database
GPG key ID: 1A8AB3257B32D91F
6 changed files with 20 additions and 6 deletions

View file

@ -38,8 +38,7 @@
<div class='messages'></div>
<div class='buttons'>
<div id="recaptcha"></div>
<br>
<% if(ctx.enableRecaptcha) print(`<div id="recaptcha"></div><br>`); %>
<input type='submit' value='Create an account'/>
</div>
</form>

View file

@ -108,6 +108,10 @@ class Api extends events.EventTarget {
return !!remoteConfig.enableSafety;
}
recaptchaEnabled() {
return !!remoteConfig.enableRecaptcha;
}
hasPrivilege(lookup) {
let minViableRank = null;
for (let p of Object.keys(remoteConfig.privileges)) {

View file

@ -10,12 +10,17 @@ const RECAPTCHA_SITE_KEY = "site key";
class RegistrationView extends events.EventTarget {
constructor() {
super();
// Show the recaptcha only for anonymous users.
const showRecaptcha = (!api.isLoggedIn() && api.recaptchaEnabled());
this._hostNode = document.getElementById("content-holder");
views.replaceContent(
this._hostNode,
template({
userNamePattern: api.getUserNameRegex(),
passwordPattern: api.getPasswordRegex(),
enableRecaptcha: showRecaptcha,
})
);
views.syncScrollPosition();
@ -23,8 +28,7 @@ class RegistrationView extends events.EventTarget {
this._formNode.addEventListener("submit", (e) => this._evtSubmit(e));
this.setRecaptchaToken = this.setRecaptchaToken.bind(this);
// Show the recaptcha for anonymous users.
if (!api.isLoggedIn())
if (showRecaptcha)
this.renderRecaptcha();
}
@ -36,7 +40,6 @@ class RegistrationView extends events.EventTarget {
}
setRecaptchaToken(token) {
console.log("Recaptcha token:", token);
this.recaptchaToken = token;
}

View file

@ -7,6 +7,13 @@ name: szurubooru
domain: # example: http://example.com
# used to salt the users' password hashes and generate filenames for static content
secret: change
# Whether solving a captcha is required for registration for anonymous users.
enable_recaptcha: no
# A reCAPTCHA v2 secret token.
# https://developers.google.com/recaptcha/intro
# https://developers.google.com/recaptcha/docs/display
recaptcha_secret: change
# Delete thumbnails and source files on post delete

View file

@ -49,6 +49,7 @@ def get_info(ctx: rest.Context, _params: Dict[str, str] = {}) -> rest.Response:
"privileges": util.snake_case_to_lower_camel_case_keys(
config.config["privileges"]
),
"enableRecaptcha": config.config["enable_recaptcha"],
},
}
if auth.has_privilege(ctx.user, "posts:view:featured"):

View file

@ -42,7 +42,7 @@ def create_user(
auth.verify_privilege(ctx.user, "users:create:any")
# Verify if the recaptcha was correct.
if expect_recaptcha:
if expect_recaptcha and config.config["enable_recaptcha"]:
resp = requests.post("https://www.google.com/recaptcha/api/siteverify", data={
"secret": config.config["recaptcha_secret"],
"response": ctx.get_param_as_string("recaptchaToken", default=""),