| 1 | package auth |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "image" |
| 9 | _ "image/gif" |
| 10 | _ "image/jpeg" |
| 11 | _ "image/png" |
| 12 | "io" |
| 13 | "net/http" |
| 14 | "net/url" |
| 15 | "strings" |
| 16 | "time" |
| 17 | ) |
| 18 | |
| 19 | // ErrNoPhoto betekent dat Entra/Graph geen profielfoto heeft (404). |
| 20 | var ErrNoPhoto = errors.New("auth: geen Entra-profielfoto") |
| 21 | |
| 22 | const ( |
| 23 | defaultGraphPhotoURL = "https://graph.microsoft.com/v1.0/me/photo/$value" |
| 24 | graphPhotoMax = 2 << 20 |
| 25 | ) |
| 26 | |
| 27 | // GraphPhotoURL overschrijft de Graph-foto-URL (alleen voor tests). |
| 28 | // Leeg is de productie-URL op graph.microsoft.com. |
| 29 | func (e *Entra) photoURL() string { |
| 30 | if e != nil && strings.TrimSpace(e.GraphPhotoURL) != "" { |
| 31 | return strings.TrimSpace(e.GraphPhotoURL) |
| 32 | } |
| 33 | return defaultGraphPhotoURL |
| 34 | } |
| 35 | |
| 36 | // FetchPhoto haalt de profielfoto op bij Microsoft Graph. |
| 37 | // Alleen de Graph-host (of de testhost in GraphPhotoURL) is toegestaan. |
| 38 | // 404 wordt ErrNoPhoto; andere fouten blijven een gewone error. |
| 39 | // De bytes zijn een geldige rasterafbeelding, begrensd tot graphPhotoMax. |
| 40 | func (e *Entra) FetchPhoto(ctx context.Context, accessToken string) ([]byte, error) { |
| 41 | if e == nil || strings.TrimSpace(accessToken) == "" { |
| 42 | return nil, ErrNoPhoto |
| 43 | } |
| 44 | rawURL := e.photoURL() |
| 45 | if err := e.graphURLOK(rawURL); err != nil { |
| 46 | return nil, err |
| 47 | } |
| 48 | req, err := http.NewRequestWithContext(ctx, http.MethodGet, rawURL, nil) |
| 49 | if err != nil { |
| 50 | return nil, err |
| 51 | } |
| 52 | req.Header.Set("Authorization", "Bearer "+accessToken) |
| 53 | req.Header.Set("Accept", "image/*") |
| 54 | if e.UserAgent != "" { |
| 55 | req.Header.Set("User-Agent", e.UserAgent) |
| 56 | } |
| 57 | res, err := e.graphClient().Do(req) |
| 58 | if err != nil { |
| 59 | return nil, fmt.Errorf("auth: Graph-foto: %w", err) |
| 60 | } |
| 61 | defer res.Body.Close() |
| 62 | if res.StatusCode == http.StatusNotFound || res.StatusCode == http.StatusNoContent { |
| 63 | _, _ = io.Copy(io.Discard, io.LimitReader(res.Body, 64<<10)) |
| 64 | return nil, ErrNoPhoto |
| 65 | } |
| 66 | data, err := io.ReadAll(io.LimitReader(res.Body, graphPhotoMax+1)) |
| 67 | if err != nil { |
| 68 | return nil, fmt.Errorf("auth: Graph-foto lezen: %w", err) |
| 69 | } |
| 70 | if len(data) > graphPhotoMax { |
| 71 | return nil, fmt.Errorf("auth: Graph-foto te groot") |
| 72 | } |
| 73 | if res.StatusCode >= 400 { |
| 74 | return nil, fmt.Errorf("auth: Graph-foto: %s", res.Status) |
| 75 | } |
| 76 | if _, _, err := image.Decode(bytes.NewReader(data)); err != nil { |
| 77 | return nil, fmt.Errorf("auth: Graph-foto ongeldig: %w", err) |
| 78 | } |
| 79 | return data, nil |
| 80 | } |
| 81 | |
| 82 | func (e *Entra) graphClient() *http.Client { |
| 83 | base := e.HTTP |
| 84 | if base == nil { |
| 85 | base = &http.Client{Timeout: 15 * time.Second} |
| 86 | } |
| 87 | c := *base |
| 88 | c.CheckRedirect = func(req *http.Request, via []*http.Request) error { |
| 89 | if len(via) >= 5 { |
| 90 | return fmt.Errorf("auth: te veel redirects") |
| 91 | } |
| 92 | return e.graphURLOK(req.URL.String()) |
| 93 | } |
| 94 | return &c |
| 95 | } |
| 96 | |
| 97 | func (e *Entra) graphURLOK(raw string) error { |
| 98 | u, err := url.Parse(raw) |
| 99 | if err != nil || u.Host == "" { |
| 100 | return fmt.Errorf("auth: ongeldige Graph-URL") |
| 101 | } |
| 102 | want, err := url.Parse(e.photoURL()) |
| 103 | if err != nil || want.Host == "" { |
| 104 | return fmt.Errorf("auth: ongeldige Graph-URL") |
| 105 | } |
| 106 | if !strings.EqualFold(u.Host, want.Host) { |
| 107 | return fmt.Errorf("auth: Graph-foto alleen vanaf %s", want.Host) |
| 108 | } |
| 109 | if e.GraphPhotoURL == "" && u.Scheme != "https" { |
| 110 | return fmt.Errorf("auth: Graph-foto moet HTTPS zijn") |
| 111 | } |
| 112 | if u.Scheme != "https" && u.Scheme != "http" { |
| 113 | return fmt.Errorf("auth: ongeldige Graph-URL") |
| 114 | } |
| 115 | return nil |
| 116 | } |