An open API service indexing awesome lists of open source software.

https://github.com/kelo221/goptcha

Simple implementation of a captcha written in Go. No external dependencies.
https://github.com/kelo221/goptcha

captcha captcha-generator go golang

Last synced: 4 months ago
JSON representation

Simple implementation of a captcha written in Go. No external dependencies.

Awesome Lists containing this project

README

          

# Goptcha

### A proof of concept captcha solution done in Golang.


## Example using Fiber

```go
package main

import (
"bytes"
"fmt"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/session"
"github.com/kelo221/goptcha"
"image/png"
"log"
"net/http"
"time"
)

var capchaStore *session.Store

func main() {

app := fiber.New()

capchaStore = session.New(session.Config{
Expiration: time.Minute * 1,
CookieSecure: true,
CookieHTTPOnly: true,
})

Goptcha.Configure(&Goptcha.Config{
ImageSizeMultiplier: 4,
CharSet: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
CharacterCount: 8,
Opacity: 100,
})

app.Get("/captcha", getCaptcha)
app.Post("/checker", restricted)

err := app.Listen(":3000")
if err != nil {
return
}

}

func getCaptcha(c *fiber.Ctx) error {

captchaString, img := Goptcha.GenerateCaptcha()

fmt.Println(captchaString)

sess, err := capchaStore.Get(c)
if err != nil {
panic(err)
}

sess.Set("captcha", captchaString)
if err := sess.Save(); err != nil {
panic(err)
}

// Create a new buffer and encode an image to it
buf := new(bytes.Buffer)
if err := png.Encode(buf, img); err != nil {
log.Println(err)
return c.SendStatus(fiber.StatusInternalServerError)
}

// Set the content type header to image/jpeg
c.Set(fiber.HeaderContentType, "image/png")

// Write the image bytes to the response body
if _, err := c.Write(buf.Bytes()); err != nil {
log.Println(err)
return c.SendStatus(fiber.StatusInternalServerError)
}

return nil
}

func restricted(c *fiber.Ctx) error {

var data map[string]string

if err := c.BodyParser(&data); err != nil {
println("parsing error")
return err
}

if data["captcha"] == "" {
c.Status(400)
return c.Status(400).JSON(fiber.Map{
"message": "Missing captcha!",
})
}

sess, err := capchaStore.Get(c)
if err != nil {
log.Println(err)
}

captcha := sess.Get("captcha")

log.Println(captcha, data["captcha"])

if captcha != data["captcha"] {
return c.Status(http.StatusBadRequest).JSON(fiber.Map{"error": "Incorrect captcha!"})
}

return c.Status(http.StatusOK).JSON(fiber.Map{"Success": "Correct captcha!"})
}

```
Here `localhost:3000/captcha` returns an randomly generated image, which contains text that is passed to the `localhost:3000/checker` endpoint for verification.

Execution time for a single image is about 2 milliseconds.

![image](https://user-images.githubusercontent.com/61495413/227745559-e9dde796-8d7a-4839-85dd-fdc091773239.png)

*Sample captcha*