Skip to content

Commit 46be033

Browse files
author
Gusted
committed
Load mCaptcha on register page
1 parent d56af8e commit 46be033

File tree

11 files changed

+118
-2
lines changed

11 files changed

+118
-2
lines changed

custom/conf/app.example.ini

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,8 @@ ROUTER = console
699699
;; Type of captcha you want to use. Options: image, recaptcha, hcaptcha
700700
;CAPTCHA_TYPE = image
701701
;;
702+
;; Change this to use recaptcha.net or other recaptcha service
703+
;RECAPTCHA_URL = https://www.google.com/recaptcha/
702704
;; Enable recaptcha to use Google's recaptcha service
703705
;; Go to https://www.google.com/recaptcha/admin to sign up for a key
704706
;RECAPTCHA_SECRET =
@@ -708,8 +710,13 @@ ROUTER = console
708710
;HCAPTCHA_SECRET =
709711
;HCAPTCHA_SITEKEY =
710712
;;
711-
;; Change this to use recaptcha.net or other recaptcha service
712-
;RECAPTCHA_URL = https://www.google.com/recaptcha/
713+
;; Change this to use demo.mcaptcha.org or your self-hosted mcaptcha.org instance.
714+
;MCAPTCHA_URL = https://demo.mcaptcha.org
715+
;;
716+
;; Go to your configured mCaptcha instance and register a sitekey
717+
;; and use your account's secret.
718+
;MCAPTCHA_SECRET =
719+
;MCAPTCHA_SITEKEY =
713720
;;
714721
;; Default value for KeepEmailPrivate
715722
;; Each new user will get the value of this setting copied into their profile

modules/setting/setting.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ const (
5959
ImageCaptcha = "image"
6060
ReCaptcha = "recaptcha"
6161
HCaptcha = "hcaptcha"
62+
MCaptcha = "mcaptcha"
6263
)
6364

6465
// settings

package-lock.json

Lines changed: 63 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
},
99
"dependencies": {
1010
"@claviska/jquery-minicolors": "2.3.6",
11+
"@mcaptcha/vanilla-glue": "0.1.0-alpha-1",
1112
"@primer/octicons": "17.3.0",
1213
"add-asset-webpack-plugin": "2.0.1",
1314
"css-loader": "6.7.1",

routers/web/auth/auth.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,8 @@ func SignUp(ctx *context.Context) {
414414
ctx.Data["CaptchaType"] = setting.Service.CaptchaType
415415
ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
416416
ctx.Data["HcaptchaSitekey"] = setting.Service.HcaptchaSitekey
417+
ctx.Data["McaptchaSitekey"] = setting.Service.McaptchaSitekey
418+
ctx.Data["McaptchaURL"] = setting.Service.McaptchaURL
417419
ctx.Data["PageIsSignUp"] = true
418420

419421
// Show Disabled Registration message if DisableRegistration or AllowOnlyExternalRegistration options are true
@@ -435,6 +437,8 @@ func SignUpPost(ctx *context.Context) {
435437
ctx.Data["CaptchaType"] = setting.Service.CaptchaType
436438
ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
437439
ctx.Data["HcaptchaSitekey"] = setting.Service.HcaptchaSitekey
440+
ctx.Data["McaptchaSitekey"] = setting.Service.McaptchaSitekey
441+
ctx.Data["McaptchaURL"] = setting.Service.McaptchaURL
438442
ctx.Data["PageIsSignUp"] = true
439443

440444
// Permission denied if DisableRegistration or AllowOnlyExternalRegistration options are true

routers/web/auth/linkaccount.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ func LinkAccount(ctx *context.Context) {
4040
ctx.Data["RecaptchaURL"] = setting.Service.RecaptchaURL
4141
ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
4242
ctx.Data["HcaptchaSitekey"] = setting.Service.HcaptchaSitekey
43+
ctx.Data["McaptchaSitekey"] = setting.Service.McaptchaSitekey
44+
ctx.Data["McaptchaURL"] = setting.Service.McaptchaURL
4345
ctx.Data["DisableRegistration"] = setting.Service.DisableRegistration
4446
ctx.Data["AllowOnlyInternalRegistration"] = setting.Service.AllowOnlyInternalRegistration
4547
ctx.Data["ShowRegistrationButton"] = false
@@ -96,6 +98,8 @@ func LinkAccountPostSignIn(ctx *context.Context) {
9698
ctx.Data["CaptchaType"] = setting.Service.CaptchaType
9799
ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
98100
ctx.Data["HcaptchaSitekey"] = setting.Service.HcaptchaSitekey
101+
ctx.Data["McaptchaSitekey"] = setting.Service.McaptchaSitekey
102+
ctx.Data["McaptchaURL"] = setting.Service.McaptchaURL
99103
ctx.Data["DisableRegistration"] = setting.Service.DisableRegistration
100104
ctx.Data["ShowRegistrationButton"] = false
101105

@@ -195,6 +199,8 @@ func LinkAccountPostRegister(ctx *context.Context) {
195199
ctx.Data["CaptchaType"] = setting.Service.CaptchaType
196200
ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
197201
ctx.Data["HcaptchaSitekey"] = setting.Service.HcaptchaSitekey
202+
ctx.Data["McaptchaSitekey"] = setting.Service.McaptchaSitekey
203+
ctx.Data["McaptchaURL"] = setting.Service.McaptchaURL
198204
ctx.Data["DisableRegistration"] = setting.Service.DisableRegistration
199205
ctx.Data["ShowRegistrationButton"] = false
200206

routers/web/auth/openid.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,8 @@ func RegisterOpenID(ctx *context.Context) {
341341
ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
342342
ctx.Data["HcaptchaSitekey"] = setting.Service.HcaptchaSitekey
343343
ctx.Data["RecaptchaURL"] = setting.Service.RecaptchaURL
344+
ctx.Data["McaptchaSitekey"] = setting.Service.McaptchaSitekey
345+
ctx.Data["McaptchaURL"] = setting.Service.McaptchaURL
344346
ctx.Data["OpenID"] = oid
345347
userName, _ := ctx.Session.Get("openid_determined_username").(string)
346348
if userName != "" {
@@ -372,6 +374,8 @@ func RegisterOpenIDPost(ctx *context.Context) {
372374
ctx.Data["CaptchaType"] = setting.Service.CaptchaType
373375
ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
374376
ctx.Data["HcaptchaSitekey"] = setting.Service.HcaptchaSitekey
377+
ctx.Data["McaptchaSitekey"] = setting.Service.McaptchaSitekey
378+
ctx.Data["McaptchaURL"] = setting.Service.McaptchaURL
375379
ctx.Data["OpenID"] = oid
376380

377381
if setting.Service.AllowOnlyInternalRegistration {

templates/user/auth/signup_inner.tmpl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@
5454
<div class="h-captcha" data-sitekey="{{ .HcaptchaSitekey }}"></div>
5555
</div>
5656
{{end}}
57+
{{if and .EnableCaptcha (eq .CaptchaType "mcaptcha")}}
58+
<div class="inline field required df ac">
59+
<label></label>
60+
<div class="border-secondary" id="mcaptcha__widget-container" style="width: 304px; height: 78px"></div>
61+
<div class="m-captcha" data-sitekey="{{ .McaptchaSitekey }}"></div>
62+
</div>
63+
{{end}}
64+
5765

5866
<div class="inline field">
5967
<label></label>

templates/user/auth/signup_openid_register.tmpl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@
4040
<div class="h-captcha" data-sitekey="{{ .HcaptchaSitekey }}"></div>
4141
</div>
4242
{{end}}
43+
{{if and .EnableCaptcha (eq .CaptchaType "mcaptcha")}}
44+
<div class="inline field required">
45+
<div class="m-captcha" data-sitekey="{{ .McaptchaSitekey }}"></div>
46+
</div>
47+
{{end}}
4348
<div class="inline field">
4449
<label for="openid">OpenID URI</label>
4550
<input id="openid" value="{{ .OpenID }}" readonly>

web_src/js/features/mcaptcha.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export async function initMcaptcha() {
2+
const siteKeyEl = document.querySelector('.m-captcha');
3+
4+
if (!siteKeyEl) {
5+
return;
6+
}
7+
const {default: mCaptcha} = await import(/* webpackChunkName: "mcaptcha-vanilla-glue" */'@mcaptcha/vanilla-glue');
8+
const siteKey = siteKeyEl.getAttribute('data-sitekey');
9+
mCaptcha.default({
10+
siteKey: {
11+
instanceUrl: new URL('http://localhost:7000'),
12+
key: siteKey,
13+
}
14+
});
15+
}

web_src/js/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ import {initCommonOrganization} from './features/common-organization.js';
8686
import {initRepoWikiForm} from './features/repo-wiki.js';
8787
import {initRepoCommentForm, initRepository} from './features/repo-legacy.js';
8888
import {initFormattingReplacements} from './features/formatting.js';
89+
import {initMcaptcha} from './features/mcaptcha.js';
8990

9091
// Run time-critical code as soon as possible. This is safe to do because this
9192
// script appears at the end of <body> and rendered HTML is accessible at that point.
@@ -182,6 +183,7 @@ $(document).ready(() => {
182183
initRepository();
183184

184185
initCommitStatuses();
186+
initMcaptcha();
185187

186188
initUserAuthLinkAccountView();
187189
initUserAuthOauth2();

0 commit comments

Comments
 (0)