secretbin

package module
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2025 License: MIT Imports: 24 Imported by: 2

README

SecretBin-Go

This module allows for automatic secret creation in SecretBin. Note however this module currently only support creating AES256-GCM secrets. XChaCha20 is currently not supported.

Usage

package main

import (
    "fmt"
	secretbin "github.com/Nihility-io/SecretBin-Go/v2"
)

func main() {
    // Connect to the SecretBin server
    sb, err := secretbin.New("https://secretbin.example.com")
    if err != nil {
        panic(err)
    }

    // Create a secret with arguments joined into a single message.
    secret := secretbin.Secret{Message: "Hello World"}

    // Append files to the secret
    if err := secret.AddFileAttachment("myfile.pdf"); err != nil {
        panic(err)
    }

    // Submit the secret with the specified options to SecretBin.
    // This will encrypt the secret and return a link to access it.
    link, err := sb.SubmitSecret(secret, secretbin.Options{
        Password:  "abc",
        Expires:   "2w",
        BurnAfter: 1,
    })
    if err != nil {
        panic(err)
    }

    // Print the link to the created secret.
    fmt.Println(link)
}

Documentation

Index

Constants

View Source
const (
	Uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	Lowercase = "abcdefghijklmnopqrstuvwxyz"
	Digits    = "0123456789"
	Symbols   = "~!@#%&*_-+=,.<>?"
)

Variables

View Source
var (
	// List of possible errors returned by the SecretBin API.
	ErrInvalidExpirationTime = &SecretBinError{Name: "InvalidExpirationTime"}
	ErrSecretNotFound        = &SecretBinError{Name: "SecretNotFoundError"}
	ErrSecretAlreadyExists   = &SecretBinError{Name: "SecretAlreadyExistsError"}
	ErrSecretList            = &SecretBinError{Name: "SecretListError"}
	ErrSecretRead            = &SecretBinError{Name: "SecretReadError"}
	ErrSecretCreate          = &SecretBinError{Name: "SecretCreateError"}
	ErrSecretUpdate          = &SecretBinError{Name: "SecretUpdateError"}
	ErrSecretDelete          = &SecretBinError{Name: "SecretDeleteError"}
	ErrSecretParse           = &SecretBinError{Name: "SecretParseError"}
	ErrSecretPolicy          = &SecretBinError{Name: "SecretPolicyError"}
	ErrSecretSizeLimit       = &SecretBinError{Name: "SecretSizeLimitError"}
)
View Source
var (
	ErrInvalidPasswordLength = errors.New("invalid password length; must be greater than 6")
	ErrInvalidCharacterSet   = errors.New(
		"at least one character set (uppercase, lowercase, digits, symbols) must be selected")
)

Functions

func GeneratePassword added in v2.1.0

func GeneratePassword(options PasswordOptions) (string, error)

GeneratePassword generates a secure random password based on the provided options. It ensures that at least one character from each selected set is included. If no character sets are selected or the length is below 6, it returns an error.

Types

type Attachment

type Attachment struct {
	Name        string `json:"name"`        // Filename of the attachment
	ContentType string `json:"contentType"` // MIME type of the attachment
	Data        []byte `json:"data"`        // Binary data of the attachment
}
type Banner struct {
	Type string `json:"type"` // Type of the banner ("info", "warning", "error")
	Text string `json:"text"` // Text content of the banner
}

type Client

type Client interface {
	// Config returns information about the SecretBin server.
	Config() *Config

	// SubmitSecret creates a new secret inside SecretBin and returns the access URL for said secret.
	SubmitSecret(secret Secret, options Options) (string, error)
}

func New

func New(endpoint string) (Client, error)

New creates a new SecretBin client for the given endpoint. It retrieves the API information and configuration from the server to initialize the client.

type Config

type Config struct {
	Name           string             // Name of the SecretBin instance
	Endpoint       string             // Endpoint URL of the SecretBin server
	Version        *semver.Version    // Version of the SecretBin server
	Banner         *Banner            // Optional banner displayed by the server
	Expires        map[string]Expires // Available expiration options for secrets
	DefaultExpires string             // Default expiration option for secrets
}

func (*Config) ExpireOptionsSorted

func (c *Config) ExpireOptionsSorted() []string

ExpiresOptionsSorted returns a slice of expiration option names sorted by their duration.

func (*Config) ExpiresSorted

func (c *Config) ExpiresSorted() iter.Seq2[string, Expires]

ExpiresSorted returns an iterator that yields expiration options sorted by their duration.

type Expires

type Expires struct {
	Count   int    `json:"count"`   // Number of units for this expiration option
	Unit    string `json:"unit"`    // Unit of time for this expiration option (e.g., "hr", "d", "w", "m", "y")
	Seconds int    `json:"seconds"` // Duration in seconds for this expiration option
}

func (Expires) String

func (e Expires) String() string

String returns a human-readable representation of the expiration option.

type Options

type Options struct {
	// Password is used as an additional security step along the the encryption key (optional)
	Password string

	// Expires is the expiration time for the secret.
	//
	// Use [Client.Config().Expires] to get the available options.
	Expires string

	// BurnAfter indicates after how many reads the secret should be deleted.
	//
	// 0 means no burn after reading.
	BurnAfter uint
}

type PasswordOptions added in v2.1.0

type PasswordOptions struct {
	Uppercase bool
	Lowercase bool
	Digits    bool
	Symbols   bool
	Length    int
}

PasswordOptions defines the options for generating a password.

type Secret

type Secret struct {
	Message     string        `json:"message"`     // Text content of the secret
	Attachments []*Attachment `json:"attachments"` // Optional file attachments of the secret
}

func (*Secret) AddAttachment

func (s *Secret) AddAttachment(name string, contentType string, data []byte)

AddAttachment adds an attachment to the secret content. If the content type is not provided, it will be guessed based on the file extension.

func (*Secret) AddFileAttachment

func (s *Secret) AddFileAttachment(path string) error

AddFileAttachment reads a file from the given path and adds it as an attachment to the secret content. The content type is guessed based on the file extension.

type SecretBinError

type SecretBinError struct {
	Name    string `json:"name"`
	Message string `json:"message"`
	Status  int    `json:"status"`
}

func (*SecretBinError) Error

func (e *SecretBinError) Error() string

Error marks the SecretBinError as an error type.

func (*SecretBinError) Is

func (e *SecretBinError) Is(target error) bool

Is checks if the error matches the target error.

Jump to

Keyboard shortcuts

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