auth

package
v0.16.0 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package auth implements passwordless, allowlist-closed authentication for team mode: magic-link issuance/verification and server-side cookie sessions. It is a library — no HTTP types cross its boundary; the gateway adapts it to endpoints.

Index

Constants

View Source
const (
	DefaultTokenTTL   = 15 * time.Minute
	DefaultSessionTTL = 30 * 24 * time.Hour
)

Defaults for token and session lifetimes.

Variables

View Source
var ErrInvalidSession = errors.New("invalid or expired session")

ErrInvalidSession is returned for an unknown or expired session.

View Source
var ErrInvalidToken = errors.New("invalid or expired token")

ErrInvalidToken is returned for any magic-link failure (unknown, expired, or already consumed) — deliberately indistinguishable.

Functions

This section is empty.

Types

type Config

type Config struct {
	// BaseURL is the externally visible origin; magic links are built as
	// BaseURL + "/api/auth/verify?token=...".
	BaseURL string
	// AppName is shown in the login email.
	AppName string
	// TokenTTL is the magic-link lifetime (default 15m).
	TokenTTL time.Duration
	// SessionTTL is the cookie session lifetime (default 30d).
	SessionTTL time.Duration
	// Logger defaults to slog.Default().
	Logger *slog.Logger
}

Config configures the auth service.

type Principal

type Principal struct {
	UserID     uuid.UUID
	Username   string
	Email      string
	Superadmin bool
	Disabled   bool
}

Principal is the authenticated identity resolved from a session.

func (*Principal) Actor

func (p *Principal) Actor() team.Actor

Actor converts a Principal into a team.Actor for service calls.

type Service

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

Service performs authentication over the team store.

func NewService

func NewService(st *store.Store, teamSvc *team.Service, mailer mail.Mailer, cfg Config) (*Service, error)

NewService creates the auth service.

func (*Service) Authenticate

func (s *Service) Authenticate(ctx context.Context, rawSessionToken string) (*Principal, error)

Authenticate resolves a session cookie token to a Principal, sliding the expiry when it is stale. Returns ErrInvalidSession for unknown/expired sessions or disabled users.

func (*Service) Logout

func (s *Service) Logout(ctx context.Context, rawSessionToken string) error

Logout revokes the session identified by its raw cookie token. Unknown tokens are a no-op (idempotent logout).

func (s *Service) RequestMagicLink(ctx context.Context, email, clientIP string) error

RequestMagicLink issues and emails a login link when the email is allowed.

The response is uniform by contract: this returns nil for both allowed and non-allowed emails (doing nothing in the latter case), so callers cannot enumerate the allowlist. Only malformed input yields an error. Internal failures (token store, mail delivery) are logged, not surfaced — the operator sees them, the requester does not.

func (s *Service) VerifyMagicLink(ctx context.Context, rawToken string) (sessionToken string, u *ent.User, err error)

VerifyMagicLink consumes a token and returns a new session's raw token (to be set as the cookie) plus the resolved user. Any failure returns ErrInvalidToken. A disabled user is rejected.

Jump to

Keyboard shortcuts

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