Schlüssel: API zum Hinterlegen, Ersetzen und Sperren
Endpunkte für öffentliche OpenPGP-v6-Schlüssel: lesen (aktuell und Historie), hinterlegen, neue Fassung hochladen (Verlängern, Umbenennen, Widerruf) und serverseitig sperren. Der Server prüft nur die Struktur (neues Paket internal/pgpkey, ohne Krypto-Bibliothek) und lehnt privates Schlüsselmaterial ab; Signaturen prüfen die Clients. Alle Schreibzugriffe verlangen das Passwort, damit eine gestohlene Session weder Schlüssel tauschen noch Widerrufe zurücknehmen kann. Die Passwortprüfung von /user/delete ist dafür als checkPassword herausgezogen. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TiXsPUqw7oeomZ8wZrQW5q
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
431d9555fd
commit
64e0818abf
@@ -0,0 +1,152 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Fixtures: Wegwerf-Schlüssel aus internal/pgpkey/testdata.
|
||||
func fixture(t *testing.T, name string) []byte {
|
||||
t.Helper()
|
||||
b, err := os.ReadFile("../pgpkey/testdata/" + name)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func keyForm(cert []byte, pass string) url.Values {
|
||||
return url.Values{"key": {base64.StdEncoding.EncodeToString(cert)}, "pass": {pass}}
|
||||
}
|
||||
|
||||
// getAny holt urlStr und liefert Status und dekodierten Body, auch bei Fehlern.
|
||||
func getAny(t *testing.T, urlStr string, v any) int {
|
||||
t.Helper()
|
||||
resp, err := http.Get(urlStr)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if err := json.NewDecoder(resp.Body).Decode(v); err != nil {
|
||||
t.Fatalf("decode %s: %v", urlStr, err)
|
||||
}
|
||||
return resp.StatusCode
|
||||
}
|
||||
|
||||
// postCode schickt ein Formular und liefert Status und Fehlercode.
|
||||
func postCode(t *testing.T, c *http.Client, urlStr string, form url.Values) (int, string) {
|
||||
t.Helper()
|
||||
resp := postForm(t, c, urlStr, form)
|
||||
defer resp.Body.Close()
|
||||
var e errorResponse
|
||||
_ = json.NewDecoder(resp.Body).Decode(&e)
|
||||
return resp.StatusCode, e.Error.Code
|
||||
}
|
||||
|
||||
func TestKeys(t *testing.T) {
|
||||
t.Parallel()
|
||||
srv := newTestServer(t)
|
||||
alice := registerAndLogin(t, srv, "alice")
|
||||
bob := registerAndLogin(t, srv, "bob")
|
||||
const pass = "supersecret1"
|
||||
v6, v6b, revoked := fixture(t, "v6.pgp"), fixture(t, "v6-2.pgp"), fixture(t, "v6-revoked.pgp")
|
||||
fpr := strings.TrimSpace(string(fixture(t, "v6.fpr")))
|
||||
fpr2 := strings.TrimSpace(string(fixture(t, "v6-2.fpr")))
|
||||
keyURL := srv.URL + "/api/u/alice/key"
|
||||
|
||||
var e errorResponse
|
||||
if got := getAny(t, srv.URL+"/api/u/niemand/key", &e); got != http.StatusNotFound {
|
||||
t.Fatalf("unbekannter nutzer: %d", got)
|
||||
}
|
||||
if got := getAny(t, keyURL, &e); got != http.StatusNotFound || e.Error.Code != "key.not_found" {
|
||||
t.Fatalf("ohne schlüssel: %d %s", got, e.Error.Code)
|
||||
}
|
||||
|
||||
// Ablehnungen vor dem ersten Schlüssel.
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
c *http.Client
|
||||
form url.Values
|
||||
status int
|
||||
code string
|
||||
}{
|
||||
{"ohne login", newClient(t), keyForm(v6, pass), 401, "auth.required"},
|
||||
{"falsches passwort", alice, keyForm(v6, "falsch"), 403, "auth.bad_password"},
|
||||
{"privater schlüssel", alice, keyForm(fixture(t, "v6-secret.pgp"), pass), 400, "key.secret"},
|
||||
{"v4", alice, keyForm(fixture(t, "v4.pgp"), pass), 400, "key.unsupported"},
|
||||
{"kein base64", alice, url.Values{"key": {"%%%"}, "pass": {pass}}, 400, "key.invalid"},
|
||||
{"leer", alice, url.Values{"pass": {pass}}, 400, "key.invalid"},
|
||||
} {
|
||||
if st, code := postCode(t, tc.c, srv.URL+"/api/user/key", tc.form); st != tc.status || code != tc.code {
|
||||
t.Errorf("%s: %d %s, erwartet %d %s", tc.name, st, code, tc.status, tc.code)
|
||||
}
|
||||
}
|
||||
|
||||
// Erster Schlüssel.
|
||||
if st, code := postCode(t, alice, srv.URL+"/api/user/key", keyForm(v6, pass)); st != http.StatusCreated {
|
||||
t.Fatalf("schlüssel setzen: %d %s", st, code)
|
||||
}
|
||||
var k keyJSON
|
||||
if got := getAny(t, keyURL, &k); got != http.StatusOK || k.Fingerprint != fpr || !bytes.Equal(k.Key, v6) || k.Status != "current" {
|
||||
t.Fatalf("aktueller schlüssel: %d %+v", got, k)
|
||||
}
|
||||
if st, code := postCode(t, alice, srv.URL+"/api/user/key", keyForm(v6, pass)); st != http.StatusConflict || code != "key.exists" {
|
||||
t.Fatalf("doppelt: %d %s", st, code)
|
||||
}
|
||||
// Derselbe Schlüssel gehört nicht zusätzlich bob.
|
||||
if st, code := postCode(t, bob, srv.URL+"/api/user/key", keyForm(v6, pass)); st != http.StatusConflict {
|
||||
t.Fatalf("fremder schlüssel: %d %s", st, code)
|
||||
}
|
||||
|
||||
// Ersetzen: der alte bleibt in der Historie.
|
||||
if st, code := postCode(t, alice, srv.URL+"/api/user/key", keyForm(v6b, pass)); st != http.StatusCreated {
|
||||
t.Fatalf("ersetzen: %d %s", st, code)
|
||||
}
|
||||
if getAny(t, keyURL, &k); k.Fingerprint != fpr2 {
|
||||
t.Fatalf("nach ersetzen: %+v", k)
|
||||
}
|
||||
|
||||
// Widerruf des alten Schlüssels nachreichen; bob darf das nicht.
|
||||
if st, _ := postCode(t, bob, srv.URL+"/api/user/key/update", keyForm(revoked, pass)); st != http.StatusNotFound {
|
||||
t.Fatalf("update fremder schlüssel: %d", st)
|
||||
}
|
||||
if st, code := postCode(t, alice, srv.URL+"/api/user/key/update", keyForm(revoked, pass)); st != http.StatusOK {
|
||||
t.Fatalf("update: %d %s", st, code)
|
||||
}
|
||||
var hist struct{ Keys []keyJSON }
|
||||
getAny(t, srv.URL+"/api/u/alice/keys", &hist)
|
||||
if len(hist.Keys) != 2 ||
|
||||
hist.Keys[0].Fingerprint != fpr || hist.Keys[0].Status != "replaced" || !bytes.Equal(hist.Keys[0].Key, revoked) ||
|
||||
hist.Keys[1].Fingerprint != fpr2 || hist.Keys[1].Status != "current" {
|
||||
t.Fatalf("historie: %+v", hist.Keys)
|
||||
}
|
||||
|
||||
// Serverseitig sperren.
|
||||
if st, _ := postCode(t, alice, srv.URL+"/api/user/key/block", url.Values{"pass": {"falsch"}}); st != http.StatusForbidden {
|
||||
t.Fatalf("sperren mit falschem passwort: %d", st)
|
||||
}
|
||||
if st, code := postCode(t, alice, srv.URL+"/api/user/key/block", url.Values{"pass": {pass}}); st != http.StatusOK {
|
||||
t.Fatalf("sperren: %d %s", st, code)
|
||||
}
|
||||
if got := getAny(t, keyURL, &e); got != http.StatusNotFound || e.Error.Code != "key.blocked" {
|
||||
t.Fatalf("gesperrt: %d %s", got, e.Error.Code)
|
||||
}
|
||||
if st, code := postCode(t, alice, srv.URL+"/api/user/key/block", url.Values{"pass": {pass}}); st != http.StatusNotFound || code != "key.not_found" {
|
||||
t.Fatalf("doppelt sperren: %d %s", st, code)
|
||||
}
|
||||
|
||||
// Konto löschen nimmt die Schlüssel mit.
|
||||
if st := postStatus(t, alice, srv.URL+"/api/user/delete", url.Values{"pass1": {pass}}); st != http.StatusOK {
|
||||
t.Fatalf("konto löschen: %d", st)
|
||||
}
|
||||
var n int
|
||||
if err := srv.db.QueryRow(`SELECT COUNT(*) FROM account_key`).Scan(&n); err != nil || n != 0 {
|
||||
t.Fatalf("schlüssel nach konto-löschung: %d %v", n, err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user