Files
allmende/internal/api/certificate_test.go
T
nilsandClaude Sonnet 5.5 dfe35b422d Renew the device certificate from the browser, with the recovery phrase
Within the last 30 days the owner page offers it. The node signs a new certificate for the same device key (peer ID and listings stay), saves it, and signs again own listings that would outlive the old certificate. The phrase is used for one request and not kept; another identity's phrase and no-phrase identities are refused. Device.Current gives the certificate to sign with while a renewal happens.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
2026-11-18 23:19:30 +01:00

153 lines
4.6 KiB
Go

package api
import (
"encoding/json"
"net/http"
"strings"
"testing"
"time"
"git.stinnesbeck.com/nils/allmende/internal/identity"
)
// fakeKeeper renews the device in memory, like the real one minus the disk.
type fakeKeeper struct {
dev *identity.Device
canRenew bool
renewed int
}
func (k *fakeKeeper) CanRenew() bool { return k.canRenew }
func (k *fakeKeeper) Renew(root *identity.Identity) (*identity.Cert, error) {
k.renewed++
return k.dev.Renew(root, time.Now(), 365*24*time.Hour)
}
// withKeeper installs a keeper for the fixture's own device.
func withKeeper(canRenew bool) (func(*Config), **fakeKeeper) {
var k *fakeKeeper
return func(c *Config) {
k = &fakeKeeper{dev: c.Device, canRenew: canRenew}
c.CertKeeper = k
}, &k
}
type certStatus struct {
CertExpiresAt int64 `json:"cert_expires_at"`
CanRenew bool `json:"can_renew"`
}
func (f *fixture) certStatus(t *testing.T) certStatus {
t.Helper()
code, body := f.do(t, "GET", "/api/v1/status", "", false)
if code != http.StatusOK {
t.Fatalf("status: %d %s", code, body)
}
var s certStatus
if err := json.Unmarshal(body, &s); err != nil {
t.Fatal(err)
}
return s
}
func TestStatusReportsWhenTheCertificateExpiresAndWhetherItCanBeRenewed(t *testing.T) {
tweak, _ := withKeeper(true)
f := newFixture(t, token, tweak)
s := f.certStatus(t)
if s.CertExpiresAt != f.dev.Cert.ExpiresAt {
t.Fatalf("cert_expires_at = %d, want %d", s.CertExpiresAt, f.dev.Cert.ExpiresAt)
}
if !s.CanRenew {
t.Fatal("a node with a recovery phrase must say it can renew")
}
noPhrase, _ := withKeeper(false)
g := newFixture(t, token, noPhrase)
if g.certStatus(t).CanRenew {
t.Fatal("an identity without a recovery phrase must not offer renewal")
}
plain := newFixture(t, token)
if plain.certStatus(t).CanRenew {
t.Fatal("a node without a keeper must not offer renewal")
}
}
func TestRenewingFromThePhraseExtendsTheCertificate(t *testing.T) {
tweak, keeper := withKeeper(true)
f := newFixture(t, token, tweak)
before := f.certStatus(t).CertExpiresAt
code, body := f.do(t, "PUT", "/api/v1/certificate", `{"phrase":"`+f.phrase+`"}`, true)
if code != http.StatusOK {
t.Fatalf("renew: %d %s", code, body)
}
var out certStatus
if err := json.Unmarshal(body, &out); err != nil {
t.Fatal(err)
}
after := f.certStatus(t).CertExpiresAt
if after <= before {
t.Fatalf("expiry %d did not move past %d", after, before)
}
if out.CertExpiresAt != after {
t.Fatalf("the reply says %d but status says %d", out.CertExpiresAt, after)
}
if (*keeper).renewed != 1 {
t.Fatalf("the keeper renewed %d times, want 1", (*keeper).renewed)
}
}
func TestRenewingNeedsTheAdminToken(t *testing.T) {
tweak, keeper := withKeeper(true)
f := newFixture(t, token, tweak)
code, _ := f.do(t, "PUT", "/api/v1/certificate", `{"phrase":"`+f.phrase+`"}`, false)
if code != http.StatusUnauthorized {
t.Fatalf("without the token: %d, want 401", code)
}
if (*keeper).renewed != 0 {
t.Fatal("nothing may be renewed without the token")
}
}
func TestRenewingWithAnotherIdentitysPhraseIsRefusedAndChangesNothing(t *testing.T) {
tweak, _ := withKeeper(true)
f := newFixture(t, token, tweak)
_, other, _ := identity.NewRoot()
before := f.certStatus(t).CertExpiresAt
code, body := f.do(t, "PUT", "/api/v1/certificate", `{"phrase":"`+other+`"}`, true)
if code != http.StatusConflict || !strings.Contains(string(body), "different identity") {
t.Fatalf("another identity's phrase: %d %s, want 409 naming the different identity", code, body)
}
if after := f.certStatus(t).CertExpiresAt; after != before {
t.Fatalf("a refused renewal moved the expiry from %d to %d", before, after)
}
}
func TestRenewingWithAnInvalidPhraseIsABadRequest(t *testing.T) {
tweak, keeper := withKeeper(true)
f := newFixture(t, token, tweak)
for _, phrase := range []string{"", "not twelve words", "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon"} {
code, body := f.do(t, "PUT", "/api/v1/certificate", `{"phrase":"`+phrase+`"}`, true)
if code != http.StatusBadRequest {
t.Fatalf("phrase %q: %d %s, want 400", phrase, code, body)
}
}
if (*keeper).renewed != 0 {
t.Fatal("an invalid phrase must never reach the keeper")
}
}
func TestRenewingIsRefusedForAnIdentityWithoutAPhrase(t *testing.T) {
tweak, keeper := withKeeper(false)
f := newFixture(t, token, tweak)
code, body := f.do(t, "PUT", "/api/v1/certificate", `{"phrase":"`+f.phrase+`"}`, true)
if code != http.StatusConflict || !strings.Contains(string(body), "no recovery phrase") {
t.Fatalf("no-phrase identity: %d %s, want 409 saying there is no recovery phrase", code, body)
}
if (*keeper).renewed != 0 {
t.Fatal("nothing may be renewed")
}
}