Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use new gravatar hash routine #1825

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/admin/configure.rst
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,11 @@ service. To enable it, add or uncomment this line in wikiconfig::

user_use_gravatar = True

If a user is not registered with gravar.com, a default image can be specified using
the parameter user_gravatar_default_img, this can be a publicly available URL or a
keyword “mp”, “identicon”, “monsterid”, “wavatar”, “retro” or “robohash”, the default
value is “blank” (see https://docs.gravatar.com/api/avatars/images/ for details).

Please note that using the gravatar service has some privacy issues:

* to register your image for your email at gravatar.com, you need to give them
Expand Down
1 change: 1 addition & 0 deletions src/moin/config/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,7 @@ def __init__(self, exprstr):
"interwiki name of the wiki where the user home pages are located [Unicode] - useful if you have ''many'' users. You could even link to nonwiki \"user pages\" if the wiki username is in the target URL.",
),
("use_gravatar", False, "if True, gravatar.com will be used to find User's avatar"),
("gravatar_default_img", "blank", "default image if email not registered at gravatar.com."),
),
),
"mail": (
Expand Down
3 changes: 3 additions & 0 deletions src/moin/config/wikiconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ class Config(DefaultConfig):

# read about PRIVACY ISSUES in docs before uncommenting the line below to use gravatars
# user_use_gravatar = True
# user_gravatar_default_img = "blank" # or "mp", "identicon", "monsterid", "wavatar", "retro", "robohash".
# you can also supply a publicly available image URL with user_gravatar_default_img,
# see https://docs.gravatar.com/api/avatars/images/ for details

# read about SECURITY ISSUES in docs before uncommenting the line below allowing users
# to edit style attributes in HTML and Markdown items
Expand Down
26 changes: 12 additions & 14 deletions src/moin/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import copy
import hashlib
import werkzeug
from io import BytesIO

from babel import parse_locale
Expand All @@ -29,6 +28,7 @@
from flask import g as flaskg
from flask import session, url_for, render_template
from jinja2.runtime import Undefined
from urllib.parse import urlencode

from moin import wikiutil
from moin.constants.contenttypes import CONTENTTYPE_USER
Expand Down Expand Up @@ -411,24 +411,22 @@ def avatar(self, size=30):
if not app.cfg.user_use_gravatar:
return None

from moin.themes import get_current_theme
from flask_theme import static_file_url

theme = get_current_theme()
if app.cfg.user_gravatar_default_img:
default = app.cfg.user_gravatar_default_img
else:
default = "blank"

email = self.email
if not email:
return static_file_url(theme, theme.info.get("default_avatar", "img/default_avatar.png"))

param = {}
param["gravatar_id"] = hashlib.md5(email.lower()).hexdigest()
if not email:
logging.warning(f"User {self.name0} has no valid email, cannot create an avatar.")
return None

param["default"] = static_file_url(theme, theme.info.get("default_avatar", "img/default_avatar.png"), True)
email_encoded = email.lower().encode("utf-8")
email_hash = hashlib.sha256(email_encoded).hexdigest()

param["size"] = str(size)
# TODO: use same protocol of Moin site (might be https instead of http)]
gravatar_url = "http://www.gravatar.com/avatar.php?"
gravatar_url += werkzeug.url_encode(param)
query_params = urlencode({"d": default, "s": str(size)})
gravatar_url = f"https://www.gravatar.com/avatar/{email_hash}?{query_params}"

return gravatar_url

Expand Down
Loading