onetimetoken

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: MIT Imports: 14 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package onetimetoken provides a Better Auth-shaped one-time-token plugin.

A signed-in user asks for a short-lived token with GET /one-time-token/generate. Whoever holds the token can trade it once with POST /one-time-token/verify for the user's session. A common use is handing a session to an app on another domain or subdomain that cannot see the session cookie.

Index

Constants

View Source
const (
	// PluginID matches Better Auth's plugin id.
	PluginID = "one-time-token"
	// HeaderName is the response header used by SetOTTHeaderOnNewSession.
	HeaderName = "set-ott"
)
View Source
const (
	// DefaultExpiresIn matches Better Auth's default of three minutes.
	DefaultExpiresIn = 3 * time.Minute
)

Variables

This section is empty.

Functions

func New

func New(config Config) (betterauth.Plugin, error)

New returns the common descriptor-only integration.

Types

type Config

type Config struct {
	// ExpiresIn is how long a token stays valid. Zero means
	// DefaultExpiresIn. Better Auth takes minutes; Go takes a Duration.
	ExpiresIn time.Duration
	// DisableClientRequest rejects GET /one-time-token/generate. Server code
	// can still create tokens with Manager.Generate.
	DisableClientRequest bool
	// GenerateToken replaces the default 32-character random token.
	GenerateToken TokenGenerator
	// DisableSetSessionCookie stops verify from setting the session cookie.
	DisableSetSessionCookie bool
	// StoreToken selects how tokens are stored. Zero means
	// TokenStoragePlain.
	StoreToken TokenStorage
	// HashToken is required with TokenStorageCustom and not allowed
	// otherwise.
	HashToken TokenHasher
	// SetOTTHeaderOnNewSession adds a fresh token in a "set-ott" response
	// header whenever a response sets a new session cookie.
	SetOTTHeaderOnNewSession bool
	// Clock is used only by Manager.Generate. HTTP endpoints use the
	// server's clock. Nil means the system clock.
	Clock betterauth.Clock
}

Config is immutable after construction. Field names follow Better Auth's OneTimeTokenOptions.

type Manager

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

Manager exposes the plugin descriptor and the server-only Generate API.

func NewManager

func NewManager(config Config) (*Manager, error)

NewManager returns an integration with the server-only Generate API.

func (*Manager) Generate

func (manager *Manager) Generate(
	ctx context.Context,
	database betterauth.DatabaseAdapter,
	session betterauth.SessionResult,
	sessionToken string,
) (string, error)

Generate creates a token for an active session from server code. It is the Go form of calling auth.api.generateOneTimeToken on the server, and it works when DisableClientRequest is set. session usually comes from Server.ResolveSession, and sessionToken is the raw value of the same request's session cookie. database must be the adapter the server uses.

func (*Manager) Plugin

func (manager *Manager) Plugin() betterauth.Plugin

Plugin returns the immutable server plugin descriptor.

type TokenGenerator

type TokenGenerator func(context.Context, betterauth.SessionResult) (string, error)

TokenGenerator returns a custom token for a session. It must return a value with enough randomness to resist guessing; the token is also used to derive the key that protects the stored session reference.

type TokenHasher

type TokenHasher func(context.Context, string) (string, error)

TokenHasher turns a token into its stored form for TokenStorageCustom. It must be deterministic.

type TokenStorage

type TokenStorage string

TokenStorage selects how a token is kept in the verification table. It never changes the token that is sent to the client.

const (
	// TokenStoragePlain stores the token as is. It is the default, as in
	// Better Auth. Prefer TokenStorageHashed in production.
	TokenStoragePlain TokenStorage = "plain"
	// TokenStorageHashed stores the SHA-256 hash of the token, encoded the
	// same way as Better Auth's "hashed" mode.
	TokenStorageHashed TokenStorage = "hashed"
	// TokenStorageCustom stores the result of Config.HashToken.
	TokenStorageCustom TokenStorage = "custom-hasher"
)

Jump to

Keyboard shortcuts

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