Help us shape the future of Go! Take the 2026 Go Developer Survey

auth

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package auth provides composable authentication primitives for tanGO applications.

Index

Constants

This section is empty.

Variables

View Source
var (
	// DeriveCSRFToken derives a form-safe CSRF token from an arbitrary
	// secret, typically a session token.
	DeriveCSRFToken = security.DeriveCSRFToken
	// VerifyCSRFToken verifies a submitted CSRF token against a secret.
	VerifyCSRFToken = security.VerifyCSRFToken
	// SafeRedirect returns raw when it is safe for allowedPrefix, otherwise
	// fallback.
	SafeRedirect = security.SafeRedirect
	// IsSafeRedirect reports whether raw is safe for allowedPrefix.
	IsSafeRedirect = security.IsSafeRedirect
	// IsHTTPS reports whether the client connected over HTTPS, directly or
	// through a TLS-terminating proxy that sets X-Forwarded-Proto or
	// Forwarded. Use it for a cookie's Secure flag.
	IsHTTPS = security.IsHTTPS
)
View Source
var ErrInvalidSessionModel = errors.New("tango auth: invalid session model")

Functions

func CreateSession

func CreateSession(ctx context.Context, store *db.Store, sessionMeta model.ModelMeta, userID any, duration time.Duration) (string, time.Time, error)

CreateSession creates a session row for userID with a crypto-random, cookie-safe token and the caller-supplied duration.

func CurrentUserID

func CurrentUserID(ctx *tango.Context, store *db.Store, sessionMeta model.ModelMeta, cookieName string) (any, bool, error)

CurrentUserID returns the current request's session-backed user id. ok is false when the cookie is missing, invalid, or expired.

func DeleteSession

func DeleteSession(ctx context.Context, store *db.Store, sessionMeta model.ModelMeta, token string) error

DeleteSession deletes every session row matching token. Missing tokens are a no-op.

func HashPassword

func HashPassword(password string) (string, error)

HashPassword hashes password using bcrypt's default cost.

func RequireLogin

func RequireLogin(store *db.Store, sessionMeta model.ModelMeta, cookieName string, loginPath string, allowedPrefix string, next tango.View) tango.View

RequireLogin wraps next and redirects unauthenticated requests to loginPath, carrying a safe next query parameter validated against allowedPrefix.

func SessionUser

func SessionUser(ctx context.Context, store *db.Store, sessionMeta model.ModelMeta, token string) (any, bool, error)

SessionUser returns the stored UserID for a valid, unexpired session. ok is false for empty, missing, or expired tokens.

func ValidateSessionModel

func ValidateSessionModel(sessionMeta model.ModelMeta) error

ValidateSessionModel validates the fixed field-name convention auth's generic session helpers expect: a primary key, Token string, UserID as a foreign key, and ExpiresAt time.Time.

func VerifyPassword

func VerifyPassword(hash string, password string) bool

VerifyPassword reports whether password matches hash. Invalid hashes simply return false.

Types

This section is empty.

Directories

Path Synopsis
Package jwt provides explicit, stateless HS256 access-token primitives.
Package jwt provides explicit, stateless HS256 access-token primitives.

Jump to

Keyboard shortcuts

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