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='messages'></div>
<div class='buttons'> <div class='buttons'>
<div id="recaptcha"></div> <% if(ctx.enableRecaptcha) print(`<div id="recaptcha"></div><br>`); %>
<br>
<input type='submit' value='Create an account'/> <input type='submit' value='Create an account'/>
</div> </div>
</form> </form>

View file

@ -108,6 +108,10 @@ class Api extends events.EventTarget {
return !!remoteConfig.enableSafety; return !!remoteConfig.enableSafety;
} }
recaptchaEnabled() {
return !!remoteConfig.enableRecaptcha;
}
hasPrivilege(lookup) { hasPrivilege(lookup) {
let minViableRank = null; let minViableRank = null;
for (let p of Object.keys(remoteConfig.privileges)) { 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 { class RegistrationView extends events.EventTarget {
constructor() { constructor() {
super(); super();
// Show the recaptcha only for anonymous users.
const showRecaptcha = (!api.isLoggedIn() && api.recaptchaEnabled());
this._hostNode = document.getElementById("content-holder"); this._hostNode = document.getElementById("content-holder");
views.replaceContent( views.replaceContent(
this._hostNode, this._hostNode,
template({ template({
userNamePattern: api.getUserNameRegex(), userNamePattern: api.getUserNameRegex(),
passwordPattern: api.getPasswordRegex(), passwordPattern: api.getPasswordRegex(),
enableRecaptcha: showRecaptcha,
}) })
); );
views.syncScrollPosition(); views.syncScrollPosition();
@ -23,8 +28,7 @@ class RegistrationView extends events.EventTarget {
this._formNode.addEventListener("submit", (e) => this._evtSubmit(e)); this._formNode.addEventListener("submit", (e) => this._evtSubmit(e));
this.setRecaptchaToken = this.setRecaptchaToken.bind(this); this.setRecaptchaToken = this.setRecaptchaToken.bind(this);
// Show the recaptcha for anonymous users. if (showRecaptcha)
if (!api.isLoggedIn())
this.renderRecaptcha(); this.renderRecaptcha();
} }
@ -36,7 +40,6 @@ class RegistrationView extends events.EventTarget {
} }
setRecaptchaToken(token) { setRecaptchaToken(token) {
console.log("Recaptcha token:", token);
this.recaptchaToken = token; this.recaptchaToken = token;
} }

View file

@ -7,6 +7,13 @@ name: szurubooru
domain: # example: http://example.com domain: # example: http://example.com
# used to salt the users' password hashes and generate filenames for static content # used to salt the users' password hashes and generate filenames for static content
secret: change 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 recaptcha_secret: change
# Delete thumbnails and source files on post delete # 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( "privileges": util.snake_case_to_lower_camel_case_keys(
config.config["privileges"] config.config["privileges"]
), ),
"enableRecaptcha": config.config["enable_recaptcha"],
}, },
} }
if auth.has_privilege(ctx.user, "posts:view:featured"): 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") auth.verify_privilege(ctx.user, "users:create:any")
# Verify if the recaptcha was correct. # 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={ resp = requests.post("https://www.google.com/recaptcha/api/siteverify", data={
"secret": config.config["recaptcha_secret"], "secret": config.config["recaptcha_secret"],
"response": ctx.get_param_as_string("recaptchaToken", default=""), "response": ctx.get_param_as_string("recaptchaToken", default=""),