valkeystate

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: AGPL-3.0 Imports: 13 Imported by: 0

Documentation

Overview

Package valkeystate keeps the state that must agree across backend processes and is touched on every sign-in in Valkey (or any Redis-compatible server) instead of the database: the failed attempt counters and the single-use nonces. It is optional. Without it the same state lives in the database tables (used_nonces, auth_failures), so a flood of wrong passwords turns into database writes; with it the flood costs the database nothing.

It has the same methods and meaning as the store's, so the backend uses either one. Every operation has a time limit, and every error is returned: the backend fails closed on them (an address counts as locked out, a nonce as spent), exactly as it does when the database fails.

Nothing here is evicted quietly. The server must run with maxmemory-policy noeviction: a counter or a nonce that was evicted would let an attacker try again, or replay a value, so a full server must refuse writes (which fails closed) instead.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client talks to the server. Its methods are safe for concurrent use.

func Open

func Open(ctx context.Context, rawURL string) (*Client, error)

Open connects to the server at rawURL (redis://[:password@]host:port[/db], or rediss:// for TLS) and checks that it answers.

func (*Client) ClearFailures

func (v *Client) ClearFailures(ctx context.Context, key string) error

ClearFailures forgets the failures under key, after a success.

func (*Client) Close

func (v *Client) Close()

Close closes the connections.

func (*Client) CountFailures

func (v *Client) CountFailures(ctx context.Context, key string, since time.Time) (int, error)

CountFailures counts the failures under key that are newer than since.

func (*Client) CountSessions

func (v *Client) CountSessions(ctx context.Context) (int64, error)

CountSessions returns how many sessions are alive.

func (*Client) CreateSession

func (v *Client) CreateSession(ctx context.Context, tokenHash string, userID int64, method string, expires time.Time) error

CreateSession stores a session until expires.

func (*Client) DeleteSession

func (v *Client) DeleteSession(ctx context.Context, tokenHash string) error

DeleteSession ends one session. A session that does not exist is not an error.

func (*Client) DeleteUserSessions

func (v *Client) DeleteUserSessions(ctx context.Context, userID int64) error

DeleteUserSessions ends every session of one user. Only the sessions seen at the start are removed from the index, so one that is created at the same moment stays reachable by the next call.

func (*Client) GetSession

func (v *Client) GetSession(ctx context.Context, tokenHash string) (models.Session, error)

GetSession returns an unexpired session, or store.ErrNotFound.

func (*Client) NonceSeen

func (v *Client) NonceSeen(ctx context.Context, nonce string) (bool, error)

NonceSeen reports whether nonce was used, without claiming it.

func (*Client) Ping

func (v *Client) Ping(ctx context.Context) error

Ping checks that the server answers.

func (*Client) RecordFailure

func (v *Client) RecordFailure(ctx context.Context, key string, at time.Time) error

RecordFailure notes a failed attempt under key.

func (*Client) UseNonce

func (v *Client) UseNonce(ctx context.Context, nonce string, expires time.Time) (bool, error)

UseNonce claims nonce until expires and reports whether this is the first use. SET NX decides atomically, so two processes racing for the same value cannot both win.

Jump to

Keyboard shortcuts

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