Goptcha

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 25, 2023 License: MPL-2.0 Imports: 11 Imported by: 0

README

Goptcha

A proof of concept captcha solution done in Golang.

Example using Fiber

package main

import (
	"github.com/kelo221/goptcha"
	"bytes"
	"fmt"
	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/fiber/v2/middleware/session"
	"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.

image

Sample captcha

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Configure

func Configure(config *Config)

Configure modifies default captcha generation settings. (OPTIONAL)

func GenerateCaptcha

func GenerateCaptcha() (string, *image.Gray)

func SaveImage

func SaveImage(img image.Gray)

Types

type Config

type Config struct {
	CharacterCount      int
	ImageSizeMultiplier int

	CharSet       string
	Opacity       uint8
	NoiseModifier uint8
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL