Skip to content

Commit 359d802

Browse files
Gustedfsologureng
authored andcommitted
CB/bp: Add support mCaptcha as captcha provider (go-gitea#20458)
https://mcaptcha.org/ Co-authored-by: Felipe Leopoldo Sologuren Gutiérrez <[email protected]>
1 parent 9c4027f commit 359d802

File tree

20 files changed

+183
-6
lines changed

20 files changed

+183
-6
lines changed

custom/conf/app.example.ini

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -710,9 +710,11 @@ ROUTER = console
710710
;; Enable captcha validation for registration
711711
;ENABLE_CAPTCHA = false
712712
;;
713-
;; Type of captcha you want to use. Options: image, recaptcha, hcaptcha
713+
;; Type of captcha you want to use. Options: image, recaptcha, hcaptcha, mcaptcha.
714714
;CAPTCHA_TYPE = image
715715
;;
716+
;; Change this to use recaptcha.net or other recaptcha service
717+
;RECAPTCHA_URL = https://www.google.com/recaptcha/
716718
;; Enable recaptcha to use Google's recaptcha service
717719
;; Go to https://www.google.com/recaptcha/admin to sign up for a key
718720
;RECAPTCHA_SECRET =
@@ -722,8 +724,13 @@ ROUTER = console
722724
;HCAPTCHA_SECRET =
723725
;HCAPTCHA_SITEKEY =
724726
;;
725-
;; Change this to use recaptcha.net or other recaptcha service
726-
;RECAPTCHA_URL = https://www.google.com/recaptcha/
727+
;; Change this to use demo.mcaptcha.org or your self-hosted mcaptcha.org instance.
728+
;MCAPTCHA_URL = https://demo.mcaptcha.org
729+
;;
730+
;; Go to your configured mCaptcha instance and register a sitekey
731+
;; and use your account's secret.
732+
;MCAPTCHA_SECRET =
733+
;MCAPTCHA_SITEKEY =
727734
;;
728735
;; Default value for KeepEmailPrivate
729736
;; Each new user will get the value of this setting copied into their profile

docs/content/doc/advanced/config-cheat-sheet.en-us.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -575,13 +575,16 @@ Certain queues have defaults that override the defaults set in `[queue]` (this o
575575
provided email rather than a generated email.
576576
- `ENABLE_CAPTCHA`: **false**: Enable this to use captcha validation for registration.
577577
- `REQUIRE_EXTERNAL_REGISTRATION_CAPTCHA`: **false**: Enable this to force captcha validation
578-
even for External Accounts (i.e. GitHub, OpenID Connect, etc). You must `ENABLE_CAPTCHA` also.
579-
- `CAPTCHA_TYPE`: **image**: \[image, recaptcha, hcaptcha\]
578+
even for External Accounts (i.e. GitHub, OpenID Connect, etc). You also must enable `ENABLE_CAPTCHA`.
579+
- `CAPTCHA_TYPE`: **image**: \[image, recaptcha, hcaptcha, mcaptcha\]
580580
- `RECAPTCHA_SECRET`: **""**: Go to https://www.google.com/recaptcha/admin to get a secret for recaptcha.
581581
- `RECAPTCHA_SITEKEY`: **""**: Go to https://www.google.com/recaptcha/admin to get a sitekey for recaptcha.
582582
- `RECAPTCHA_URL`: **https://www.google.com/recaptcha/**: Set the recaptcha url - allows the use of recaptcha net.
583583
- `HCAPTCHA_SECRET`: **""**: Sign up at https://www.hcaptcha.com/ to get a secret for hcaptcha.
584584
- `HCAPTCHA_SITEKEY`: **""**: Sign up at https://www.hcaptcha.com/ to get a sitekey for hcaptcha.
585+
- `MCAPTCHA_SECRET`: **""**: Go to your mCaptcha instance to get a secret for mCaptcha.
586+
- `MCAPTCHA_SITEKEY`: **""**: Go to your mCaptcha instance to get a sitekey for mCaptcha.
587+
- `MCAPTCHA_URL` **https://demo.mcaptcha.org/**: Set the mCaptcha URL.
585588
- `DEFAULT_KEEP_EMAIL_PRIVATE`: **false**: By default set users to keep their email address private.
586589
- `DEFAULT_ALLOW_CREATE_ORGANIZATION`: **true**: Allow new users to create organizations by default.
587590
- `DEFAULT_USER_IS_RESTRICTED`: **false**: Give new users restricted permissions by default

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.18
55
require (
66
code.gitea.io/gitea-vet v0.2.2-0.20220122151748-48ebc902541b
77
code.gitea.io/sdk/gitea v0.15.1
8+
codeberg.org/gusted/mcaptcha v0.0.0-20220722211632-55c1ffff1222
89
gitea.com/go-chi/binding v0.0.0-20220309004920-114340dabecb
910
gitea.com/go-chi/cache v0.2.0
1011
gitea.com/go-chi/captcha v0.0.0-20211013065431-70641c1a35d5

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ code.gitea.io/sdk/gitea v0.15.1 h1:WJreC7YYuxbn0UDaPuWIe/mtiNKTvLN8MLkaw71yx/M=
6464
code.gitea.io/sdk/gitea v0.15.1/go.mod h1:klY2LVI3s3NChzIk/MzMn7G1FHrfU7qd63iSMVoHRBA=
6565
codeberg.org/Codeberg/captcha v0.0.0-20220520081522-1e8560aeefdc h1:A3gd590p+XKy38/yvugn2O44gFNlydTxUizsAyLvod4=
6666
codeberg.org/Codeberg/captcha v0.0.0-20220520081522-1e8560aeefdc/go.mod h1:8udua5pKNuvkQNeyEeFHVUugRTTTuOnGa/pFQTAeWXk=
67+
codeberg.org/gusted/mcaptcha v0.0.0-20220722211632-55c1ffff1222 h1:PCW4i+gnQ9XxF8V+nBch3KWdGe4MiP3xXUCA/z0jhHk=
68+
codeberg.org/gusted/mcaptcha v0.0.0-20220722211632-55c1ffff1222/go.mod h1:IIAjsijsd8q1isWX8MACefDEgTQslQ4stk2AeeTt3kM=
6769
contrib.go.opencensus.io/exporter/aws v0.0.0-20181029163544-2befc13012d0/go.mod h1:uu1P0UCM/6RbsMrgPa98ll8ZcHM858i/AD06a9aLRCA=
6870
contrib.go.opencensus.io/exporter/ocagent v0.5.0/go.mod h1:ImxhfLRpxoYiSq891pBrLVhN+qmP8BTVvdH2YLs7Gl0=
6971
contrib.go.opencensus.io/exporter/stackdriver v0.12.1/go.mod h1:iwB6wGarfphGGe/e5CWqyUk/cLzKnWsOKPVW3no6OTw=

modules/mcaptcha/mcaptcha.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2022 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package mcaptcha
6+
7+
import (
8+
"context"
9+
"fmt"
10+
11+
"code.gitea.io/gitea/modules/setting"
12+
13+
"codeberg.org/gusted/mcaptcha"
14+
)
15+
16+
func Verify(ctx context.Context, token string) (bool, error) {
17+
valid, err := mcaptcha.Verify(ctx, &mcaptcha.VerifyOpts{
18+
InstanceURL: setting.Service.McaptchaURL,
19+
Sitekey: setting.Service.McaptchaSitekey,
20+
Secret: setting.Service.McaptchaSecret,
21+
Token: token,
22+
})
23+
if err != nil {
24+
return false, fmt.Errorf("wasn't able to verify mCaptcha: %v", err)
25+
}
26+
return valid, nil
27+
}

modules/setting/service.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ var Service = struct {
4747
RecaptchaURL string
4848
HcaptchaSecret string
4949
HcaptchaSitekey string
50+
McaptchaSecret string
51+
McaptchaSitekey string
52+
McaptchaURL string
5053
DefaultKeepEmailPrivate bool
5154
DefaultAllowCreateOrganization bool
5255
DefaultUserIsRestricted bool
@@ -133,6 +136,9 @@ func newService() {
133136
Service.RecaptchaURL = sec.Key("RECAPTCHA_URL").MustString("https://www.google.com/recaptcha/")
134137
Service.HcaptchaSecret = sec.Key("HCAPTCHA_SECRET").MustString("")
135138
Service.HcaptchaSitekey = sec.Key("HCAPTCHA_SITEKEY").MustString("")
139+
Service.McaptchaURL = sec.Key("MCAPTCHA_URL").MustString("https://demo.mcaptcha.org/")
140+
Service.McaptchaSecret = sec.Key("MCAPTCHA_SECRET").MustString("")
141+
Service.McaptchaSitekey = sec.Key("MCAPTCHA_SITEKEY").MustString("")
136142
Service.DefaultKeepEmailPrivate = sec.Key("DEFAULT_KEEP_EMAIL_PRIVATE").MustBool()
137143
Service.DefaultAllowCreateOrganization = sec.Key("DEFAULT_ALLOW_CREATE_ORGANIZATION").MustBool(true)
138144
Service.DefaultUserIsRestricted = sec.Key("DEFAULT_USER_IS_RESTRICTED").MustBool(false)

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-2",
1112
"@primer/octicons": "17.2.0",
1213
"add-asset-webpack-plugin": "2.0.1",
1314
"css-loader": "6.7.1",

routers/web/auth/auth.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"code.gitea.io/gitea/modules/eventsource"
1919
"code.gitea.io/gitea/modules/hcaptcha"
2020
"code.gitea.io/gitea/modules/log"
21+
"code.gitea.io/gitea/modules/mcaptcha"
2122
"code.gitea.io/gitea/modules/password"
2223
"code.gitea.io/gitea/modules/recaptcha"
2324
"code.gitea.io/gitea/modules/session"
@@ -415,6 +416,8 @@ func SignUp(ctx *context.Context) {
415416
ctx.Data["CaptchaType"] = setting.Service.CaptchaType
416417
ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
417418
ctx.Data["HcaptchaSitekey"] = setting.Service.HcaptchaSitekey
419+
ctx.Data["McaptchaSitekey"] = setting.Service.McaptchaSitekey
420+
ctx.Data["McaptchaURL"] = setting.Service.McaptchaURL
418421
ctx.Data["PageIsSignUp"] = true
419422

420423
// Show Disabled Registration message if DisableRegistration or AllowOnlyExternalRegistration options are true
@@ -436,6 +439,8 @@ func SignUpPost(ctx *context.Context) {
436439
ctx.Data["CaptchaType"] = setting.Service.CaptchaType
437440
ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
438441
ctx.Data["HcaptchaSitekey"] = setting.Service.HcaptchaSitekey
442+
ctx.Data["McaptchaSitekey"] = setting.Service.McaptchaSitekey
443+
ctx.Data["McaptchaURL"] = setting.Service.McaptchaURL
439444
ctx.Data["PageIsSignUp"] = true
440445

441446
// Permission denied if DisableRegistration or AllowOnlyExternalRegistration options are true
@@ -459,6 +464,8 @@ func SignUpPost(ctx *context.Context) {
459464
valid, err = recaptcha.Verify(ctx, form.GRecaptchaResponse)
460465
case setting.HCaptcha:
461466
valid, err = hcaptcha.Verify(ctx, form.HcaptchaResponse)
467+
case setting.MCaptcha:
468+
valid, err = mcaptcha.Verify(ctx, form.McaptchaResponse)
462469
default:
463470
ctx.ServerError("Unknown Captcha Type", fmt.Errorf("Unknown Captcha Type: %s", setting.Service.CaptchaType))
464471
return

0 commit comments

Comments
 (0)