Documentation
¶
Overview ¶
Package auth provides composable authentication primitives for tanGO applications.
Index ¶
- Variables
- func CreateSession(ctx context.Context, store *db.Store, sessionMeta model.ModelMeta, userID any, ...) (string, time.Time, error)
- func CurrentUserID(ctx *tango.Context, store *db.Store, sessionMeta model.ModelMeta, ...) (any, bool, error)
- func DeleteSession(ctx context.Context, store *db.Store, sessionMeta model.ModelMeta, ...) error
- func HashPassword(password string) (string, error)
- func RequireLogin(store *db.Store, sessionMeta model.ModelMeta, cookieName string, ...) tango.View
- func SessionUser(ctx context.Context, store *db.Store, sessionMeta model.ModelMeta, ...) (any, bool, error)
- func ValidateSessionModel(sessionMeta model.ModelMeta) error
- func VerifyPassword(hash string, password string) bool
Constants ¶
This section is empty.
Variables ¶
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 )
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 ¶
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 ¶
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 ¶
VerifyPassword reports whether password matches hash. Invalid hashes simply return false.
Types ¶
This section is empty.