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
- Variables
- func CSRFTokenFor(serverSecret []byte, sessionToken string) string
- func ClearSessionCookie(w http.ResponseWriter, secure bool)
- func RequireSession(validator SessionValidator, loginPath string) func(http.Handler) http.Handler
- func SessionFromContext(ctx context.Context) (authkit.WebSession, bool)
- func SetSessionCookie(w http.ResponseWriter, session authkit.WebSession, secure bool)
- func VerifyCSRF(serverSecret []byte, sessionToken, submitted string) bool
- type Allowlist
- type PGIntentStore
- func (s *PGIntentStore) Create(ctx context.Context, intent authkit.AuthIntent) error
- func (s *PGIntentStore) DeleteExpired(ctx context.Context, now time.Time) error
- func (s *PGIntentStore) FindByCode(ctx context.Context, messenger authkit.Messenger, code string) (authkit.AuthIntent, error)
- func (s *PGIntentStore) RecordRedemption(ctx context.Context, intentID string, redeemedAt time.Time) error
- type PGLinkStore
- type PGSessionIssuer
- type SessionValidator
Constants ¶
const SessionCookieName = "snagbox_session"
SessionCookieName is the name of the cookie that carries the web session token for the snagbox admin UI.
Variables ¶
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 ¶
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 ¶
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 ¶
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 ¶
NewAllowlist builds an Allowlist from a list of Telegram user ids.
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 ¶
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.