Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
B
backend
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
hizla
waitlist
backend
Commits
2303acfd
Commit
2303acfd
authored
10 months ago
by
Levatax
Browse files
Options
Downloads
Patches
Plain Diff
feat: protect registration using hcaptcha
parent
d7d6db1b
Branches
Branches containing commit
Tags
Tags containing commit
1 merge request
!3
Upgrade to Fiber v3 and implement hCaptcha protection
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
.env.example
+3
-1
3 additions, 1 deletion
.env.example
app.go
+2
-4
2 additions, 4 deletions
app.go
conf.go
+4
-0
4 additions, 0 deletions
conf.go
register.go
+34
-1
34 additions, 1 deletion
register.go
with
43 additions
and
6 deletions
.env.example
+
3
−
1
View file @
2303acfd
...
...
@@ -2,3 +2,5 @@ ALLOWED_ORIGINS=https://hizla.io
DB=db
VERBOSE=1
LISTEN_ADDR=127.0.0.1:3000
HCAPTCHA_SITE_KEY=unset
HCAPTCHA_SECRET=unset
\ No newline at end of file
This diff is collapsed.
Click to expand it.
app.go
+
2
−
4
View file @
2303acfd
...
...
@@ -36,10 +36,8 @@ func serve(sig chan os.Signal, db *leveldb.DB) error {
// /register
routeRegister
(
app
,
db
)
// Graceful shutdown
app
.
Use
(
func
(
c
fiber
.
Ctx
)
error
{
return
c
.
Next
()
})
// /hcaptcha-site-key
routeHCaptchaSiteKey
(
app
)
// graceful shutdown
go
func
()
{
...
...
This diff is collapsed.
Click to expand it.
conf.go
+
4
−
0
View file @
2303acfd
...
...
@@ -9,6 +9,8 @@ const (
dbPath
uint8
=
iota
listenAddr
allowedOrigins
hCaptchaSecret
hCaptchaSiteKey
verboseLogging
confLen
)
...
...
@@ -18,6 +20,8 @@ var confEnv = [confLen][2]string{
{
"DB"
,
"db"
},
{
"LISTEN_ADDR"
,
"127.0.0.1:3000"
},
{
"ALLOWED_ORIGINS"
,
"https://hizla.io"
},
{
"HCAPTCHA_SECRET"
,
"unset"
},
{
"HCAPTCHA_SITE_KEY"
,
"unset"
},
{
"VERBOSE"
,
"1"
},
}
...
...
This diff is collapsed.
Click to expand it.
register.go
+
34
−
1
View file @
2303acfd
...
...
@@ -4,6 +4,7 @@ import (
"log"
"regexp"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/contrib/hcaptcha"
"github.com/syndtr/goleveldb/leveldb"
)
...
...
@@ -13,9 +14,27 @@ type registration struct {
Email
string
`json:"email"`
}
// Middleware to conditionally apply hCaptcha
func
conditionalCaptcha
(
captcha
fiber
.
Handler
)
fiber
.
Handler
{
return
func
(
c
fiber
.
Ctx
)
error
{
if
conf
[
hCaptchaSecret
]
==
"unset"
{
if
verbose
{
log
.
Printf
(
"Captcha bypassed for %q"
,
c
.
IP
())
}
return
c
.
Next
()
}
return
captcha
(
c
)
}
}
// Waitlist registration route
func
routeRegister
(
app
*
fiber
.
App
,
db
*
leveldb
.
DB
)
{
app
.
Post
(
"/register"
,
func
(
c
fiber
.
Ctx
)
error
{
captcha
:=
hcaptcha
.
New
(
hcaptcha
.
Config
{
SecretKey
:
conf
[
hCaptchaSecret
],
})
app
.
Post
(
"/register"
,
conditionalCaptcha
(
captcha
),
func
(
c
fiber
.
Ctx
)
error
{
req
:=
new
(
registration
)
// Parse and validate the request
...
...
@@ -67,3 +86,17 @@ func routeRegister(app *fiber.App, db *leveldb.DB) {
})
})
}
// Route to expose hCaptcha site key
func
routeHCaptchaSiteKey
(
app
*
fiber
.
App
)
{
app
.
Get
(
"/hcaptcha-site-key"
,
func
(
c
fiber
.
Ctx
)
error
{
if
conf
[
hCaptchaSiteKey
]
==
"unset"
{
return
c
.
Status
(
fiber
.
StatusInternalServerError
)
.
JSON
(
fiber
.
Map
{
"message"
:
"hCaptcha site key not configured"
,
})
}
return
c
.
JSON
(
fiber
.
Map
{
"hcaptcha_site_key"
:
conf
[
hCaptchaSiteKey
],
})
})
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment