package httpapi import ( "bytes" "encoding/json" "net/http" "testing" ) // nativeLogin mimics the mobile app: no cookie jar, an explicit client marker // and the session token carried in the response body. func nativeLogin(t *testing.T, h *harness) (token, csrf string) { t.Helper() raw, _ := json.Marshal(map[string]string{"username": "admin", "password": testPassword}) req, err := http.NewRequest(http.MethodPost, h.url+"/"+testEntry+"/api/v1/auth/login", bytes.NewReader(raw)) if err != nil { t.Fatal(err) } req.Header.Set("Content-Type", "application/json") req.Header.Set(nativeClientHeader, "app") resp, err := (&http.Client{}).Do(req) if err != nil { t.Fatal(err) } defer resp.Body.Close() var env struct { Code int `json:"code"` Data struct { Token string `json:"token"` CSRF string `json:"csrf"` } `json:"data"` } if err := json.NewDecoder(resp.Body).Decode(&env); err != nil { t.Fatal(err) } if env.Code != 0 || env.Data.Token == "" { t.Fatalf("native login did not return a token: %+v", env) } return env.Data.Token, env.Data.CSRF } func TestNativeClientReceivesToken(t *testing.T) { h := newHarness(t) token, csrf := nativeLogin(t, h) if csrf == "" { t.Fatal("csrf missing") } // A browser login must not leak the token into the JSON body. code, env := h.do(t, http.MethodPost, "/api/v1/auth/login", map[string]string{"username": "admin", "password": testPassword}, "") if code != http.StatusOK { t.Fatalf("browser login status %d", code) } if _, ok := env["data"].(map[string]any)["token"]; ok { t.Fatal("browser login must not return a bearer token") } if token == "" { t.Fatal("empty token") } } func TestBearerAuthSkipsCSRF(t *testing.T) { h := newHarness(t) token, _ := nativeLogin(t, h) client := &http.Client{} // Mutating call without X-CSRF-Token works because the credential is not // attached automatically by a browser. raw, _ := json.Marshal(map[string]any{ "name": "app-site", "kind": "static", "domains": []string{"app.example.com"}, }) req, err := http.NewRequest(http.MethodPost, h.url+"/"+testEntry+"/api/v1/sites", bytes.NewReader(raw)) if err != nil { t.Fatal(err) } req.Header.Set("Content-Type", "application/json") req.Header.Set("Authorization", "Bearer "+token) resp, err := client.Do(req) if err != nil { t.Fatal(err) } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { t.Fatalf("bearer create status %d", resp.StatusCode) } // The same request without a token must still be rejected. req2, _ := http.NewRequest(http.MethodGet, h.url+"/"+testEntry+"/api/v1/sites", nil) resp2, err := client.Do(req2) if err != nil { t.Fatal(err) } defer resp2.Body.Close() if resp2.StatusCode != http.StatusUnauthorized { t.Fatalf("anonymous request status %d", resp2.StatusCode) } } func TestBearerTokenRejectsGarbage(t *testing.T) { h := newHarness(t) req, _ := http.NewRequest(http.MethodGet, h.url+"/"+testEntry+"/api/v1/sites", nil) req.Header.Set("Authorization", "Bearer not-a-real-token") resp, err := (&http.Client{}).Do(req) if err != nil { t.Fatal(err) } defer resp.Body.Close() if resp.StatusCode != http.StatusUnauthorized { t.Fatalf("garbage token accepted: %d", resp.StatusCode) } }