auth

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2026 License: MIT Imports: 13 Imported by: 0 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 auth provides Postgres-backed adapters for the msgr-authkit bot-first login-link flow, an admin Telegram-ID allowlist, session-cookie middleware, and a CSRF token helper for the web admin UI.

Index

Constants

View Source
const SessionCookieName = "snagbox_session"

SessionCookieName is the name of the cookie that carries the web session token for the snagbox admin UI.

Variables

View Source
var ErrCSRFInvalid = errors.New("auth: invalid csrf token")

ErrCSRFInvalid indicates a submitted CSRF token did not match the expected value for the session.

Functions

func CSRFTokenFor

func CSRFTokenFor(serverSecret []byte, sessionToken string) string

CSRFTokenFor derives a deterministic CSRF token for a session token using an HMAC keyed by serverSecret.

func ClearSessionCookie

func ClearSessionCookie(w http.ResponseWriter, secure bool)

ClearSessionCookie expires the session cookie on the client.

func RequireSession

func RequireSession(validator SessionValidator, loginPath string) func(http.Handler) http.Handler

RequireSession returns middleware that enforces a valid web session on the wrapped handler, redirecting to loginPath when the session is missing or invalid and storing the resolved session in the request context otherwise.

func SessionFromContext

func SessionFromContext(ctx context.Context) (authkit.WebSession, bool)

SessionFromContext returns the web session stored by RequireSession, if any.

func SetSessionCookie

func SetSessionCookie(w http.ResponseWriter, session authkit.WebSession, secure bool)

SetSessionCookie writes the session token to the response as an HttpOnly cookie that expires with the session.

func VerifyCSRF

func VerifyCSRF(serverSecret []byte, sessionToken, submitted string) bool

VerifyCSRF reports whether submitted matches the CSRF token expected for the given session token, using a constant-time comparison.

Types

type Allowlist

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

Allowlist checks whether a Telegram user id is permitted to administer snagbox (ADMIN_TG_IDS).

func NewAllowlist

func NewAllowlist(ids []int64) Allowlist

NewAllowlist builds an Allowlist from a list of Telegram user ids.

func (Allowlist) Allowed

func (a Allowlist) Allowed(tgID int64) bool

Allowed reports whether tgID is an admin.

type PGIntentStore

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

PGIntentStore is a Postgres-backed implementation of authkit.IntentStore.

func NewPGIntentStore

func NewPGIntentStore(db *sql.DB) *PGIntentStore

NewPGIntentStore builds a PGIntentStore over the given database handle.

func (*PGIntentStore) Create

func (s *PGIntentStore) Create(ctx context.Context, intent authkit.AuthIntent) error

Create inserts a new auth intent.

func (*PGIntentStore) DeleteExpired

func (s *PGIntentStore) DeleteExpired(ctx context.Context, now time.Time) error

DeleteExpired removes intents whose expiration passed before now.

func (*PGIntentStore) FindByCode

func (s *PGIntentStore) FindByCode(ctx context.Context, messenger authkit.Messenger, code string) (authkit.AuthIntent, error)

FindByCode returns the intent identified by the messenger+code pair.

func (*PGIntentStore) RecordRedemption

func (s *PGIntentStore) RecordRedemption(ctx context.Context, intentID string, redeemedAt time.Time) error

RecordRedemption applies the redemption state transition atomically, using a compare-and-swap UPDATE to remain safe under concurrent redemptions.

type PGLinkStore

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

PGLinkStore is a Postgres-backed implementation of authkit.LinkStore.

func NewPGLinkStore

func NewPGLinkStore(db *sql.DB) *PGLinkStore

NewPGLinkStore builds a PGLinkStore over the given database handle.

func (*PGLinkStore) FindByIdentity

func (s *PGLinkStore) FindByIdentity(ctx context.Context, identity authkit.Identity) (authkit.AccountLink, error)

FindByIdentity resolves the internal account link for an external identity.

func (*PGLinkStore) Upsert

func (s *PGLinkStore) Upsert(ctx context.Context, link authkit.AccountLink) error

Upsert creates or refreshes an identity->app-user mapping.

type PGSessionIssuer

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

PGSessionIssuer is a Postgres-backed implementation of authkit.SessionIssuer. Session tokens are stored only as SHA-256 hashes, never in plaintext.

func NewPGSessionIssuer

func NewPGSessionIssuer(db *sql.DB, ttl time.Duration) *PGSessionIssuer

NewPGSessionIssuer builds a PGSessionIssuer over the given database handle with the provided session time-to-live.

func (*PGSessionIssuer) Issue

func (s *PGSessionIssuer) Issue(ctx context.Context, appUserID string) (authkit.WebSession, error)

Issue creates a new web session for appUserID, persisting only the token hash, and returns the session with its plaintext token.

func (*PGSessionIssuer) Revoke

func (s *PGSessionIssuer) Revoke(ctx context.Context, token string) error

Revoke deletes the session identified by token.

func (*PGSessionIssuer) Validate

func (s *PGSessionIssuer) Validate(ctx context.Context, token string) (authkit.WebSession, error)

Validate resolves a session by its token, returning authkit.ErrSessionNotFound when unknown and authkit.ErrSessionExpired when past its expiration.

type SessionValidator

type SessionValidator interface {
	Validate(ctx context.Context, token string) (authkit.WebSession, error)
}

SessionValidator validates a session token and returns the resolved web session. PGSessionIssuer satisfies this interface.

Jump to

Keyboard shortcuts

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