CI / Test (ubuntu-latest) (push) Canceled after 0s
CI / Test (windows-latest) (push) Canceled after 0s
CI / Storage tests on PostgreSQL (push) Canceled after 0s
CI / Complement (federation off) (push) Canceled after 0s
CI / golangci-lint (push) Canceled after 0s
CI / Smoke test the binary (push) Canceled after 0s
Implements PLAN.md Phase 1. A client can now register, log in, manage its devices and set its profile; an operator can create accounts and flip the runtime switches. There are still no rooms and no real /sync. userapi: accounts, argon2id passwords in PHC format, devices, access and refresh tokens stored as SHA-256 hashes, account data and the admin flag. Its storage interface is storage.UserStore, implemented once in sqlstore with `?` placeholders; the postgres driver supplies a rebind function and unique-violation recognition rather than keeping a second copy of thirty statements. Timestamps in these tables are int64 milliseconds, the unit the Client-Server API uses on the wire. Client-Server API: register (user-interactive auth with m.login.dummy and optional registration tokens), register/available, token validity, login with m.login.password, logout, logout/all, refresh, account/whoami, capabilities, devices/* with password-gated deletion, profile/*, global and per-room account data. GET /sync is a deliberate placeholder: it answers a valid token with an empty but well-formed response and refuses a revoked one. Phase 1 has no rooms, so that is the whole truth, and it is what makes session revocation observable. Phase 4 replaces it with the syncapi. Auth middleware resolves a bearer token (header or the legacy query parameter) to a device and user, answering an expired token with the soft logout the spec asks for. Rate limiting is a token bucket per endpoint class keyed on the client address, returning M_LIMIT_EXCEEDED with retry_after_ms. adminapi v1 under /_warp/admin/v1: admin-only auth via a normal Matrix token, GET/PUT /federation and /registration driving internal/settings and reporting a config-pinned switch as a 409, GET/POST /users, GET /stats. warp create-account [--admin] creates the first account straight in the database, so a new install with registration off has a way in. Complement is wired up for the federation=off profile: an image meeting its contract, an entrypoint that configures Warp entirely from WARP_* variables, and a runner that builds, clones and executes the target set (TestRegistration, TestLogin, TestDeviceManagement). It has not been run yet -- there is no container runtime on the development machine -- so the three tests' assertions were read from the Complement sources and pinned as httptest tests instead, and CI gained a job that runs the real thing. Also fixes a .gitignore bug from Phase 0: the unanchored `warp` pattern, meant for the built binary, also matched the directory server/cmd/warp and silently excluded the entire command package from the repository. The binary patterns are now anchored to the repository root, and cmd/warp is committed for the first time. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
161 lines
5.0 KiB
Go
161 lines
5.0 KiB
Go
package userapi
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
|
|
"golang.org/x/crypto/argon2"
|
|
)
|
|
|
|
func TestHashAndVerifyPassword(t *testing.T) {
|
|
const password = "correct horse battery staple"
|
|
|
|
hash, err := HashPassword(password)
|
|
if err != nil {
|
|
t.Fatalf("HashPassword: %v", err)
|
|
}
|
|
if !strings.HasPrefix(hash, "$argon2id$v=") {
|
|
t.Fatalf("hash %q is not in the PHC format", hash)
|
|
}
|
|
|
|
ok, err := VerifyPassword(hash, password)
|
|
if err != nil || !ok {
|
|
t.Fatalf("VerifyPassword of the right password = %v, %v", ok, err)
|
|
}
|
|
|
|
ok, err = VerifyPassword(hash, password+"!")
|
|
if err != nil {
|
|
t.Fatalf("VerifyPassword: %v", err)
|
|
}
|
|
if ok {
|
|
t.Error("the wrong password verified")
|
|
}
|
|
}
|
|
|
|
// TestHashIsSalted is what makes a stolen table worth attacking one row at a
|
|
// time rather than all at once.
|
|
func TestHashIsSalted(t *testing.T) {
|
|
first, err := HashPassword("same password")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
second, err := HashPassword("same password")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if first == second {
|
|
t.Error("two hashes of the same password are identical, so the salt is not random")
|
|
}
|
|
}
|
|
|
|
// TestHashRecordsItsParameters is what lets the cost be raised later without
|
|
// invalidating every existing password.
|
|
func TestHashRecordsItsParameters(t *testing.T) {
|
|
hash, err := HashPassword("x")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
params, salt, key, err := decodeHash(hash)
|
|
if err != nil {
|
|
t.Fatalf("decodeHash of our own output: %v", err)
|
|
}
|
|
if params.memory != argonMemoryKiB || params.time != argonTime || params.lanes != argonLanes {
|
|
t.Errorf("recorded parameters = %+v, want the current constants", params)
|
|
}
|
|
if len(salt) != argonSaltLen || len(key) != argonKeyLen {
|
|
t.Errorf("salt is %d bytes and key %d bytes", len(salt), len(key))
|
|
}
|
|
}
|
|
|
|
// TestVerifyAgainstAnOlderCost proves the point of recording them: a hash
|
|
// made with weaker parameters still verifies.
|
|
func TestVerifyAgainstAnOlderCost(t *testing.T) {
|
|
const password = "from an older release"
|
|
// A hash as a previous, cheaper, configuration would have written it.
|
|
salt := []byte("0123456789abcdef")
|
|
key := argon2.IDKey([]byte(password), salt, 1, 8*1024, 1, 32)
|
|
hash := "$argon2id$v=19$m=8192,t=1,p=1$" + b64.EncodeToString(salt) + "$" + b64.EncodeToString(key)
|
|
|
|
ok, err := VerifyPassword(hash, password)
|
|
if err != nil || !ok {
|
|
t.Fatalf("VerifyPassword against older parameters = %v, %v", ok, err)
|
|
}
|
|
}
|
|
|
|
// TestVerifyRejectsMalformedHashes keeps a corrupt row from looking like a
|
|
// wrong password for ever.
|
|
func TestVerifyRejectsMalformedHashes(t *testing.T) {
|
|
good, err := HashPassword("x")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
parts := strings.Split(good, "$")
|
|
|
|
for name, hash := range map[string]string{
|
|
"empty": "",
|
|
"not a hash": "hunter2",
|
|
"too few fields": "$argon2id$v=19$m=19456,t=2,p=1$" + parts[4],
|
|
"wrong algorithm": "$argon2i$v=19$" + strings.Join(parts[3:], "$"),
|
|
"unknown version": "$argon2id$v=16$" + strings.Join(parts[3:], "$"),
|
|
"unparseable params": "$argon2id$v=19$m=lots,t=2,p=1$" + parts[4] + "$" + parts[5],
|
|
"zero memory": "$argon2id$v=19$m=0,t=2,p=1$" + parts[4] + "$" + parts[5],
|
|
"bad base64 salt": "$argon2id$v=19$m=19456,t=2,p=1$not!base64$" + parts[5],
|
|
"empty key": "$argon2id$v=19$m=19456,t=2,p=1$" + parts[4] + "$",
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
ok, err := VerifyPassword(hash, "x")
|
|
if ok {
|
|
t.Fatal("a malformed hash verified")
|
|
}
|
|
if !errors.Is(err, ErrInvalidPasswordHash) {
|
|
t.Errorf("error = %v, want ErrInvalidPasswordHash", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestPasswordLengthIsBounded(t *testing.T) {
|
|
long := strings.Repeat("x", MaxPasswordLength+1)
|
|
|
|
if _, err := HashPassword(long); !errors.Is(err, ErrPasswordTooLong) {
|
|
t.Errorf("HashPassword of an overlong password = %v, want ErrPasswordTooLong", err)
|
|
}
|
|
|
|
hash, err := HashPassword(strings.Repeat("x", MaxPasswordLength))
|
|
if err != nil {
|
|
t.Fatalf("a password at the limit must be accepted: %v", err)
|
|
}
|
|
if _, err := VerifyPassword(hash, long); !errors.Is(err, ErrPasswordTooLong) {
|
|
t.Errorf("VerifyPassword of an overlong password = %v, want ErrPasswordTooLong", err)
|
|
}
|
|
}
|
|
|
|
// TestNonASCIIPasswords: the hash takes bytes, so there is nothing to
|
|
// normalise, but a client sending one must not be refused.
|
|
func TestNonASCIIPasswords(t *testing.T) {
|
|
const password = "übers3kr1t 🔐"
|
|
|
|
hash, err := HashPassword(password)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if ok, err := VerifyPassword(hash, password); err != nil || !ok {
|
|
t.Errorf("VerifyPassword = %v, %v", ok, err)
|
|
}
|
|
}
|
|
|
|
// TestDummyPasswordHashIsUsable: the dummy hash exists so that a login for
|
|
// an account that does not exist costs the same as one that does. If it ever
|
|
// stopped being a valid hash, that work would vanish and the timing would
|
|
// leak which user names are real.
|
|
func TestDummyPasswordHashIsUsable(t *testing.T) {
|
|
ok, err := VerifyPassword(dummyPasswordHash, "anything")
|
|
if err != nil {
|
|
t.Fatalf("the dummy hash is not verifiable: %v", err)
|
|
}
|
|
if ok {
|
|
t.Error("the dummy hash matched a password")
|
|
}
|
|
}
|