auth

package
v0.0.0-...-963cf5d Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2026 License: GPL-3.0 Imports: 21 Imported by: 0

Documentation

Overview

Package auth provides passwordless visitor accounts for a site: a user proves an email address by clicking a magic link and then carries a signed session cookie. Users are stored as SML below the site's data directory; there is no database. The package knows nothing about pages or features — internal/server wires it to HTTP (see CWUP/tasks/feature_requests.md).

Index

Constants

View Source
const (
	// CookieName is the session cookie.
	CookieName = "fcms_session"
	// SessionTTL is how long a login lasts.
	SessionTTL = 30 * 24 * time.Hour
	// TokenTTL is how long a magic link is valid.
	TokenTTL = 15 * time.Minute
	// MaxEmailLen is the longest address accepted (RFC 5321 path limit).
	MaxEmailLen = 254
)
View Source
const HoneypotField = "website"

HoneypotField is a login form field hidden from people; bots fill it in.

Variables

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

ErrInvalidToken is returned for unknown, used or expired login tokens.

Functions

func NormalizeEmail

func NormalizeEmail(email string) (key string, ok bool)

NormalizeEmail returns the key that makes an account unique: lower case, "+tag" removed, and for Gmail the dots in the local part removed, so one mailbox cannot open several accounts through address aliases. ok is false for anything that is not a single plain address.

func RenderConfirm

func RenderConfirm(d ConfirmData) (template.HTML, error)

RenderConfirm renders the page behind a magic link.

func RenderLoginForm

func RenderLoginForm(d LoginFormData) (template.HTML, error)

RenderLoginForm renders the login page (or "logged in as" with logout).

func RenderSent

func RenderSent(lang, email string) (template.HTML, error)

RenderSent renders the "check your inbox" page.

func SafeNext

func SafeNext(next string) string

SafeNext returns next if it is a path on this site ("/..."), else "/". Used for every redirect target that comes from a form or a link.

func T

func T(lang, key string) string

T returns the login text for key in lang, falling back to English.

Types

type ConfirmData

type ConfirmData struct {
	Lang   string
	Email  string
	Action string
}

ConfirmData feeds the page behind a magic link. Email is empty for an invalid link.

type LoginFormData

type LoginFormData struct {
	Lang    string
	Next    string
	Email   string
	Error   string
	Privacy string // route path of the privacy policy, optional
	User    *User  // set when already logged in
	CSRF    string
}

LoginFormData feeds the login page.

type Mail

type Mail struct {
	Subject string
	HTML    string
}

Mail is a rendered email.

func LoginMail

func LoginMail(lang, siteName, url string) (Mail, error)

LoginMail renders the magic-link email.

type Service

type Service struct {

	// Secure sets the Secure flag on the session cookie (https sites).
	Secure bool
	// contains filtered or unexported fields
}

Service holds the users and login tokens of one site. Pending login tokens live in memory only: a restart invalidates open links (15 minutes at most), which is acceptable and keeps no secrets on disk.

func Open

func Open(dir string) (*Service, error)

Open loads (and creates, mode 0700) the auth directory: the signing secret (generated on first start) and users.sml.

func (*Service) CSRFToken

func (s *Service) CSRFToken(r *http.Request) string

CSRFToken returns the token that POST forms of a logged-in user carry. It is bound to the session cookie, so it changes with every login.

func (*Service) CheckCSRF

func (s *Service) CheckCSRF(r *http.Request, token string) bool

CheckCSRF compares a submitted CSRF token with the expected one.

func (*Service) CheckToken

func (s *Service) CheckToken(token string) (email string, ok bool)

CheckToken reports whether token is a valid, unused login token, without using it up (mail scanners open links; only the POST logs in).

func (*Service) ConsumeToken

func (s *Service) ConsumeToken(token string) (*User, string, error)

ConsumeToken uses up a login token and returns the user, creating the account on first login.

func (*Service) Current

func (s *Service) Current(r *http.Request) *User

Current returns the logged-in user of r, or nil.

func (*Service) IsBlocked

func (s *Service) IsBlocked(id string) bool

IsBlocked reports whether the user id exists and is blocked.

func (*Service) Login

func (s *Service) Login(w http.ResponseWriter, u *User)

Login sets the session cookie for u.

func (*Service) Logout

func (s *Service) Logout(w http.ResponseWriter)

Logout clears the session cookie.

func (*Service) NewLoginToken

func (s *Service) NewLoginToken(email, next string) (string, error)

NewLoginToken creates a magic-link token for email (which the caller has validated as a plain address). After login the user is sent to next. Only the token's hash is kept.

func (*Service) SetBlocked

func (s *Service) SetBlocked(id string, blocked bool) error

SetBlocked blocks or unblocks a user. A blocked user stays logged in but the application ignores what the user did (e.g. votes).

func (*Service) Sign

func (s *Service) Sign(purpose, msg string) string

Sign returns a URL-safe HMAC of msg with the site secret, for links that other modules hand out (e.g. newsletter confirm/unsubscribe). purpose separates the uses, so a signature of one kind never passes as another.

func (*Service) User

func (s *Service) User(id string) (*User, bool)

User returns a copy of the user with id.

func (*Service) UserByKey

func (s *Service) UserByKey(key string) (*User, bool)

UserByKey returns a copy of the user with the normalised email key.

func (*Service) Verify

func (s *Service) Verify(purpose, msg, sig string) bool

Verify checks a signature made by Sign.

type User

type User struct {
	ID      string
	Email   string
	Key     string
	Created time.Time
	Blocked bool
}

User is one verified visitor. Key is the normalised email address that makes the account unique; Email is the address the user typed last.

Jump to

Keyboard shortcuts

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