internal/auth/ provides: - TokenStore: 32-byte cryptographically random one-time tokens. Only the SHA-256 hash is persisted (so a DB leak doesn't grant active sessions). Comparison uses subtle.ConstantTimeCompare. Single-use is enforced via UPDATE ... WHERE used_at IS NULL. - Signer: HS256 JWTs with 24h lifetime, jwt.WithValidMethods to reject alg=none and other downgrade attacks. - LogMailer (dev) and SMTPMailer (prod via net/smtp) behind a Mailer interface. - RateLimiter: DB-backed fixed window per email; default 5 per 15 min for the magic-link flow. - Service: orchestrates RequestLogin (auto-creates user on first login, generates token, emails magic link) and Verify (consumes token, updates last_login, issues JWT). - Handlers: POST /auth/login and GET/POST /auth/verify. HandleLogin returns 202 even on validation failure to avoid account enumeration; rate-limit hits surface as 429. Schema additions: magic_tokens (with FK + cascade) and login_attempts. UserStore.SetStoragePath added for completeness. Tests cover: token issue/consume, single-use, expiry, rate limit, JWT round-trip, alg=none rejection, signature tampering, purge, HTTP handlers (login + verify, missing/invalid token paths). Closes #9. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
63 lines
1.7 KiB
Go
63 lines
1.7 KiB
Go
package auth
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"net/smtp"
|
|
"strings"
|
|
)
|
|
|
|
// Mailer sends magic-link emails. The interface keeps the auth flow
|
|
// decoupled from any concrete delivery backend.
|
|
type Mailer interface {
|
|
SendMagicLink(ctx context.Context, email, link string) error
|
|
}
|
|
|
|
// LogMailer writes the magic link to a writer instead of sending an
|
|
// email. Useful for development and tests.
|
|
type LogMailer struct{ W io.Writer }
|
|
|
|
func (m LogMailer) SendMagicLink(_ context.Context, email, link string) error {
|
|
if m.W == nil {
|
|
return nil
|
|
}
|
|
_, err := fmt.Fprintf(m.W, "magic link for %s: %s\n", email, link)
|
|
return err
|
|
}
|
|
|
|
// SMTPMailer delivers via plain SMTP with optional auth. TLS is the
|
|
// caller's responsibility (use submission port 587 with STARTTLS or 465
|
|
// with implicit TLS — the stdlib smtp package handles STARTTLS via
|
|
// SendMail when the server supports it).
|
|
type SMTPMailer struct {
|
|
Host string
|
|
Port string
|
|
Username string
|
|
Password string
|
|
From string
|
|
}
|
|
|
|
func (m SMTPMailer) SendMagicLink(_ context.Context, to, link string) error {
|
|
if m.Host == "" || m.From == "" {
|
|
return fmt.Errorf("smtp not configured")
|
|
}
|
|
addr := m.Host + ":" + m.Port
|
|
var auth smtp.Auth
|
|
if m.Username != "" {
|
|
auth = smtp.PlainAuth("", m.Username, m.Password, m.Host)
|
|
}
|
|
subject := "Your librenotes login link"
|
|
body := fmt.Sprintf("Click to sign in to librenotes:\n\n%s\n\nThis link is valid for 15 minutes and can be used once.\n", link)
|
|
msg := strings.Join([]string{
|
|
"From: " + m.From,
|
|
"To: " + to,
|
|
"Subject: " + subject,
|
|
"MIME-Version: 1.0",
|
|
"Content-Type: text/plain; charset=utf-8",
|
|
"",
|
|
body,
|
|
}, "\r\n")
|
|
return smtp.SendMail(addr, auth, m.From, []string{to}, []byte(msg))
|
|
}
|