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

Add gravatar #256

Merged
merged 1 commit into from
Oct 11, 2024
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
16 changes: 13 additions & 3 deletions amt/api/routes/auth.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import hashlib
import logging
from urllib.parse import quote_plus

from authlib.integrations.starlette_client import OAuth, OAuthError # type: ignore
from fastapi import APIRouter, Request
from fastapi.responses import HTMLResponse, RedirectResponse, Response

from amt.core.authorization import get_user
from amt.core.exceptions import AMTAuthorizationError
from amt.core.exceptions import AMTAuthorizationFlowError

router = APIRouter()
logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -44,9 +46,17 @@ async def auth_callback(request: Request) -> Response: # pragma: no cover
try:
token = await oauth.keycloak.authorize_access_token(request) # type: ignore
except OAuthError as error:
raise AMTAuthorizationError() from error
raise AMTAuthorizationFlowError() from error

user: dict[str, str | int | bool] = token.get("userinfo") # type: ignore
if "email" in user and isinstance(user["email"], str):
email: str = str(user["email"]).strip().lower() # type: ignore
user["email_hash"] = hashlib.sha256(email.encode()).hexdigest()

if "name" in user and isinstance(user["name"], str):
name: str = str(user["name"]).strip().lower() # type: ignore
user["name_encoded"] = quote_plus(name)

user: dict = token.get("userinfo") # type: ignore
if user:
request.session["user"] = dict(user) # type: ignore
request.session["id_token"] = token["id_token"] # type: ignore
Expand Down
6 changes: 6 additions & 0 deletions amt/core/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,9 @@ class AMTAuthorizationError(AMTHTTPException):
def __init__(self) -> None:
self.detail: str = _("Failed to authorize, please login and try again.")
super().__init__(status.HTTP_401_UNAUTHORIZED, self.detail)


class AMTAuthorizationFlowError(AMTHTTPException):
def __init__(self) -> None:
self.detail: str = _("Something went wrong during the authorization flow. Please try again later.")
super().__init__(status.HTTP_401_UNAUTHORIZED, self.detail)
27 changes: 21 additions & 6 deletions amt/locale/base.pot
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2024-10-11 17:18+0200\n"
"POT-Creation-Date: 2024-10-11 19:45+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -179,6 +179,26 @@ msgstr ""
msgid "Failed to authorize, please login and try again."
msgstr ""

#: amt/core/exceptions.py:78
msgid ""
"Something went wrong during the authorization flow. Please try again "
"later."
msgstr ""

#: amt/site/templates/errors/AMTAuthorizationError_401.html.j2:6
msgid "Not logged in"
msgstr ""

#: amt/site/templates/errors/AMTAuthorizationError_401.html.j2:8
msgid "Please login to view this page"
msgstr ""

#: amt/site/templates/errors/AMTAuthorizationError_401.html.j2:12
#: amt/site/templates/parts/header.html.j2:83
#: amt/site/templates/parts/header.html.j2:137
msgid "Login"
msgstr ""

#: amt/site/templates/errors/Exception.html.j2:6
msgid "An error occurred"
msgstr ""
Expand Down Expand Up @@ -276,11 +296,6 @@ msgstr ""
msgid "Language"
msgstr ""

#: amt/site/templates/parts/header.html.j2:83
#: amt/site/templates/parts/header.html.j2:137
msgid "Login"
msgstr ""

#: amt/site/templates/parts/header.html.j2:90
#: amt/site/templates/parts/header.html.j2:143
msgid "Logout"
Expand Down
Binary file modified amt/locale/en_US/LC_MESSAGES/messages.mo
Binary file not shown.
27 changes: 21 additions & 6 deletions amt/locale/en_US/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2024-10-11 17:18+0200\n"
"POT-Creation-Date: 2024-10-11 19:45+0000\n"
"PO-Revision-Date: 2024-07-25 21:01+0200\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language: en_US\n"
Expand Down Expand Up @@ -180,6 +180,26 @@ msgstr ""
msgid "Failed to authorize, please login and try again."
msgstr ""

#: amt/core/exceptions.py:78
msgid ""
"Something went wrong during the authorization flow. Please try again "
"later."
msgstr ""

#: amt/site/templates/errors/AMTAuthorizationError_401.html.j2:6
msgid "Not logged in"
msgstr ""

#: amt/site/templates/errors/AMTAuthorizationError_401.html.j2:8
msgid "Please login to view this page"
msgstr ""

#: amt/site/templates/errors/AMTAuthorizationError_401.html.j2:12
#: amt/site/templates/parts/header.html.j2:83
#: amt/site/templates/parts/header.html.j2:137
msgid "Login"
msgstr ""

#: amt/site/templates/errors/Exception.html.j2:6
msgid "An error occurred"
msgstr ""
Expand Down Expand Up @@ -277,11 +297,6 @@ msgstr ""
msgid "Language"
msgstr ""

#: amt/site/templates/parts/header.html.j2:83
#: amt/site/templates/parts/header.html.j2:137
msgid "Login"
msgstr ""

#: amt/site/templates/parts/header.html.j2:90
#: amt/site/templates/parts/header.html.j2:143
msgid "Logout"
Expand Down
Binary file modified amt/locale/nl_NL/LC_MESSAGES/messages.mo
Binary file not shown.
27 changes: 21 additions & 6 deletions amt/locale/nl_NL/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2024-10-11 17:18+0200\n"
"POT-Creation-Date: 2024-10-11 19:45+0000\n"
"PO-Revision-Date: 2024-07-25 21:01+0200\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language: nl_NL\n"
Expand Down Expand Up @@ -186,6 +186,26 @@ msgstr "Waarde is niet correct: {field}"
msgid "Failed to authorize, please login and try again."
msgstr "Autoriseren is mislukt. Meld u aan en probeer het opnieuw."

#: amt/core/exceptions.py:78
msgid ""
"Something went wrong during the authorization flow. Please try again "
"later."
msgstr "Er is iets fout gegaan tijdens de autorisatiestroom. Probeer het later opnieuw"

#: amt/site/templates/errors/AMTAuthorizationError_401.html.j2:6
msgid "Not logged in"
msgstr "Niet ingelogd"

#: amt/site/templates/errors/AMTAuthorizationError_401.html.j2:8
msgid "Please login to view this page"
msgstr "Meld u aan om deze pagina te bekijken"

#: amt/site/templates/errors/AMTAuthorizationError_401.html.j2:12
#: amt/site/templates/parts/header.html.j2:83
#: amt/site/templates/parts/header.html.j2:137
msgid "Login"
msgstr "Inloggen"

#: amt/site/templates/errors/Exception.html.j2:6
msgid "An error occurred"
msgstr "Er is een fout opgetreden"
Expand Down Expand Up @@ -283,11 +303,6 @@ msgstr "versie"
msgid "Language"
msgstr "Taal"

#: amt/site/templates/parts/header.html.j2:83
#: amt/site/templates/parts/header.html.j2:137
msgid "Login"
msgstr "Inloggen"

#: amt/site/templates/parts/header.html.j2:90
#: amt/site/templates/parts/header.html.j2:143
msgid "Logout"
Expand Down
17 changes: 17 additions & 0 deletions amt/site/templates/errors/AMTAuthorizationError_401.html.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% extends 'layouts/base.html.j2' %}

{% block content %}
<div class="rvo-max-width-layout rvo-max-width-layout--md rvo-max-width-layout-inline-padding--none">
<div class="rvo-content">
<h1 class="utrecht-heading-1 rvo-layout-row rvo-layout-gap--sm">{% trans %}Not logged in{% endtrans %}</h1>
<p>
{% trans %}Please login to view this page{% endtrans %}. <a
class="rvo-link rvo-link--with-icon rvo-link--no-underline" href="/auth/login">
<span
class="utrecht-icon rvo-icon rvo-icon-externe-link rvo-icon--md rvo-icon--hemelblauw rvo-link__icon--before"
role="img" aria-label="Externe link"></span> {% trans %}Login{% endtrans %}
</a> </p>
</div>
</div>

{% endblock %}
6 changes: 6 additions & 0 deletions amt/site/templates/parts/header.html.j2
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@
aria-label="User"></span>
{% trans %}Logout{%endtrans %}
</a>
<a class="rvo-link rvo-topnav__link rvo-link--logoblauw" href="#">
<img width="24" height="24"
src="https://gravatar.com/avatar/{{user['email_hash']}}?r=g&s=48&d=https%3A%2F%2Fui-avatars.com%2Fapi%2F/{{user['name_encoded']}}/48"
alt="User icon">
{{ user['name'] }}
</a>
{% endif %}
</li>
</ul>
Expand Down
Loading