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 ¶
const ( // PluginID matches Better Auth's plugin id. PluginID = "one-time-token" // HeaderName is the response header used by SetOTTHeaderOnNewSession. HeaderName = "set-ott" )
const ( // DefaultExpiresIn matches Better Auth's default of three minutes. DefaultExpiresIn = 3 * time.Minute )
Variables ¶
This section is empty.
Functions ¶
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 ¶
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 ¶
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" )