Go Integration
Zero-dependency Go integration patterns for token verification. Stdlib only, no external packages.
Copy these patterns into your project to integrate gkCAPTCHA server-side verification.
Installation
terminal
go get github.com/gatekeepersa/gkcaptcha-goZero external dependencies — stdlib only.
Quick Start
main.go
import gkcaptcha "github.com/gatekeepersa/gkcaptcha-go"
client, err := gkcaptcha.NewClient(gkcaptcha.Config{
SecretKey: os.Getenv("GKCAPTCHA_SECRET_KEY"),
SiteKey: os.Getenv("GKCAPTCHA_SITE_KEY"),
})
if err != nil {
log.Fatal(err)
}
result, err := client.VerifyToken(ctx, captchaToken, nil)
if err != nil || !result.Success {
http.Error(w, "CAPTCHA verification failed", http.StatusForbidden)
return
}Configuration
config.go
client, err := gkcaptcha.NewClient(gkcaptcha.Config{
SecretKey: os.Getenv("GKCAPTCHA_SECRET_KEY"), // required
SiteKey: os.Getenv("GKCAPTCHA_SITE_KEY"), // required
APIURL: "https://gkcaptcha.gatekeeper.sa", // default
Timeout: 5 * time.Second, // default
MaxRetries: 1, // default
RetryDelay: 1 * time.Second, // default
FailClosed: false, // default: fail-open
})Gin Middleware
The Go SDK has zero framework dependencies. Copy-paste this Gin middleware into your application:
middleware.go
// Gin middleware example (copy-paste, not included in package)
func GkCaptchaMiddleware(client *gkcaptcha.GkCaptchaClient) gin.HandlerFunc {
return func(c *gin.Context) {
token := c.GetHeader("X-Captcha-Token")
if token == "" {
token = c.PostForm("captchaToken")
}
if token == "" {
c.AbortWithStatusJSON(403, gin.H{"success": false, "error": "CAPTCHA token required"})
return
}
result, err := client.VerifyToken(c.Request.Context(), token, &gkcaptcha.VerifyOptions{
ClientIP: c.ClientIP(),
UserAgent: c.Request.UserAgent(),
})
if err != nil || !result.Success {
c.AbortWithStatusJSON(403, gin.H{"success": false, "error": "CAPTCHA verification failed"})
return
}
c.Next()
}
}Error Handling
By default (fail-open), if the gkCAPTCHA API is unreachable after retries, VerifyToken returns { Success: true, FailOpen: true } with a nil error. Set FailClosed: true to return an error instead.
افتراضيًا (فتح عند الفشل)، إذا كانت واجهة gkCAPTCHA غير متاحة بعد المحاولات، يُرجع التحقق نجاحًا مع علامة FailOpen. لتغيير هذا السلوك استخدم FailClosed: true.
handler.go
result, err := client.VerifyToken(ctx, token, nil)
if err != nil {
// Network/config error — log and handle
var gkErr *gkcaptcha.GkCaptchaError
if errors.As(err, &gkErr) {
log.Printf("gkcaptcha error [%s]: %v", gkErr.Code, err)
}
return
}
if !result.Success {
// Verification failed — reject the request
log.Printf("captcha failed: %s (score: %.2f)", result.ReasonCode, result.Score)
http.Error(w, "CAPTCHA required", http.StatusForbidden)
return
}
// result.Success == true — proceedEnvironment Variables
| Variable | Required | Description |
|---|---|---|
| GKCAPTCHA_SECRET_KEY | Yes | Secret key from gatekeeper.sa dashboard |
| GKCAPTCHA_SITE_KEY | Yes | Site key from gatekeeper.sa dashboard |
| GKCAPTCHA_API_URL | No | Override API URL (default: https://gkcaptcha.gatekeeper.sa) |