okami.mom/albedo

git clone https://git.nkp.pet/okami.mom/albedo

Aly Raffaufdocs(test): explain coverage and manual checks2f6697a

main
2.2 KiB52 linesraw
1// Authentication recovery must retry the captured flow and secret fields.
2// A controlled peer changes discovery between attempts to expose rebinding.
3package daemon
4
5import (
6	"encoding/json"
7	"io"
8	"net/http"
9	"testing"
10)
11
12func TestLoginRetryFreezesAdvertisedFlowAndSecretFields(t *testing.T) {
13	values := map[string]json.RawMessage{"tenant": json.RawMessage(`"original"`), "secret": json.RawMessage(`"one-time-secret"`)}
14	var admitted []string
15	conn := controlledConnection(t, func(w http.ResponseWriter, r *http.Request) {
16		if r.URL.Path != "/auth/logins/chosen-login" {
17			t.Errorf("changed login identity: %s", r.URL)
18		}
19		if r.Method == http.MethodGet {
20			w.WriteHeader(404)
21			_, _ = io.WriteString(w, `{"type":"about:blank","title":"Not found","status":404,"code":"not_found","detail":"No retained login"}`)
22			return
23		}
24		if r.Method != http.MethodPut {
25			t.Errorf("wrong login admission method %s", r.Method)
26		}
27		body, _ := io.ReadAll(r.Body)
28		admitted = append(admitted, string(body))
29		if len(admitted) == 1 {
30			values["secret"] = json.RawMessage(`"edited-after-send"`)
31			delete(values, "tenant")
32			w.WriteHeader(201)
33			_, _ = io.WriteString(w, `{"invalid":"acknowledgment"}`)
34			return
35		}
36		w.Header().Set("ETag", `"login-a"`)
37		_ = json.NewEncoder(w).Encode(map[string]any{"id": "chosen-login", "provider": "provider", "url": "https://provider.example/authorize", "expires_at": "2026-10-04T00:00:00Z", "state": "waiting", "instructions": nil, "progress": "Waiting", "accounts": []any{}, "failure": nil})
38	})
39	result, err := StartSignInWithID(t.Context(), conn, "chosen-login", "provider", "device", values)
40	if err != nil || result.ETag != `"login-a"` || len(admitted) != 2 || admitted[0] != admitted[1] {
41		t.Fatalf("login intent changed across retry: %v %#v %v", err, result, admitted)
42	}
43	var request map[string]any
44	_ = json.Unmarshal([]byte(admitted[0]), &request)
45	if request["flow"] != "device" || request["values"].(map[string]any)["secret"] != "one-time-secret" {
46		t.Fatalf("advertised flow or secret omitted: %v", request)
47	}
48}
49
50// The daemon cannot manufacture an invalid extension schema or result. This
51// controlled peer verifies the effect is never replayed and omitted form values
52// do not become unintended null writes.