ott

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2026 License: MIT Imports: 7 Imported by: 0

README

GoModularAuth One-Time Token Plugin (gomodularauth-one-time-token)

Go Reference

The One-Time Token Plugin generates, consumes, and revokes cryptographically secure, single-use tokens for password resets, email verification, and magic links.

Installation

go get github.com/BladiCreator/go-modular-auth/plugins/one-time-token

Quick Start

package main

import (
	"context"
	"time"

	auth "github.com/BladiCreator/go-modular-auth"
	adaptermem "github.com/BladiCreator/go-modular-auth/adapters/memory"
	ott "github.com/BladiCreator/go-modular-auth/plugins/one-time-token"
)

func main() {
	ctx := context.Background()

	engine, _ := auth.New(auth.Config{
		Adapter: adaptermem.NewAdapter(),
		Plugins: []auth.PluginModule{
			ott.New(ott.WithTTL(15 * time.Minute)),
		},
	})

	tokens, _ := auth.Get[ott.OneTimeToken](engine)

	// Generate single-use token
	token, _ := tokens.Generate(ctx, "usr_alice")

	// Consume token atomically
	vt, _ := tokens.Consume(ctx, token)
	_ = vt
}

Detailed guide: doc/plugins/one-time-token.md.

Documentation

Overview

Package ott provides one-time token authentication capabilities (reserved module).

Index

Constants

View Source
const DefaultPurpose verification.Purpose = "ott:token"

DefaultPurpose is the default verification purpose for one-time tokens.

View Source
const PluginID = "one_time_token"

PluginID is the unique identifier for this plugin module.

Variables

View Source
var (
	ErrInvalidSubject = errors.New("ott: subject is required")
	ErrInvalidToken   = errors.New("ott: invalid or expired token")
	ErrInvalidTTL     = errors.New("ott: ttl must be positive")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	DefaultTTL time.Duration
	Purpose    verification.Purpose
}

Config defines the configuration for the one-time token plugin.

type GenerateOption

type GenerateOption func(*GenerateOptions)

GenerateOption mutates GenerateOptions.

func WithCustomPurpose

func WithCustomPurpose(p verification.Purpose) GenerateOption

WithCustomPurpose overrides the token purpose for a single generation.

func WithCustomTTL

func WithCustomTTL(ttl time.Duration) GenerateOption

WithCustomTTL overrides the token TTL for a single generation.

type GenerateOptions

type GenerateOptions struct {
	TTL     time.Duration
	Purpose verification.Purpose
}

GenerateOptions contains per-token options.

type Module

type Module struct {
	// contains filtered or unexported fields
}

Module implements auth.PluginModule for One-Time Token authentication.

func New

func New(opts ...Option) *Module

New constructs an installable One-Time Token plugin module.

func (*Module) ID

func (m *Module) ID() string

ID returns the module's unique identifier.

func (*Module) Init

func (m *Module) Init(h *auth.Host) error

Init initializes the plugin module on the host and registers its capability.

type OneTimeToken

type OneTimeToken interface {
	// Generate creates a secure, single-use token bound to subject.
	Generate(ctx context.Context, subject string, opts ...GenerateOption) (string, error)

	// Consume atomically consumes the token and returns the underlying verification token.
	Consume(ctx context.Context, token string, opts ...GenerateOption) (*verification.Token, error)

	// Revoke invalidates all active tokens for the given subject.
	Revoke(ctx context.Context, subject string, opts ...GenerateOption) error
}

OneTimeToken defines the public capability exposed by this plugin.

type Option

type Option func(*Config)

Option modifies Config.

func WithPurpose

func WithPurpose(p verification.Purpose) Option

WithPurpose sets the default verification purpose for tokens.

func WithTTL

func WithTTL(ttl time.Duration) Option

WithTTL sets the default TTL for issued tokens.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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