authentik.lib.avatars

Avatar utils

  1"""Avatar utils"""
  2
  3from base64 import b64encode
  4from functools import cache as funccache
  5from hashlib import md5, sha256
  6from typing import TYPE_CHECKING
  7from urllib.parse import urlencode, urlparse
  8
  9from django.core.cache import cache
 10from django.http import HttpRequest, HttpResponseNotFound
 11from django.templatetags.static import static
 12from lxml import etree  # nosec
 13from lxml.etree import Element, SubElement, _Element  # nosec
 14from requests.exceptions import ConnectionError, HTTPError, RequestException, Timeout
 15
 16from authentik.admin.utils import get_system_settings
 17from authentik.lib.utils.dict import get_path_from_dict
 18from authentik.lib.utils.http import get_http_session
 19
 20if TYPE_CHECKING:
 21    from authentik.core.models import User
 22
 23GRAVATAR_URL = "https://www.gravatar.com"
 24DEFAULT_AVATAR = static("dist/assets/images/user_default.png")
 25AVATAR_STATUS_TTL_SECONDS = 60 * 60 * 8  # 8 Hours
 26
 27SVG_XML_NS = "http://www.w3.org/2000/svg"
 28SVG_NS_MAP = {None: SVG_XML_NS}
 29# Match fonts used in web UI
 30SVG_FONTS = [
 31    "'RedHatText'",
 32    "'Overpass'",
 33    "overpass",
 34    "helvetica",
 35    "arial",
 36    "sans-serif",
 37]
 38
 39
 40def avatar_mode_none(user: User, mode: str) -> str | None:
 41    """No avatar"""
 42    return DEFAULT_AVATAR
 43
 44
 45def avatar_mode_attribute(user: User, mode: str) -> str | None:
 46    """Avatars based on a user attribute"""
 47    avatar = get_path_from_dict(user.attributes, mode[11:], default=None)
 48    return avatar
 49
 50
 51def avatar_mode_gravatar(user: User, mode: str) -> str | None:
 52    """Gravatar avatars"""
 53
 54    mail_hash = sha256(user.email.lower().encode("utf-8")).hexdigest()  # nosec
 55    parameters = {"size": "158", "rating": "g", "default": "404"}
 56    gravatar_url = f"{GRAVATAR_URL}/avatar/{mail_hash}?{urlencode(parameters)}"
 57
 58    return avatar_mode_url(user, gravatar_url)
 59
 60
 61def generate_colors(text: str) -> tuple[str, str]:
 62    """Generate colors based on `text`"""
 63    color = (
 64        int(md5(text.lower().encode("utf-8"), usedforsecurity=False).hexdigest(), 16) % 0xFFFFFF
 65    )  # nosec
 66
 67    # Get a (somewhat arbitrarily) reduced scope of colors
 68    # to avoid too dark or light backgrounds
 69    blue = min(max((color) & 0xFF, 55), 200)
 70    green = min(max((color >> 8) & 0xFF, 55), 200)
 71    red = min(max((color >> 16) & 0xFF, 55), 200)
 72    bg_hex = f"{red:02x}{green:02x}{blue:02x}"
 73    # Contrasting text color (https://stackoverflow.com/a/3943023)
 74    text_hex = (
 75        "000" if (red * 0.299 + green * 0.587 + blue * 0.114) > 186 else "fff"  # noqa: PLR2004
 76    )
 77    return bg_hex, text_hex
 78
 79
 80@funccache
 81def generate_avatar_from_name(
 82    name: str,
 83    length: int = 2,
 84    size: int = 64,
 85    rounded: bool = False,
 86    font_size: float = 0.4375,
 87    bold: bool = False,
 88    uppercase: bool = True,
 89) -> str:
 90    """ "Generate an avatar with initials in SVG format.
 91
 92    Inspired from: https://github.com/LasseRafn/ui-avatars
 93    """
 94    name_parts = name.split()
 95    # Only abbreviate first and last name
 96    if len(name_parts) > 2:  # noqa: PLR2004
 97        name_parts = [name_parts[0], name_parts[-1]]
 98
 99    if len(name_parts) == 1:
100        initials = name_parts[0][:length]
101    else:
102        initials = "".join([part[0] for part in name_parts[:-1]])
103        initials += name_parts[-1]
104        initials = initials[:length]
105
106    bg_hex, text_hex = generate_colors(name)
107
108    half_size = size // 2
109    shape = "circle" if rounded else "rect"
110    font_weight = "600" if bold else "400"
111
112    root_element: _Element = Element(f"{{{SVG_XML_NS}}}svg", nsmap=SVG_NS_MAP)
113    root_element.attrib["width"] = f"{size}px"
114    root_element.attrib["height"] = f"{size}px"
115    root_element.attrib["viewBox"] = f"0 0 {size} {size}"
116    root_element.attrib["version"] = "1.1"
117
118    shape = SubElement(root_element, f"{{{SVG_XML_NS}}}{shape}", nsmap=SVG_NS_MAP)
119    shape.attrib["fill"] = f"#{bg_hex}"
120    shape.attrib["cx"] = f"{half_size}"
121    shape.attrib["cy"] = f"{half_size}"
122    shape.attrib["width"] = f"{size}"
123    shape.attrib["height"] = f"{size}"
124    shape.attrib["r"] = f"{half_size}"
125
126    text = SubElement(root_element, f"{{{SVG_XML_NS}}}text", nsmap=SVG_NS_MAP)
127    text.attrib["x"] = "50%"
128    text.attrib["y"] = "50%"
129    text.attrib["style"] = (
130        f"color: #{text_hex}; " "line-height: 1; " f"font-family: {','.join(SVG_FONTS)}; "
131    )
132    text.attrib["fill"] = f"#{text_hex}"
133    text.attrib["alignment-baseline"] = "middle"
134    text.attrib["dominant-baseline"] = "middle"
135    text.attrib["text-anchor"] = "middle"
136    text.attrib["font-size"] = f"{round(size * font_size)}"
137    text.attrib["font-weight"] = f"{font_weight}"
138    text.attrib["dy"] = ".1em"
139    text.text = initials if not uppercase else initials.upper()
140
141    return etree.tostring(root_element).decode()
142
143
144def avatar_mode_generated(user: User, mode: str) -> str | None:
145    """Wrapper that converts generated avatar to base64 svg"""
146    # By default generate based off of user's display name
147    name = user.name.strip()
148    if name == "":
149        # Fallback to username
150        name = user.username.strip()
151    # If we still don't have anything, fallback to `a k`
152    if name == "":
153        name = "a k"
154    svg = generate_avatar_from_name(name)
155    return f"data:image/svg+xml;base64,{b64encode(svg.encode('utf-8')).decode('utf-8')}"
156
157
158def format_avatar_url(user: User, mode: str) -> str:
159    """Format avatar URL placeholders while preserving URL percent-encoding."""
160    mail_hash = md5(user.email.lower().encode("utf-8"), usedforsecurity=False).hexdigest()  # nosec
161    replacements = {
162        "%(username)s": user.username,
163        "%(mail_hash)s": mail_hash,
164        "%(upn)s": str(user.attributes.get("upn", "")),
165    }
166    formatted_url = mode
167    for placeholder, value in replacements.items():
168        formatted_url = formatted_url.replace(placeholder, value)
169    return formatted_url
170
171
172def avatar_mode_url(user: User, mode: str) -> str | None:
173    """Format url"""
174    mail_hash = md5(user.email.lower().encode("utf-8"), usedforsecurity=False).hexdigest()  # nosec
175    formatted_url = format_avatar_url(user, mode)
176
177    hostname = urlparse(formatted_url).hostname
178    cache_key_hostname_available = f"goauthentik.io/lib/avatars/{hostname}/available"
179
180    if not cache.get(cache_key_hostname_available, True):
181        return None
182
183    cache_key_image_url = f"goauthentik.io/lib/avatars/{hostname}/{mail_hash}"
184
185    if cache.has_key(cache_key_image_url):
186        cache.touch(cache_key_image_url, AVATAR_STATUS_TTL_SECONDS)
187        return cache.get(cache_key_image_url)
188
189    try:
190        res = get_http_session().head(formatted_url, timeout=5, allow_redirects=True)
191
192        if res.status_code == HttpResponseNotFound.status_code:
193            cache.set(cache_key_image_url, None, timeout=AVATAR_STATUS_TTL_SECONDS)
194            return None
195        if not res.headers.get("Content-Type", "").startswith("image/"):
196            cache.set(cache_key_image_url, None, timeout=AVATAR_STATUS_TTL_SECONDS)
197            return None
198        res.raise_for_status()
199    except Timeout, ConnectionError, HTTPError:
200        cache.set(cache_key_hostname_available, False, timeout=AVATAR_STATUS_TTL_SECONDS)
201        return None
202    except RequestException:
203        return formatted_url
204
205    cache.set(cache_key_image_url, formatted_url, timeout=AVATAR_STATUS_TTL_SECONDS)
206    return formatted_url
207
208
209def get_avatar(user: User, request: HttpRequest | None = None) -> str:
210    """Get avatar with configured mode"""
211    mode_map = {
212        "none": avatar_mode_none,
213        "initials": avatar_mode_generated,
214        "gravatar": avatar_mode_gravatar,
215    }
216    modes: str = get_system_settings().avatars
217    for mode in modes.split(","):
218        avatar = None
219        if mode in mode_map:
220            avatar = mode_map[mode](user, mode)
221        elif mode.startswith("attributes."):
222            avatar = avatar_mode_attribute(user, mode)
223        elif "://" in mode:
224            avatar = avatar_mode_url(user, mode)
225        if avatar:
226            return avatar
227    return avatar_mode_none(user, modes)
GRAVATAR_URL = 'https://www.gravatar.com'
DEFAULT_AVATAR = '/static/dist/assets/images/user_default.png'
AVATAR_STATUS_TTL_SECONDS = 28800
SVG_XML_NS = 'http://www.w3.org/2000/svg'
SVG_NS_MAP = {None: 'http://www.w3.org/2000/svg'}
SVG_FONTS = ["'RedHatText'", "'Overpass'", 'overpass', 'helvetica', 'arial', 'sans-serif']
def avatar_mode_none(unknown):
41def avatar_mode_none(user: User, mode: str) -> str | None:
42    """No avatar"""
43    return DEFAULT_AVATAR

No avatar

def avatar_mode_attribute(unknown):
46def avatar_mode_attribute(user: User, mode: str) -> str | None:
47    """Avatars based on a user attribute"""
48    avatar = get_path_from_dict(user.attributes, mode[11:], default=None)
49    return avatar

Avatars based on a user attribute

def avatar_mode_gravatar(unknown):
52def avatar_mode_gravatar(user: User, mode: str) -> str | None:
53    """Gravatar avatars"""
54
55    mail_hash = sha256(user.email.lower().encode("utf-8")).hexdigest()  # nosec
56    parameters = {"size": "158", "rating": "g", "default": "404"}
57    gravatar_url = f"{GRAVATAR_URL}/avatar/{mail_hash}?{urlencode(parameters)}"
58
59    return avatar_mode_url(user, gravatar_url)

Gravatar avatars

def generate_colors(text: str) -> tuple[str, str]:
62def generate_colors(text: str) -> tuple[str, str]:
63    """Generate colors based on `text`"""
64    color = (
65        int(md5(text.lower().encode("utf-8"), usedforsecurity=False).hexdigest(), 16) % 0xFFFFFF
66    )  # nosec
67
68    # Get a (somewhat arbitrarily) reduced scope of colors
69    # to avoid too dark or light backgrounds
70    blue = min(max((color) & 0xFF, 55), 200)
71    green = min(max((color >> 8) & 0xFF, 55), 200)
72    red = min(max((color >> 16) & 0xFF, 55), 200)
73    bg_hex = f"{red:02x}{green:02x}{blue:02x}"
74    # Contrasting text color (https://stackoverflow.com/a/3943023)
75    text_hex = (
76        "000" if (red * 0.299 + green * 0.587 + blue * 0.114) > 186 else "fff"  # noqa: PLR2004
77    )
78    return bg_hex, text_hex

Generate colors based on text

@funccache
def generate_avatar_from_name( name: str, length: int = 2, size: int = 64, rounded: bool = False, font_size: float = 0.4375, bold: bool = False, uppercase: bool = True) -> str:
 81@funccache
 82def generate_avatar_from_name(
 83    name: str,
 84    length: int = 2,
 85    size: int = 64,
 86    rounded: bool = False,
 87    font_size: float = 0.4375,
 88    bold: bool = False,
 89    uppercase: bool = True,
 90) -> str:
 91    """ "Generate an avatar with initials in SVG format.
 92
 93    Inspired from: https://github.com/LasseRafn/ui-avatars
 94    """
 95    name_parts = name.split()
 96    # Only abbreviate first and last name
 97    if len(name_parts) > 2:  # noqa: PLR2004
 98        name_parts = [name_parts[0], name_parts[-1]]
 99
100    if len(name_parts) == 1:
101        initials = name_parts[0][:length]
102    else:
103        initials = "".join([part[0] for part in name_parts[:-1]])
104        initials += name_parts[-1]
105        initials = initials[:length]
106
107    bg_hex, text_hex = generate_colors(name)
108
109    half_size = size // 2
110    shape = "circle" if rounded else "rect"
111    font_weight = "600" if bold else "400"
112
113    root_element: _Element = Element(f"{{{SVG_XML_NS}}}svg", nsmap=SVG_NS_MAP)
114    root_element.attrib["width"] = f"{size}px"
115    root_element.attrib["height"] = f"{size}px"
116    root_element.attrib["viewBox"] = f"0 0 {size} {size}"
117    root_element.attrib["version"] = "1.1"
118
119    shape = SubElement(root_element, f"{{{SVG_XML_NS}}}{shape}", nsmap=SVG_NS_MAP)
120    shape.attrib["fill"] = f"#{bg_hex}"
121    shape.attrib["cx"] = f"{half_size}"
122    shape.attrib["cy"] = f"{half_size}"
123    shape.attrib["width"] = f"{size}"
124    shape.attrib["height"] = f"{size}"
125    shape.attrib["r"] = f"{half_size}"
126
127    text = SubElement(root_element, f"{{{SVG_XML_NS}}}text", nsmap=SVG_NS_MAP)
128    text.attrib["x"] = "50%"
129    text.attrib["y"] = "50%"
130    text.attrib["style"] = (
131        f"color: #{text_hex}; " "line-height: 1; " f"font-family: {','.join(SVG_FONTS)}; "
132    )
133    text.attrib["fill"] = f"#{text_hex}"
134    text.attrib["alignment-baseline"] = "middle"
135    text.attrib["dominant-baseline"] = "middle"
136    text.attrib["text-anchor"] = "middle"
137    text.attrib["font-size"] = f"{round(size * font_size)}"
138    text.attrib["font-weight"] = f"{font_weight}"
139    text.attrib["dy"] = ".1em"
140    text.text = initials if not uppercase else initials.upper()
141
142    return etree.tostring(root_element).decode()

"Generate an avatar with initials in SVG format.

Inspired from: https://github.com/LasseRafn/ui-avatars

def avatar_mode_generated(unknown):
145def avatar_mode_generated(user: User, mode: str) -> str | None:
146    """Wrapper that converts generated avatar to base64 svg"""
147    # By default generate based off of user's display name
148    name = user.name.strip()
149    if name == "":
150        # Fallback to username
151        name = user.username.strip()
152    # If we still don't have anything, fallback to `a k`
153    if name == "":
154        name = "a k"
155    svg = generate_avatar_from_name(name)
156    return f"data:image/svg+xml;base64,{b64encode(svg.encode('utf-8')).decode('utf-8')}"

Wrapper that converts generated avatar to base64 svg

def format_avatar_url(unknown):
159def format_avatar_url(user: User, mode: str) -> str:
160    """Format avatar URL placeholders while preserving URL percent-encoding."""
161    mail_hash = md5(user.email.lower().encode("utf-8"), usedforsecurity=False).hexdigest()  # nosec
162    replacements = {
163        "%(username)s": user.username,
164        "%(mail_hash)s": mail_hash,
165        "%(upn)s": str(user.attributes.get("upn", "")),
166    }
167    formatted_url = mode
168    for placeholder, value in replacements.items():
169        formatted_url = formatted_url.replace(placeholder, value)
170    return formatted_url

Format avatar URL placeholders while preserving URL percent-encoding.

def avatar_mode_url(unknown):
173def avatar_mode_url(user: User, mode: str) -> str | None:
174    """Format url"""
175    mail_hash = md5(user.email.lower().encode("utf-8"), usedforsecurity=False).hexdigest()  # nosec
176    formatted_url = format_avatar_url(user, mode)
177
178    hostname = urlparse(formatted_url).hostname
179    cache_key_hostname_available = f"goauthentik.io/lib/avatars/{hostname}/available"
180
181    if not cache.get(cache_key_hostname_available, True):
182        return None
183
184    cache_key_image_url = f"goauthentik.io/lib/avatars/{hostname}/{mail_hash}"
185
186    if cache.has_key(cache_key_image_url):
187        cache.touch(cache_key_image_url, AVATAR_STATUS_TTL_SECONDS)
188        return cache.get(cache_key_image_url)
189
190    try:
191        res = get_http_session().head(formatted_url, timeout=5, allow_redirects=True)
192
193        if res.status_code == HttpResponseNotFound.status_code:
194            cache.set(cache_key_image_url, None, timeout=AVATAR_STATUS_TTL_SECONDS)
195            return None
196        if not res.headers.get("Content-Type", "").startswith("image/"):
197            cache.set(cache_key_image_url, None, timeout=AVATAR_STATUS_TTL_SECONDS)
198            return None
199        res.raise_for_status()
200    except Timeout, ConnectionError, HTTPError:
201        cache.set(cache_key_hostname_available, False, timeout=AVATAR_STATUS_TTL_SECONDS)
202        return None
203    except RequestException:
204        return formatted_url
205
206    cache.set(cache_key_image_url, formatted_url, timeout=AVATAR_STATUS_TTL_SECONDS)
207    return formatted_url

Format url

def get_avatar(unknown):
210def get_avatar(user: User, request: HttpRequest | None = None) -> str:
211    """Get avatar with configured mode"""
212    mode_map = {
213        "none": avatar_mode_none,
214        "initials": avatar_mode_generated,
215        "gravatar": avatar_mode_gravatar,
216    }
217    modes: str = get_system_settings().avatars
218    for mode in modes.split(","):
219        avatar = None
220        if mode in mode_map:
221            avatar = mode_map[mode](user, mode)
222        elif mode.startswith("attributes."):
223            avatar = avatar_mode_attribute(user, mode)
224        elif "://" in mode:
225            avatar = avatar_mode_url(user, mode)
226        if avatar:
227            return avatar
228    return avatar_mode_none(user, modes)

Get avatar with configured mode