- feat(auth): short-lived access tokens plus rotating refresh tokens stored as
SHA-256 hashes; replaying a spent token revokes every session of that user
- feat(auth): is_admin flag; the first registered user is the admin and the
last admin can be neither demoted nor deleted
- feat(api): POST /auth/refresh, /auth/logout, /auth/password and GET /auth/me
- feat(api): admin-only GET/POST /users, PATCH /users/{id}, PUT
/users/{id}/password and DELETE /users/{id}; the admin check reads the role
from the database on every call
- feat(api): starting a library scan is now admin-only
- feat(storage): users and refresh token repositories for SQLite and
PostgreSQL (migration 0004; the oldest existing account becomes admin)
- feat(config): auth.access_ttl_minutes and auth.refresh_ttl_hours replace
auth.token_ttl_hours
- test: rotation, reuse detection, concurrent rotation with a single winner,
admin rules, role changes taking effect immediately
107 lines
2.6 KiB
Go
107 lines
2.6 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, Username: "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.Username != "bob" {
|
|
t.Errorf("claims = %+v", claims)
|
|
}
|
|
}
|
|
|
|
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, Username: "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, Username: "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")
|
|
}
|
|
}
|