turnstile

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2024 License: MIT Imports: 8 Imported by: 1

README

The Cloudflare Turnstile Go Server Side Client

Turnstile is a service that allows you to protect your web applications from abuse. It is a Cloudflare service that is currently in private beta. You can read more about it here.

Installation

go get github.com/9ssi7/turnstile

Documentation

Go Reference

Usage

package main

import (
    "context"
    "github.com/9ssi7/turnstile"
)

func main() {
    ctx := context.Background()
    srv := turnstile.New(turnstile.Config{
        Secret: "your-secret",
    })
    ok, err := srv.Verify(ctx, "your-token", "your-ip")
    if err != nil {
        panic(err)
    }
    if !ok {
        panic("token is not valid")
    }
    println("token is valid")
}

Contributing

Contributions are always welcome!

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Secret is the secret key used to verify the token.
	// This is required.
	Secret string

	// BackupSecret is the backup secret key used to verify the token.
	// This is optional.
	BackupSecret string

	// Timeout is the timeout for the service.
	// This is optional.
	// Default: 10 seconds
	Timeout time.Duration
}

Config is the configuration for the service.

type Service

type Service interface {
	// Verify is used to verify the token.
	// It returns true if the token is valid.
	// It returns false if the token is invalid.
	// It returns an error if there was an error verifying the token.
	Verify(ctx context.Context, token string, ip string) (bool, error)

	// VerifyIdempotent is used to verify the token.
	// The key parameter is used to ensure idempotency.
	// You may use the RandomUUID method to generate a random UUID.
	// It returns true if the token is valid.
	// It returns false if the token is invalid.
	// It returns an error if there was an error verifying the token.
	VerifyIdempotent(ctx context.Context, token string, ip string, key string) (bool, error)

	// VerifyBackup is used to verify the token.
	// It returns true if the token is valid.
	// It returns false if the token is invalid.
	// It returns an error if there was an error verifying the token.
	VerifyBackup(ctx context.Context, token string, ip string) (bool, error)

	// VerifyBackupIdempotent is used to verify the token.
	// The key parameter is used to ensure idempotency.
	// You may use the RandomUUID method to generate a random UUID.
	// It returns true if the token is valid.
	// It returns false if the token is invalid.
	// It returns an error if there was an error verifying the token.
	VerifyBackupIdempotent(ctx context.Context, token string, ip string, key string) (bool, error)

	// RandomUUID is used to generate a random UUID.
	// It returns a random UUID.
	RandomUUID() string
}

Service is the interface for the service. It is used to verify the token. It is also used to generate a random UUID.

func New

func New(config Config) Service

New is used to create a new service. It returns a new service.

Jump to

Keyboard shortcuts

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