miaopanel
1package httpapi
2
3import (
4 "bytes"
5 "encoding/json"
6 "net/http"
7 "testing"
8)
9
10// nativeLogin mimics the mobile app: no cookie jar, an explicit client marker
11// and the session token carried in the response body.
12func nativeLogin(t *testing.T, h *harness) (token, csrf string) {
13 t.Helper()
14 raw, _ := json.Marshal(map[string]string{"username": "admin", "password": testPassword})
15 req, err := http.NewRequest(http.MethodPost,
16 h.url+"/"+testEntry+"/api/v1/auth/login", bytes.NewReader(raw))
17 if err != nil {
18 t.Fatal(err)
19 }
20 req.Header.Set("Content-Type", "application/json")
21 req.Header.Set(nativeClientHeader, "app")
22 resp, err := (&http.Client{}).Do(req)
23 if err != nil {
24 t.Fatal(err)
25 }
26 defer resp.Body.Close()
27 var env struct {
28 Code int `json:"code"`
29 Data struct {
30 Token string `json:"token"`
31 CSRF string `json:"csrf"`
32 } `json:"data"`
33 }
34 if err := json.NewDecoder(resp.Body).Decode(&env); err != nil {
35 t.Fatal(err)
36 }
37 if env.Code != 0 || env.Data.Token == "" {
38 t.Fatalf("native login did not return a token: %+v", env)
39 }
40 return env.Data.Token, env.Data.CSRF
41}
42
43func TestNativeClientReceivesToken(t *testing.T) {
44 h := newHarness(t)
45 token, csrf := nativeLogin(t, h)
46 if csrf == "" {
47 t.Fatal("csrf missing")
48 }
49 // A browser login must not leak the token into the JSON body.
50 code, env := h.do(t, http.MethodPost, "/api/v1/auth/login",
51 map[string]string{"username": "admin", "password": testPassword}, "")
52 if code != http.StatusOK {
53 t.Fatalf("browser login status %d", code)
54 }
55 if _, ok := env["data"].(map[string]any)["token"]; ok {
56 t.Fatal("browser login must not return a bearer token")
57 }
58 if token == "" {
59 t.Fatal("empty token")
60 }
61}
62
63func TestBearerAuthSkipsCSRF(t *testing.T) {
64 h := newHarness(t)
65 token, _ := nativeLogin(t, h)
66 client := &http.Client{}
67
68 // Mutating call without X-CSRF-Token works because the credential is not
69 // attached automatically by a browser.
70 raw, _ := json.Marshal(map[string]any{
71 "name": "app-site", "kind": "static", "domains": []string{"app.example.com"},
72 })
73 req, err := http.NewRequest(http.MethodPost,
74 h.url+"/"+testEntry+"/api/v1/sites", bytes.NewReader(raw))
75 if err != nil {
76 t.Fatal(err)
77 }
78 req.Header.Set("Content-Type", "application/json")
79 req.Header.Set("Authorization", "Bearer "+token)
80 resp, err := client.Do(req)
81 if err != nil {
82 t.Fatal(err)
83 }
84 defer resp.Body.Close()
85 if resp.StatusCode != http.StatusOK {
86 t.Fatalf("bearer create status %d", resp.StatusCode)
87 }
88
89 // The same request without a token must still be rejected.
90 req2, _ := http.NewRequest(http.MethodGet, h.url+"/"+testEntry+"/api/v1/sites", nil)
91 resp2, err := client.Do(req2)
92 if err != nil {
93 t.Fatal(err)
94 }
95 defer resp2.Body.Close()
96 if resp2.StatusCode != http.StatusUnauthorized {
97 t.Fatalf("anonymous request status %d", resp2.StatusCode)
98 }
99}
100
101func TestBearerTokenRejectsGarbage(t *testing.T) {
102 h := newHarness(t)
103 req, _ := http.NewRequest(http.MethodGet, h.url+"/"+testEntry+"/api/v1/sites", nil)
104 req.Header.Set("Authorization", "Bearer not-a-real-token")
105 resp, err := (&http.Client{}).Do(req)
106 if err != nil {
107 t.Fatal(err)
108 }
109 defer resp.Body.Close()
110 if resp.StatusCode != http.StatusUnauthorized {
111 t.Fatalf("garbage token accepted: %d", resp.StatusCode)
112 }
113}