sonda/internal/auth/auth_test.go
tgiachi 96ad8b1f62 feat(auth): accounts are identified by an email address
Registration, login and the admin's create-user call take `email` instead of
`username`, and the user object returns `email`. The address is checked for its
shape (a plain address, no display name, at most 254 bytes), trimmed, lowercased
and unique. Sonda sends no mail and does not verify it.

- Migration 0018 renames the users column (and the PostgreSQL index); values are
  kept, so an account made before keeps its plain name and still logs in with it
  (login does not check the shape).
- Error code username_taken becomes email_taken; the JWT claim is now `email`,
  so access tokens issued before the upgrade are refused once and clients refresh.
- OpenAPI spec, README, the web login form (type email), strings (en, it) and
  the generated API types follow.

Closes #65
2026-11-12 14:40:33 +01:00

124 lines
3.1 KiB
Go

package auth
import (
"strings"
"testing"
"time"
"github.com/golang-jwt/jwt/v5"
)
// ---- password hashing ----
func TestHashAndVerify(t *testing.T) {
h, err := HashPassword("correct horse")
if err != nil {
t.Fatal(err)
}
if !strings.HasPrefix(h, "$argon2id$") {
t.Fatalf("hash = %q, want argon2id PHC string", h)
}
if !VerifyPassword(h, "correct horse") {
t.Error("correct password rejected")
}
if VerifyPassword(h, "wrong") {
t.Error("wrong password accepted")
}
}
func TestHashIsSalted(t *testing.T) {
a, _ := HashPassword("same")
b, _ := HashPassword("same")
if a == b {
t.Error("two hashes of the same password are identical")
}
}
func TestVerifyRejectsGarbage(t *testing.T) {
for _, h := range []string{"", "plaintext", "$argon2id$v=19$m=1,t=1,p=1$bad", "$bcrypt$x$y"} {
if VerifyPassword(h, "x") {
t.Errorf("garbage hash %q accepted", h)
}
}
}
// ---- tokens ----
const testSecret = "0123456789abcdef0123456789abcdef"
func TestTokenRoundTrip(t *testing.T) {
iss := NewTokenIssuer(testSecret, time.Hour)
tok, exp, err := iss.Issue(User{ID: 7, Email: "bob"})
if err != nil {
t.Fatal(err)
}
if time.Until(exp) <= 0 {
t.Error("expiry is not in the future")
}
claims, err := iss.Parse(tok)
if err != nil {
t.Fatal(err)
}
if claims.UserID != 7 || claims.Email != "bob" {
t.Errorf("claims = %+v", claims)
}
}
func TestTokenCarriesTheMomentItWasIssued(t *testing.T) {
iss := NewTokenIssuer(testSecret, time.Hour)
at := time.Date(2026, 5, 6, 7, 8, 9, 500_000_000, time.UTC)
iss.now = func() time.Time { return at }
tok, _, err := iss.Issue(User{ID: 7, Email: "bob"})
if err != nil {
t.Fatal(err)
}
claims, err := iss.Parse(tok)
if err != nil {
t.Fatal(err)
}
if want := at.Truncate(time.Second); !claims.IssuedAt.Equal(want) {
t.Errorf("issued at = %v, want %v (whole seconds)", claims.IssuedAt, want)
}
}
func TestTokenExpired(t *testing.T) {
iss := NewTokenIssuer(testSecret, time.Hour)
iss.now = func() time.Time { return time.Now().Add(-2 * time.Hour) }
tok, _, err := iss.Issue(User{ID: 1, Email: "a"})
if err != nil {
t.Fatal(err)
}
iss.now = time.Now
if _, err := iss.Parse(tok); err == nil {
t.Fatal("expired token accepted")
}
}
func TestTokenWrongSecret(t *testing.T) {
tok, _, _ := NewTokenIssuer(testSecret, time.Hour).Issue(User{ID: 1, Email: "a"})
other := NewTokenIssuer("ffffffffffffffffffffffffffffffff", time.Hour)
if _, err := other.Parse(tok); err == nil {
t.Fatal("token signed with another secret accepted")
}
}
func TestTokenRejectsAlgNone(t *testing.T) {
claims := jwt.RegisteredClaims{
Subject: "1",
Issuer: tokenIssuerName,
ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Hour)),
}
tok, err := jwt.NewWithClaims(jwt.SigningMethodNone, claims).SignedString(jwt.UnsafeAllowNoneSignatureType)
if err != nil {
t.Fatal(err)
}
if _, err := NewTokenIssuer(testSecret, time.Hour).Parse(tok); err == nil {
t.Fatal("alg=none token accepted")
}
}
func TestTokenGarbage(t *testing.T) {
if _, err := NewTokenIssuer(testSecret, time.Hour).Parse("not.a.jwt"); err == nil {
t.Fatal("garbage token accepted")
}
}