Skip to content

Commit 75d079d

Browse files
author
Gusted
committed
Implement backend verification
1 parent 62ea258 commit 75d079d

File tree

9 files changed

+41
-0
lines changed

9 files changed

+41
-0
lines changed

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
@@ -62,6 +62,8 @@ code.gitea.io/gitea-vet v0.2.2-0.20220122151748-48ebc902541b/go.mod h1:zcNbT/aJE
6262
code.gitea.io/sdk/gitea v0.11.3/go.mod h1:z3uwDV/b9Ls47NGukYM9XhnHtqPh/J+t40lsUrR6JDY=
6363
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=
65+
codeberg.org/gusted/mcaptcha v0.0.0-20220722211632-55c1ffff1222 h1:PCW4i+gnQ9XxF8V+nBch3KWdGe4MiP3xXUCA/z0jhHk=
66+
codeberg.org/gusted/mcaptcha v0.0.0-20220722211632-55c1ffff1222/go.mod h1:IIAjsijsd8q1isWX8MACefDEgTQslQ4stk2AeeTt3kM=
6567
contrib.go.opencensus.io/exporter/aws v0.0.0-20181029163544-2befc13012d0/go.mod h1:uu1P0UCM/6RbsMrgPa98ll8ZcHM858i/AD06a9aLRCA=
6668
contrib.go.opencensus.io/exporter/ocagent v0.5.0/go.mod h1:ImxhfLRpxoYiSq891pBrLVhN+qmP8BTVvdH2YLs7Gl0=
6769
contrib.go.opencensus.io/exporter/stackdriver v0.12.1/go.mod h1:iwB6wGarfphGGe/e5CWqyUk/cLzKnWsOKPVW3no6OTw=

modules/mcaptcha/mcaptcha.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package mcaptcha
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"code.gitea.io/gitea/modules/setting"
8+
9+
"codeberg.org/gusted/mcaptcha"
10+
)
11+
12+
func Verify(ctx context.Context, token string) (bool, error) {
13+
valid, err := mcaptcha.Verify(ctx, &mcaptcha.VerifyOpts{
14+
InstanceURL: setting.Service.McaptchaURL,
15+
Sitekey: setting.Service.McaptchaSitekey,
16+
Secret: setting.Service.McaptchaSecret,
17+
Token: token,
18+
})
19+
if err != nil {
20+
return false, fmt.Errorf("wasn't able to verify mCaptcha: %v", err)
21+
}
22+
return valid, nil
23+
}

routers/web/auth/auth.go

Lines changed: 3 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"
@@ -462,6 +463,8 @@ func SignUpPost(ctx *context.Context) {
462463
valid, err = recaptcha.Verify(ctx, form.GRecaptchaResponse)
463464
case setting.HCaptcha:
464465
valid, err = hcaptcha.Verify(ctx, form.HcaptchaResponse)
466+
case setting.MCaptcha:
467+
valid, err = mcaptcha.Verify(ctx, form.McaptchaResponse)
465468
default:
466469
ctx.ServerError("Unknown Captcha Type", fmt.Errorf("Unknown Captcha Type: %s", setting.Service.CaptchaType))
467470
return

routers/web/auth/linkaccount.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"code.gitea.io/gitea/modules/context"
1717
"code.gitea.io/gitea/modules/hcaptcha"
1818
"code.gitea.io/gitea/modules/log"
19+
"code.gitea.io/gitea/modules/mcaptcha"
1920
"code.gitea.io/gitea/modules/recaptcha"
2021
"code.gitea.io/gitea/modules/session"
2122
"code.gitea.io/gitea/modules/setting"
@@ -239,6 +240,8 @@ func LinkAccountPostRegister(ctx *context.Context) {
239240
valid, err = recaptcha.Verify(ctx, form.GRecaptchaResponse)
240241
case setting.HCaptcha:
241242
valid, err = hcaptcha.Verify(ctx, form.HcaptchaResponse)
243+
case setting.MCaptcha:
244+
valid, err = mcaptcha.Verify(ctx, form.McaptchaResponse)
242245
default:
243246
ctx.ServerError("Unknown Captcha Type", fmt.Errorf("Unknown Captcha Type: %s", setting.Service.CaptchaType))
244247
return

routers/web/auth/openid.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,12 @@ func RegisterOpenIDPost(ctx *context.Context) {
401401
return
402402
}
403403
valid, err = hcaptcha.Verify(ctx, form.HcaptchaResponse)
404+
case setting.MCaptcha:
405+
if err := ctx.Req.ParseForm(); err != nil {
406+
ctx.ServerError("", err)
407+
return
408+
}
409+
valid, err = hcaptcha.Verify(ctx, form.HcaptchaResponse)
404410
default:
405411
ctx.ServerError("Unknown Captcha Type", fmt.Errorf("Unknown Captcha Type: %s", setting.Service.CaptchaType))
406412
return

services/forms/user_form.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ type RegisterForm struct {
9595
Retype string
9696
GRecaptchaResponse string `form:"g-recaptcha-response"`
9797
HcaptchaResponse string `form:"h-captcha-response"`
98+
McaptchaResponse string `form:"m-captcha-response"`
9899
}
99100

100101
// Validate validates the fields

services/forms/user_form_auth_openid.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type SignUpOpenIDForm struct {
3131
Email string `binding:"Required;Email;MaxSize(254)"`
3232
GRecaptchaResponse string `form:"g-recaptcha-response"`
3333
HcaptchaResponse string `form:"h-captcha-response"`
34+
McaptchaResponse string `form:"m-captcha-response"`
3435
}
3536

3637
// Validate validates the fields

web_src/js/features/mcaptcha.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export async function initMcaptcha() {
55
}
66

77
const {default: mCaptcha} = await import(/* webpackChunkName: "mcaptcha-vanilla-glue" */'@mcaptcha/vanilla-glue');
8+
mCaptcha.INPUT_NAME = 'm-captcha-response';
89
const siteKey = siteKeyEl.getAttribute('data-sitekey');
910

1011
mCaptcha.default({

0 commit comments

Comments
 (0)