|
| 1 | +// Copyright (C) 2025 Homer Server Contributors |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | + |
| 5 | +package handlers |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "fmt" |
| 10 | + "io" |
| 11 | + "net/http" |
| 12 | + "strings" |
| 13 | + "time" |
| 14 | + |
| 15 | + "github.com/labstack/echo/v4" |
| 16 | +) |
| 17 | + |
| 18 | +const maxAvatarBytes = 2 << 20 // 2 MiB |
| 19 | + |
| 20 | +// V4GetMeAvatar serves the cached SSO profile photo for the authenticated user. |
| 21 | +// GET /api/v4/me/avatar |
| 22 | +func (h *AuthHandler) V4GetMeAvatar(c echo.Context) error { |
| 23 | + claims, err := h.jwtClaimsFromContext(c) |
| 24 | + if err != nil { |
| 25 | + return v4ContextError(c, err) |
| 26 | + } |
| 27 | + if h.avatarStore == nil { |
| 28 | + return c.NoContent(http.StatusNotFound) |
| 29 | + } |
| 30 | + data, contentType, ok := h.avatarStore.Get(claims.Username) |
| 31 | + if !ok { |
| 32 | + return c.NoContent(http.StatusNotFound) |
| 33 | + } |
| 34 | + return c.Blob(http.StatusOK, contentType, data) |
| 35 | +} |
| 36 | + |
| 37 | +func oauthAvatarURLs(profile map[string]interface{}, provider *OAuthProvider) []string { |
| 38 | + seen := make(map[string]struct{}) |
| 39 | + var urls []string |
| 40 | + add := func(u string) { |
| 41 | + u = strings.TrimSpace(u) |
| 42 | + if u == "" { |
| 43 | + return |
| 44 | + } |
| 45 | + if _, ok := seen[u]; ok { |
| 46 | + return |
| 47 | + } |
| 48 | + seen[u] = struct{}{} |
| 49 | + urls = append(urls, u) |
| 50 | + } |
| 51 | + |
| 52 | + add(oauthStringClaim(profile, "picture")) |
| 53 | + add(oauthStringClaim(profile, "avatar")) |
| 54 | + add(oauthStringClaim(profile, "photo")) |
| 55 | + |
| 56 | + if provider != nil { |
| 57 | + profileURL := strings.ToLower(provider.ProfileURL) |
| 58 | + name := strings.ToLower(provider.Name) |
| 59 | + if strings.Contains(profileURL, "graph.microsoft.com") || name == "azure" || name == "microsoft" { |
| 60 | + add("https://graph.microsoft.com/v1.0/me/photo/$value") |
| 61 | + } |
| 62 | + } |
| 63 | + return urls |
| 64 | +} |
| 65 | + |
| 66 | +func fetchOAuthProfilePhoto(ctx context.Context, client *http.Client, profile map[string]interface{}, provider *OAuthProvider) ([]byte, string, error) { |
| 67 | + if client == nil { |
| 68 | + return nil, "", fmt.Errorf("http client is nil") |
| 69 | + } |
| 70 | + var lastErr error |
| 71 | + for _, u := range oauthAvatarURLs(profile, provider) { |
| 72 | + data, ctype, err := fetchAuthenticatedImage(ctx, client, u) |
| 73 | + if err != nil { |
| 74 | + lastErr = err |
| 75 | + continue |
| 76 | + } |
| 77 | + if len(data) > 0 { |
| 78 | + return data, ctype, nil |
| 79 | + } |
| 80 | + } |
| 81 | + if lastErr != nil { |
| 82 | + return nil, "", lastErr |
| 83 | + } |
| 84 | + return nil, "", fmt.Errorf("no profile photo available") |
| 85 | +} |
| 86 | + |
| 87 | +func fetchAuthenticatedImage(ctx context.Context, client *http.Client, rawURL string) ([]byte, string, error) { |
| 88 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, rawURL, nil) |
| 89 | + if err != nil { |
| 90 | + return nil, "", err |
| 91 | + } |
| 92 | + resp, err := client.Do(req) |
| 93 | + if err != nil { |
| 94 | + return nil, "", err |
| 95 | + } |
| 96 | + defer resp.Body.Close() |
| 97 | + if resp.StatusCode == http.StatusNotFound || resp.StatusCode == http.StatusNoContent { |
| 98 | + return nil, "", fmt.Errorf("photo HTTP %d", resp.StatusCode) |
| 99 | + } |
| 100 | + if resp.StatusCode != http.StatusOK { |
| 101 | + return nil, "", fmt.Errorf("photo HTTP %d", resp.StatusCode) |
| 102 | + } |
| 103 | + body, err := io.ReadAll(io.LimitReader(resp.Body, maxAvatarBytes)) |
| 104 | + if err != nil { |
| 105 | + return nil, "", err |
| 106 | + } |
| 107 | + ctype := strings.TrimSpace(resp.Header.Get("Content-Type")) |
| 108 | + if ctype == "" || !strings.HasPrefix(ctype, "image/") { |
| 109 | + ctype = "image/jpeg" |
| 110 | + } |
| 111 | + return body, ctype, nil |
| 112 | +} |
| 113 | + |
| 114 | +func cacheOAuthAvatar(h *AuthHandler, username string, profile map[string]interface{}, client *http.Client, provider *OAuthProvider) { |
| 115 | + if h == nil || h.avatarStore == nil || username == "" || client == nil { |
| 116 | + return |
| 117 | + } |
| 118 | + ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) |
| 119 | + defer cancel() |
| 120 | + data, ctype, err := fetchOAuthProfilePhoto(ctx, client, profile, provider) |
| 121 | + if err != nil || len(data) == 0 { |
| 122 | + return |
| 123 | + } |
| 124 | + h.avatarStore.Put(username, data, ctype) |
| 125 | +} |
0 commit comments