| 1 | package auth |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "image" |
| 7 | "image/color" |
| 8 | "image/png" |
| 9 | "net/http" |
| 10 | "net/http/httptest" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | ) |
| 14 | |
| 15 | func TestFetchPhotoOK(t *testing.T) { |
| 16 | img := tinyPNG() |
| 17 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 18 | if r.URL.Path != "/v1.0/me/photo/$value" { |
| 19 | t.Errorf("path %s", r.URL.Path) |
| 20 | } |
| 21 | if r.Header.Get("Authorization") != "Bearer entra-access" { |
| 22 | t.Errorf("auth %q", r.Header.Get("Authorization")) |
| 23 | } |
| 24 | w.Header().Set("Content-Type", "image/png") |
| 25 | _, _ = w.Write(img) |
| 26 | })) |
| 27 | t.Cleanup(srv.Close) |
| 28 | e := NewEntra("https://login.example.test/t/v2.0", "t", "c", "s", "BlueGit-test") |
| 29 | e.HTTP = srv.Client() |
| 30 | e.GraphPhotoURL = srv.URL + "/v1.0/me/photo/$value" |
| 31 | got, err := e.FetchPhoto(context.Background(), "entra-access") |
| 32 | if err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | if !bytes.Equal(got, img) { |
| 36 | t.Fatalf("bytes %d", len(got)) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func TestFetchPhotoNotFound(t *testing.T) { |
| 41 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 42 | http.Error(w, `{"error":{"code":"ImageNotFound"}}`, http.StatusNotFound) |
| 43 | })) |
| 44 | t.Cleanup(srv.Close) |
| 45 | e := NewEntra("https://login.example.test/t/v2.0", "t", "c", "s", "BlueGit-test") |
| 46 | e.HTTP = srv.Client() |
| 47 | e.GraphPhotoURL = srv.URL + "/v1.0/me/photo/$value" |
| 48 | _, err := e.FetchPhoto(context.Background(), "entra-access") |
| 49 | if err != ErrNoPhoto { |
| 50 | t.Fatalf("got %v", err) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | func TestFetchPhotoRejectsForeignHost(t *testing.T) { |
| 55 | var hit bool |
| 56 | foreign := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 57 | hit = true |
| 58 | w.WriteHeader(http.StatusOK) |
| 59 | })) |
| 60 | t.Cleanup(foreign.Close) |
| 61 | e := NewEntra("https://login.example.test/t/v2.0", "t", "c", "s", "BlueGit-test") |
| 62 | e.HTTP = foreign.Client() |
| 63 | // Productie-URL: graph.microsoft.com, niet de testhost. |
| 64 | _, err := e.FetchPhoto(context.Background(), "entra-access") |
| 65 | if err == nil || hit { |
| 66 | t.Fatalf("moet vreemde host weigeren, err=%v hit=%v", err, hit) |
| 67 | } |
| 68 | if !strings.Contains(err.Error(), "graph.microsoft.com") && !strings.Contains(err.Error(), "Graph") { |
| 69 | t.Fatalf("fout %v", err) |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | func TestFetchPhotoRejectsRedirectOffHost(t *testing.T) { |
| 74 | var leak bool |
| 75 | evil := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 76 | leak = true |
| 77 | _, _ = w.Write(tinyPNG()) |
| 78 | })) |
| 79 | t.Cleanup(evil.Close) |
| 80 | mux := http.NewServeMux() |
| 81 | mux.HandleFunc("/v1.0/me/photo/$value", func(w http.ResponseWriter, r *http.Request) { |
| 82 | http.Redirect(w, r, evil.URL+"/stolen", http.StatusFound) |
| 83 | }) |
| 84 | srv := httptest.NewServer(mux) |
| 85 | t.Cleanup(srv.Close) |
| 86 | e := NewEntra("https://login.example.test/t/v2.0", "t", "c", "s", "BlueGit-test") |
| 87 | e.HTTP = srv.Client() |
| 88 | e.GraphPhotoURL = srv.URL + "/v1.0/me/photo/$value" |
| 89 | _, err := e.FetchPhoto(context.Background(), "entra-access") |
| 90 | if err == nil || leak { |
| 91 | t.Fatalf("redirect naar andere host, err=%v leak=%v", err, leak) |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | func TestFetchPhotoRejectsInvalidImage(t *testing.T) { |
| 96 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 97 | w.Header().Set("Content-Type", "image/png") |
| 98 | _, _ = w.Write([]byte("niet-een-plaatje")) |
| 99 | })) |
| 100 | t.Cleanup(srv.Close) |
| 101 | e := NewEntra("https://login.example.test/t/v2.0", "t", "c", "s", "BlueGit-test") |
| 102 | e.HTTP = srv.Client() |
| 103 | e.GraphPhotoURL = srv.URL + "/v1.0/me/photo/$value" |
| 104 | if _, err := e.FetchPhoto(context.Background(), "entra-access"); err == nil { |
| 105 | t.Fatal("ongeldig type") |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | func TestFetchPhotoRejectsTooLarge(t *testing.T) { |
| 110 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 111 | w.Header().Set("Content-Type", "application/octet-stream") |
| 112 | _, _ = w.Write(bytes.Repeat([]byte("x"), graphPhotoMax+2)) |
| 113 | })) |
| 114 | t.Cleanup(srv.Close) |
| 115 | e := NewEntra("https://login.example.test/t/v2.0", "t", "c", "s", "BlueGit-test") |
| 116 | e.HTTP = srv.Client() |
| 117 | e.GraphPhotoURL = srv.URL + "/v1.0/me/photo/$value" |
| 118 | _, err := e.FetchPhoto(context.Background(), "entra-access") |
| 119 | if err == nil || !strings.Contains(err.Error(), "groot") { |
| 120 | t.Fatalf("grootte %v", err) |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | func tinyPNG() []byte { |
| 125 | img := image.NewNRGBA(image.Rect(0, 0, 2, 2)) |
| 126 | img.SetNRGBA(0, 0, color.NRGBA{R: 255, A: 255}) |
| 127 | var buf bytes.Buffer |
| 128 | _ = png.Encode(&buf, img) |
| 129 | return buf.Bytes() |
| 130 | } |